diff --git a/hands_on/local_maxima/local_maxima.py b/hands_on/local_maxima/local_maxima.py index 1575179..204bdf8 100644 --- a/hands_on/local_maxima/local_maxima.py +++ b/hands_on/local_maxima/local_maxima.py @@ -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) + + diff --git a/hands_on/local_maxima/test_local_maxima.py b/hands_on/local_maxima/test_local_maxima.py index 5428945..8f7a46b 100644 --- a/hands_on/local_maxima/test_local_maxima.py +++ b/hands_on/local_maxima/test_local_maxima.py @@ -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