From 74da98b5bd08347c5e9d2e417ea37d6a3b602026 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 09:51:56 +0300 Subject: [PATCH 1/5] Create find_maxima function --- local_maxima.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 local_maxima.py diff --git a/local_maxima.py b/local_maxima.py new file mode 100644 index 0000000..154b290 --- /dev/null +++ b/local_maxima.py @@ -0,0 +1,17 @@ +def find_maxima(points): + indices = [] + + if not points: + print("No input given") + return [] + elif len(points) == 1: + return [0] + + if points[0] > points[1]: + indices.append(0) + for i in range(1, len(points)-1): + if points[i] > points[i+1] and points[i] > points[i-1]: + indices.append(i) + if points[-1] > points[-2]: + indices.append(len(points)-1) + return indices \ No newline at end of file From a50a9cc98d6aad705fcff0b6c05189c395d6a9c2 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 10:51:02 +0300 Subject: [PATCH 2/5] Implemented missing tests --- hands_on/local_maxima_part2/test_local_maxima.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/hands_on/local_maxima_part2/test_local_maxima.py b/hands_on/local_maxima_part2/test_local_maxima.py index 316442d..d6561f5 100644 --- a/hands_on/local_maxima_part2/test_local_maxima.py +++ b/hands_on/local_maxima_part2/test_local_maxima.py @@ -23,8 +23,14 @@ def test_find_maxima_empty(): def test_find_maxima_plateau(): - raise Exception('not yet implemented') + values = [1, 2, 2, 1] + expected = [1] + maxima = find_maxima(values) + assert maxima == expected def test_find_maxima_not_a_plateau(): - raise Exception('not yet implemented') + values = [1, 2, 2, 3, 1] + expected = [3] + maxima = find_maxima(values) + assert maxima == expected From 7a69525cb90fc27bb1a075c221940ef7d934e4b6 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 10:51:19 +0300 Subject: [PATCH 3/5] Implement local_maxima function --- hands_on/local_maxima_part2/local_maxima.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/hands_on/local_maxima_part2/local_maxima.py b/hands_on/local_maxima_part2/local_maxima.py index db89ba3..dec43ac 100644 --- a/hands_on/local_maxima_part2/local_maxima.py +++ b/hands_on/local_maxima_part2/local_maxima.py @@ -1,4 +1,4 @@ -def find_maxima(x): +def find_maxima(points): """Find local maxima of x. Input arguments: @@ -7,4 +7,19 @@ def find_maxima(x): Output: idx -- list of indices of the local maxima in x """ - return [] + indices = [] + + if not points: + print("No input given") + return [] + elif len(points) == 1: + return [0] + + if points[0] > points[1]: + indices.append(0) + for i in range(1, len(points)-1): + if points[i] > points[i+1] and points[i] > points[i-1]: + indices.append(i) + if points[-1] > points[-2]: + indices.append(len(points)-1) + return indices \ No newline at end of file From 660c6d9b504cf2083efadc94881dced3771e563c Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 10:52:08 +0300 Subject: [PATCH 4/5] Update variable names --- hands_on/local_maxima_part2/local_maxima.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hands_on/local_maxima_part2/local_maxima.py b/hands_on/local_maxima_part2/local_maxima.py index dec43ac..9f40cc5 100644 --- a/hands_on/local_maxima_part2/local_maxima.py +++ b/hands_on/local_maxima_part2/local_maxima.py @@ -1,11 +1,11 @@ def find_maxima(points): - """Find local maxima of x. + """Find local maxima of points. Input arguments: - x -- 1D list of real numbers + points -- 1D list of real numbers Output: - idx -- list of indices of the local maxima in x + indices -- list of indices of the local maxima in points """ indices = [] From cd7ba7e37794f16138cdecf99b25d0edc1c5c445 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 11:31:27 +0300 Subject: [PATCH 5/5] Fix plateau identification --- hands_on/local_maxima_part2/local_maxima.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/hands_on/local_maxima_part2/local_maxima.py b/hands_on/local_maxima_part2/local_maxima.py index 9f40cc5..e505faa 100644 --- a/hands_on/local_maxima_part2/local_maxima.py +++ b/hands_on/local_maxima_part2/local_maxima.py @@ -8,7 +8,8 @@ def find_maxima(points): indices -- list of indices of the local maxima in points """ indices = [] - + plateau_idx = [] + if not points: print("No input given") return [] @@ -20,6 +21,13 @@ def find_maxima(points): for i in range(1, len(points)-1): if points[i] > points[i+1] and points[i] > points[i-1]: indices.append(i) + plateau_idx = [] + if points[i] == points[i+1]: + plateau_idx.append(i) + if points[i] > points[i+1] and plateau_idx: + indices.append(plateau_idx[0]) + plateau_idx = [] + if points[-1] > points[-2]: indices.append(len(points)-1) return indices \ No newline at end of file