From bdab0707b625bce763b9730e4d8badaa927e1911 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 27 Aug 2024 16:10:46 +0300 Subject: [PATCH] add find_maxima and testing for find_maxima --- hands_on/local_maxima/local_maxima.py | 15 +++++++- hands_on/local_maxima/test_local_maxima.py | 41 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 hands_on/local_maxima/test_local_maxima.py diff --git a/hands_on/local_maxima/local_maxima.py b/hands_on/local_maxima/local_maxima.py index db89ba3..556e9b2 100644 --- a/hands_on/local_maxima/local_maxima.py +++ b/hands_on/local_maxima/local_maxima.py @@ -7,4 +7,17 @@ def find_maxima(x): Output: idx -- list of indices of the local maxima in x """ - return [] + maxima= [] + for i, val in enumerate(x): + if i == 0 and val > x[i+1]: + maxima.append(i) + elif i == len(x) -1 and val > x[len(x) -2]: + maxima.append(i) + else: + if val > x[i-1] and val > x[i+1]: + maxima.append(i) + return maxima + +if __name__ == "__main__": + idx = find_maxima([1,3,-2,0,2,1]) + print(idx) diff --git a/hands_on/local_maxima/test_local_maxima.py b/hands_on/local_maxima/test_local_maxima.py new file mode 100644 index 0000000..dd6f436 --- /dev/null +++ b/hands_on/local_maxima/test_local_maxima.py @@ -0,0 +1,41 @@ +from local_maxima import find_maxima + +def test_local_maxima_first(): + values = [1,3, -2, 0,2,1] + expected = [1, 4] + + result = find_maxima(values) + + assert result == expected + +def test_local_maxima_second(): + values = [4, 2, 1, 3, 1, 5] + expected = [0,3,5] + + result = find_maxima(values) + + assert result == expected + +def test_local_maxima_third(): + values = [] + expected = [] + + result = find_maxima(values) + + assert result == expected + +def test_local_maxima_fourth(): + values = [1,2,2,1] + expected = [] + + result = find_maxima(values) + + assert result == expected + +def test_local_maxima_fifth(): + values = [1,2,2,3,1] + expected = [3] + + result = find_maxima(values) + + assert result == expected \ No newline at end of file