Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solution #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions hands_on/local_maxima/local_maxima.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
def local_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
"""
return []

ret = []

pos = 0
incr = False


if x[0] >= x[1]:
ret.append(0)

while pos < len(x)-2:

while x[pos] < x[pos+1]:
# print("comparing {} and {}".format(vet[pos], vet[pos+1]))
pos += 1
incr = True

if incr:
ret.append(pos)
incr = False

while x[pos] == x[pos+1]:
pos += 1
#print("appending pos {} to ret".format(pos))

if x[pos] > x[pos+1]:
pos += 1

if x[-2] < x[-1]:
#print("LAST COMPARISON: {} - {}".format(vet[-2], vet[-1]))
ret.append(len(x)-1)

return ret
7 changes: 5 additions & 2 deletions hands_on/local_maxima/test_local_maxima.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_local_maxima():

def test_local_maxima_negative():
values = [-1, -1, 0, -1]
expected = [2]
expected = [0, 2]
maxima = local_maxima(values)
assert maxima == expected

Expand All @@ -23,5 +23,8 @@ def test_local_maxima_edges():


def test_local_maxima_plateau():
raise Exception('not yet implemented')
values = [1, 2, 2, 1]
expected = [1]
maxima = local_maxima(values)
assert maxima == expected