Skip to content
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
17 changes: 15 additions & 2 deletions hands_on/local_maxima_part2/local_maxima.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
def find_maxima(x):
import numpy as np

def find_maxima(x: np.array):
"""Find local maxima of x.

Input arguments:
Expand All @@ -7,4 +9,15 @@ def find_maxima(x):
Output:
idx -- list of indices of the local maxima in x
"""
return []
idx = []
for i in range(0, len(x)):
pred = True
if i != 0:
pred = pred and (x[i-1] <= x[i])
if i != len(x)-1:
pred = pred and (x[i+1] < x[i])
if pred:
idx.append(i)
return idx


15 changes: 13 additions & 2 deletions hands_on/local_maxima_part2/test_local_maxima.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from local_maxima import find_maxima
import numpy as np


def test_find_maxima():
Expand All @@ -23,8 +24,18 @@ def test_find_maxima_empty():


def test_find_maxima_plateau():
raise Exception('not yet implemented')
values = [1, 2, 2, 1]
expected_v1 = [1]
expected_v2 = [2]
expected_v3 = [1, 2]

assert (np.all(find_maxima(values) == expected_v1)
or np.all(find_maxima(values) == expected_v2)
or np.all(find_maxima(values) == expected_v3))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, two comments:

  1. find_maxima returns a list, so there's no need to use np.all: two list are equal if all their elements are equal
  2. in a real-life situation, you would make a choice between one of these possible options and test that one specifically



def test_find_maxima_not_a_plateau():
raise Exception('not yet implemented')
values = np.array([1, 2, 2, 3, 1])
expected = np.array([3])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking, the function takes and returns a list. This works because the array object implements the same functionality of a list that you need for this functions, but in other situation it could fail


assert np.all(find_maxima(values) == expected)