fix findmax for three test cases #1 fix1 #14
3 changed files with 38 additions and 5 deletions
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue