From 78d53aeddbfcc9a585654ad371b344d57e9529d8 Mon Sep 17 00:00:00 2001 From: louiscklaw Date: Fri, 31 Jan 2025 19:51:26 +0800 Subject: [PATCH] update, --- hck717/gitUpdate.bat | 7 +++++++ hck717/q1.md | 34 +++++++++++++++++++++++++++++++ hck717/q2.md | 48 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 hck717/gitUpdate.bat create mode 100644 hck717/q1.md create mode 100644 hck717/q2.md diff --git a/hck717/gitUpdate.bat b/hck717/gitUpdate.bat new file mode 100644 index 00000000..1efde65b --- /dev/null +++ b/hck717/gitUpdate.bat @@ -0,0 +1,7 @@ +git status . + +@pause + +git add . +git commit -m"update hck717," +start git push \ No newline at end of file diff --git a/hck717/q1.md b/hck717/q1.md new file mode 100644 index 00000000..9faaf9ca --- /dev/null +++ b/hck717/q1.md @@ -0,0 +1,34 @@ +A peak in a 1D list of values is a value that is not smaller than its neighbours. Assume all values are natural numbers. + +For example, consider the following list of values +a = [1, 6, 7, 4, 3, 2, 1, 4, 5, 6] + +Here, 7 is a peak and 6 is a peak. + +Write a python program that uses recursion to find a peak (not all, just one peak) given a list of values. +Your function should return the list index of the peak. +To do so you must implement the following recursive algorithm that uses the same idea as binary search (i.e., divide and conquer). + •Recursively look at position n//2 + •if a[n//2] < a[n//2 - 1] then only look at left half 0 … n//2-1 to look for a peak + •else if a[n//2] < a[n//2 + 1] then only look at right half n//2+1 … n-1 to look for a peak + •else n//2 position is a peak + +Note that although there are two peaks in the given example, this method will only find peak 7 at index 2. Your program must follow this behaviour. + +Input specification: + • +A line with the numbers separated by space + +Output specification: + • +The index of the peak + +Sample testcases (The bolded text are user input) + +Case 1: +1 6 7 4 3 2 1 4 5 6 +2 + +Case 2: +1 10 2 +1 \ No newline at end of file diff --git a/hck717/q2.md b/hck717/q2.md new file mode 100644 index 00000000..7c05e3bc --- /dev/null +++ b/hck717/q2.md @@ -0,0 +1,48 @@ +A peak in a 2D list of values is a value that is not smaller than its 4 neighbours. Assume all values are natural numbers. + +For example, consider the following list of values +a = [[10, 8, 10, 10], + [14, 13, 12, 11], + [15, 9, 11, 21], + [16, 17, 19, 20]] + +Here, 21 is the only peak. + +Write a python program that uses recursion to find a peak (not all, just one peak) given a list of 2D values (m columns, n rows). +Your function should return the list index as a tuple of the peak. +To do so you must implement the following recursive algorithm that uses the same idea as binary search (i.e., divide and conquer). + • Pick middle column j = m//2 + • Find global max on column j at (i, j), if there are multiple global max, pick the smallest possible i + • Compare (i, j-1), (i, j), (i, j+1) + • Pick left columns if (i, j-1) > (i, j) + ◦ Similar for right + • (i, j) is a 2D-peak if neither conditions holds + • Solve the new problem with half the number of columns + • When you have a single column, find global max and you are done (again, if there are multiple global max, pick the smallest possible index) + +Similar to Q4, this method will always find a specific peak among all the others that may exist in the list of 2D values. Your program must follow this behaviour. + +Input specification: + • n lines and m columns of numbers separated by space + • follow by an empty line input to determine the end of the input + +Output specification: + • The index as a tuple of the peak + +Sample testcases (The bolded text are user input, including the blank line) + +Case 1: +10 8 10 10 +14 13 12 11 +15 9 11 21 +16 17 19 20 + +(2, 3) + +Case 2: +1 2 3 +4 5 6 +7 8 9 +10 11 12 + +(3, 2) \ No newline at end of file