From 47f74329bf98c0736df6559f28451bd953d671ab Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 23 Sep 2025 16:22:34 +0300 Subject: [PATCH 1/2] local maxima prototype --- hands_on/first/test_first.py | 9 ++++++++- hands_on/local_maxima/local_maxima.py | 11 ++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/hands_on/first/test_first.py b/hands_on/first/test_first.py index 5ac7354..db95069 100644 --- a/hands_on/first/test_first.py +++ b/hands_on/first/test_first.py @@ -1,5 +1,12 @@ +from first import times_3 + def test_times_3_integer(): - pass + value = [2] + expected = [1,1,1] + + result = times_3(value) + + assert result == expected def test_times_3_string(): diff --git a/hands_on/local_maxima/local_maxima.py b/hands_on/local_maxima/local_maxima.py index cfd0a9e..650e20d 100644 --- a/hands_on/local_maxima/local_maxima.py +++ b/hands_on/local_maxima/local_maxima.py @@ -7,9 +7,18 @@ def find_maxima(x): Output: idx -- list of indices of the local maxima in x """ - return [] + idx = [] + for i in range(len(x)): + if i == 0 and x[i] > x[i+1]: + idx.append(i) + elif i != len(x)-1 and x[i] > x[i-1] and x[i] > x[i+1]: + idx.append(i) + elif i== len(x)-1 and x[i] > x[i-1]: + idx.append(i) + return idx if __name__ == "__main__": x = [1, 2, 3] + # x = [1,2,2,3,1] print(find_maxima(x)) -- 2.39.5 From 354d7cf9f19397d45455c1e144ff8bfab814c793 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 23 Sep 2025 16:31:03 +0300 Subject: [PATCH 2/2] local maxima tests --- hands_on/local_maxima_part2/local_maxima.py | 10 +++++++++- hands_on/local_maxima_part2/test_local_maxima.py | 16 +++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/hands_on/local_maxima_part2/local_maxima.py b/hands_on/local_maxima_part2/local_maxima.py index db89ba3..7e837f4 100644 --- a/hands_on/local_maxima_part2/local_maxima.py +++ b/hands_on/local_maxima_part2/local_maxima.py @@ -7,4 +7,12 @@ def find_maxima(x): Output: idx -- list of indices of the local maxima in x """ - return [] + idx = [] + for i in range(len(x)): + if i == 0 and x[i] > x[i+1]: + idx.append(i) + elif i != len(x)-1 and x[i] > x[i-1] and x[i] > x[i+1]: + idx.append(i) + elif i== len(x)-1 and x[i] > x[i-1]: + idx.append(i) + return idx \ No newline at end of file diff --git a/hands_on/local_maxima_part2/test_local_maxima.py b/hands_on/local_maxima_part2/test_local_maxima.py index 316442d..e1629a4 100644 --- a/hands_on/local_maxima_part2/test_local_maxima.py +++ b/hands_on/local_maxima_part2/test_local_maxima.py @@ -16,15 +16,21 @@ def test_find_maxima_edges(): def test_find_maxima_empty(): + values = [1,2,2,1] + expected = [1] + maxima = find_maxima(values) + assert maxima == expected + + +def test_find_maxima_plateau(): values = [] expected = [] maxima = find_maxima(values) assert maxima == expected -def test_find_maxima_plateau(): - raise Exception('not yet implemented') - - def test_find_maxima_not_a_plateau(): - raise Exception('not yet implemented') + values = [] + expected = [] + maxima = find_maxima(values) + assert maxima == expected -- 2.39.5