Testing Class Material
This commit is contained in:
commit
05b1f6cdd5
85 changed files with 102796 additions and 0 deletions
44
hands_on_solutions/local_maxima/local_maxima.py
Normal file
44
hands_on_solutions/local_maxima/local_maxima.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
def find_maxima(x):
|
||||
"""Find local maxima of x.
|
||||
|
||||
Example:
|
||||
>>> x = [1, 3, -2, 0, 2, 1]
|
||||
>>> find_maxima(x)
|
||||
[1, 4]
|
||||
|
||||
If in a local maximum several elements have the same value,
|
||||
return the left-most index.
|
||||
Example:
|
||||
>>> x = [1, 2, 2, 1]
|
||||
>>> find_maxima(x)
|
||||
[1]
|
||||
|
||||
Input arguments:
|
||||
x -- 1D list of real numbers
|
||||
|
||||
Output:
|
||||
idx -- list of indices of the local maxima in x
|
||||
"""
|
||||
|
||||
idx = []
|
||||
up = False
|
||||
down = False
|
||||
for i in range(len(x)):
|
||||
if i == 0 or x[i-1] < x[i]:
|
||||
up = True
|
||||
up_idx = i
|
||||
elif x[i-1] > x[i]:
|
||||
up = False
|
||||
|
||||
# if x[i-1] == x[i], no change
|
||||
|
||||
if i+1 == len(x) or x[i+1] < x[i]:
|
||||
down = True
|
||||
elif x[i+1] > x[i]:
|
||||
down = False
|
||||
|
||||
if up and down:
|
||||
idx.append(up_idx)
|
||||
|
||||
return idx
|
||||
|
36
hands_on_solutions/local_maxima/test_local_maxima.py
Normal file
36
hands_on_solutions/local_maxima/test_local_maxima.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
from local_maxima import find_maxima
|
||||
|
||||
|
||||
def test_find_maxima():
|
||||
values = [1, 3, -2, 0, 2, 1]
|
||||
expected = [1, 4]
|
||||
maxima = find_maxima(values)
|
||||
assert maxima == expected
|
||||
|
||||
|
||||
def test_find_maxima_edges():
|
||||
values = [4, 2, 1, 3, 1, 5]
|
||||
expected = [0, 3, 5]
|
||||
maxima = find_maxima(values)
|
||||
assert maxima == expected
|
||||
|
||||
|
||||
def test_find_maxima_empty():
|
||||
values = []
|
||||
expected = []
|
||||
maxima = find_maxima(values)
|
||||
assert maxima == expected
|
||||
|
||||
|
||||
def test_find_maxima_plateau():
|
||||
values = [1, 2, 2, 1]
|
||||
expected = [1]
|
||||
maxima = find_maxima(values)
|
||||
assert maxima == expected
|
||||
|
||||
|
||||
def test_find_maxima_not_a_plateau():
|
||||
values = [1, 2, 2, 3, 1]
|
||||
expected = [3]
|
||||
maxima = find_maxima(values)
|
||||
assert maxima == expected
|
Loading…
Add table
Add a link
Reference in a new issue