add find_maxima and testing for find_maxima #3

Open
johanneska wants to merge 1 commit from johanneska/2024-heraklion-testing-debugging:find_maxima into main
2 changed files with 55 additions and 1 deletions

View file

@ -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)

View file

@ -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