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

find maxima for plateaus #4

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
27 changes: 21 additions & 6 deletions hands_on/local_maxima/local_maxima.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
def local_maxima(x):
"""Find local maxima of x.
import numpy as np
from typing import Union
from numpy.typing import NDArray
import sys

def find_maxima(numbers: Union[list, NDArray]):
"""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 []
max_idxs = []
for ni, num in enumerate(numbers[:-1]):
if ni == 0:
pass
elif (numbers[ni+1] <= num and numbers[ni-1] < num):
max_idxs.append(ni)

return max_idxs

if __name__ == '__main__':
numbers = np.array(sys.argv[1:], dtype=np.int_)
max_idxs = find_maxima(numbers)
print(max_idxs)


23 changes: 13 additions & 10 deletions hands_on/local_maxima/test_local_maxima.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
from local_maxima import local_maxima
from local_maxima import find_maxima


def test_local_maxima():
def test_find_maxima():
values = [1, 3, -2, 0, 2, 1]
expected = [1, 4]
maxima = local_maxima(values)
maxima = find_maxima(values)
assert maxima == expected


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


def test_local_maxima_edges():
def test_find_maxima_edges():
values = [4, 2, 1, 3, 1, 5]
expected = [0, 3, 5]
maxima = local_maxima(values)
expected = [3]
maxima = find_maxima(values)
assert maxima == expected


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