From 8ae15bf2a54fbe83f46756947cf7e366f1482277 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 23 Sep 2025 15:21:50 +0300 Subject: [PATCH 1/3] find local maxima --- hands_on/local_maxima/local_maxima.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/hands_on/local_maxima/local_maxima.py b/hands_on/local_maxima/local_maxima.py index cfd0a9e..37548e9 100644 --- a/hands_on/local_maxima/local_maxima.py +++ b/hands_on/local_maxima/local_maxima.py @@ -7,9 +7,13 @@ def find_maxima(x): Output: idx -- list of indices of the local maxima in x """ - return [] + idx = [] + for i in range(1, len(x)-1): + if (x[i] > x[i-1]) & (x[i] > x[i+1]): + idx.append(i) + return idx if __name__ == "__main__": - x = [1, 2, 3] + x = [1, 3, -2, 0, 2, 1] print(find_maxima(x)) -- 2.39.5 From 49a768595c10fdaff6d0860756f55eea6cce6a35 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 23 Sep 2025 16:10:10 +0300 Subject: [PATCH 2/3] add test functions with wrong results to see how it breaks --- hands_on/first/test_first.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/hands_on/first/test_first.py b/hands_on/first/test_first.py index 5ac7354..682eaa6 100644 --- a/hands_on/first/test_first.py +++ b/hands_on/first/test_first.py @@ -1,6 +1,14 @@ +from first import times_3 + def test_times_3_integer(): - pass + value = 5 + expected = 150 + result = times_3(value) + assert result == expected def test_times_3_string(): - pass + value = 'yupi' + expected = 'yupiyupiyupiyu' + result = times_3(value) + assert result == expected -- 2.39.5 From 17d41ce1e0d953c854a789f182bbc9bfc8805465 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 23 Sep 2025 16:41:31 +0300 Subject: [PATCH 3/3] fix the code to pass the first three tests --- hands_on/local_maxima_part2/local_maxima.py | 23 ++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/hands_on/local_maxima_part2/local_maxima.py b/hands_on/local_maxima_part2/local_maxima.py index db89ba3..b2af2b8 100644 --- a/hands_on/local_maxima_part2/local_maxima.py +++ b/hands_on/local_maxima_part2/local_maxima.py @@ -7,4 +7,25 @@ def find_maxima(x): Output: idx -- list of indices of the local maxima in x """ - return [] + idx = [] + # catch the case when input is an empty list + if x == []: + return idx + + # catch the case where a maximum is at the edge + if len(x) > 1: + if x[0] > x[1]: + idx.append(0) + if x[-1] > x[-2]: + idx.append(len(x)-1) + + # if we only have two values, we are done + if len(x) == 2: + return idx + + # other wise for everything not on the edge: + for i in range(1, len(x)-1): + if (x[i] > x[i-1]) & (x[i] > x[i+1]): + idx.append(i) + + return sorted(idx) -- 2.39.5