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

Create find_maxima function #3

Open
wants to merge 5 commits into
base: main
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
33 changes: 28 additions & 5 deletions hands_on/local_maxima_part2/local_maxima.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
def find_maxima(x):
"""Find local maxima of x.
def find_maxima(points):
"""Find local maxima of points.

Input arguments:
x -- 1D list of real numbers
points -- 1D list of real numbers

Output:
idx -- list of indices of the local maxima in x
indices -- list of indices of the local maxima in points
"""
return []
indices = []
plateau_idx = []

if not points:
print("No input given")
return []
elif len(points) == 1:
return [0]

if points[0] > points[1]:
indices.append(0)
for i in range(1, len(points)-1):
if points[i] > points[i+1] and points[i] > points[i-1]:
indices.append(i)
plateau_idx = []
if points[i] == points[i+1]:
plateau_idx.append(i)
if points[i] > points[i+1] and plateau_idx:
indices.append(plateau_idx[0])
plateau_idx = []

if points[-1] > points[-2]:
indices.append(len(points)-1)
return indices
10 changes: 8 additions & 2 deletions hands_on/local_maxima_part2/test_local_maxima.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ def test_find_maxima_empty():


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


def test_find_maxima_not_a_plateau():
raise Exception('not yet implemented')
values = [1, 2, 2, 3, 1]
expected = [3]
maxima = find_maxima(values)
assert maxima == expected
17 changes: 17 additions & 0 deletions local_maxima.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def find_maxima(points):
indices = []

if not points:
print("No input given")
return []
elif len(points) == 1:
return [0]

if points[0] > points[1]:
indices.append(0)
for i in range(1, len(points)-1):
if points[i] > points[i+1] and points[i] > points[i-1]:
indices.append(i)
if points[-1] > points[-2]:
indices.append(len(points)-1)
return indices