From 7fddd01e4eb164a0fcb235f46365540710603ad9 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 09:55:32 +0300 Subject: [PATCH 1/3] implemented local maxima func --- hands_on/local_maxima/local_maxima.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/hands_on/local_maxima/local_maxima.py b/hands_on/local_maxima/local_maxima.py index db89ba3..acff58e 100644 --- a/hands_on/local_maxima/local_maxima.py +++ b/hands_on/local_maxima/local_maxima.py @@ -7,4 +7,15 @@ def find_maxima(x): Output: idx -- list of indices of the local maxima in x """ - return [] + local_maxima = [] + for i in range(1, len(x)-1): + first_value = x[i-1] + second_value = x[i] + third_value = x[i+1] + if first_value < second_value and second_value > third_value: + local_maxima.append(i) + return local_maxima + +test_list = [1,3,-2,0,2,1] + +print(find_maxima(test_list)) From 33261bfcfd708dca0a704d138be07c40252e54da Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 11:09:39 +0300 Subject: [PATCH 2/3] do the local maxima part 2 --- hands_on/first/test_first.py | 8 +++++ hands_on/local_maxima/local_maxima.py | 13 ++++++++ hands_on/local_maxima_part2/local_maxima.py | 31 ++++++++++++++++++- .../local_maxima_part2/test_local_maxima.py | 11 +++++-- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/hands_on/first/test_first.py b/hands_on/first/test_first.py index 64ea1a8..bb95ad2 100644 --- a/hands_on/first/test_first.py +++ b/hands_on/first/test_first.py @@ -17,3 +17,11 @@ def test_times_3_string(): result = times_3(value) assert result == expected + +def test_times_3_list(): + value = [1] + expected = [1,1,1] + + result = times_3(value) + + assert result == expected \ No newline at end of file diff --git a/hands_on/local_maxima/local_maxima.py b/hands_on/local_maxima/local_maxima.py index acff58e..ec03e08 100644 --- a/hands_on/local_maxima/local_maxima.py +++ b/hands_on/local_maxima/local_maxima.py @@ -14,8 +14,21 @@ def find_maxima(x): third_value = x[i+1] if first_value < second_value and second_value > third_value: local_maxima.append(i) + # if i==1 & first_value > second_value: + # local_maxima.append(i) return local_maxima test_list = [1,3,-2,0,2,1] +print(find_maxima(test_list)) + +test_list = [4,2,1,3,1,5] +print(find_maxima(test_list)) +test_list = [] print(find_maxima(test_list)) + +test_list = [1,2,2,1] +print(find_maxima(test_list)) + +test_list = [1,2,2,3,1] +print(find_maxima(test_list)) \ No newline at end of file diff --git a/hands_on/local_maxima_part2/local_maxima.py b/hands_on/local_maxima_part2/local_maxima.py index db89ba3..65c59b6 100644 --- a/hands_on/local_maxima_part2/local_maxima.py +++ b/hands_on/local_maxima_part2/local_maxima.py @@ -1,3 +1,5 @@ +import numpy as np + def find_maxima(x): """Find local maxima of x. @@ -7,4 +9,31 @@ def find_maxima(x): Output: idx -- list of indices of the local maxima in x """ - return [] + local_maxima = [] + print('start') + if len(x)>0: + diff_values = np.diff(x) + if np.any(diff_values==0): + index_of_plateau = np.argwhere(diff_values==0) + if x[int(index_of_plateau)] == np.max(x): + local_maxima.append(int(index_of_plateau)) + return local_maxima + for i in range(0, len(x)): + if i==0: + first_value = x[i] + second_value = x[i+1] + if first_value > second_value: + local_maxima.append(i) + elif i==len(x)-1: + first_value = x[i-1] + second_value = x[i] + if first_value < second_value: + local_maxima.append(i) + else: + first_value = x[i-1] + second_value = x[i] + third_value = x[i+1] + if first_value < second_value and second_value > third_value: + local_maxima.append(i) + return local_maxima + diff --git a/hands_on/local_maxima_part2/test_local_maxima.py b/hands_on/local_maxima_part2/test_local_maxima.py index 316442d..996212d 100644 --- a/hands_on/local_maxima_part2/test_local_maxima.py +++ b/hands_on/local_maxima_part2/test_local_maxima.py @@ -23,8 +23,15 @@ def test_find_maxima_empty(): def test_find_maxima_plateau(): - raise Exception('not yet implemented') + values = [1,2,2,1] + expected_1 = [1] + expected_2 = [2] + maxima = find_maxima(values) + assert maxima == expected_1 or maxima == expected_2 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 138ae07868e763eca16a2a12c7b085dd1a9c66a3 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 11:30:49 +0300 Subject: [PATCH 3/3] do the local maxima part 2 --- hands_on/first/plus.py | 3 +++ hands_on/first/test_plus.py | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 hands_on/first/plus.py create mode 100644 hands_on/first/test_plus.py diff --git a/hands_on/first/plus.py b/hands_on/first/plus.py new file mode 100644 index 0000000..9b879a0 --- /dev/null +++ b/hands_on/first/plus.py @@ -0,0 +1,3 @@ + +def plus(x,y): + return x + y \ No newline at end of file diff --git a/hands_on/first/test_plus.py b/hands_on/first/test_plus.py new file mode 100644 index 0000000..a1849fe --- /dev/null +++ b/hands_on/first/test_plus.py @@ -0,0 +1,18 @@ +from plus import plus +import numpy as np +from math import isclose + +def test_plus_123(): + x = 1 + y = 2 + assert plus(x,y) == 3 + +def test_plus_1dec1_2dec2_3dec3(): + x = 1.1 + y = 2.2 + assert isclose(plus(x,y), 3.3) + +def test_plus_with_numpy(): + x = np.array([[1,1],[1,1]]) + y = np.array([[2,2],[2,2]]) + np.testing.assert_equal(plus(x,y), np.array([[3,3],[3,1]])) \ No newline at end of file