diff --git a/hands_on/local_maxima/local_maxima.py b/hands_on/local_maxima/local_maxima.py index 1575179..7307aa7 100644 --- a/hands_on/local_maxima/local_maxima.py +++ b/hands_on/local_maxima/local_maxima.py @@ -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 diff --git a/hands_on/local_maxima/test_local_maxima.py b/hands_on/local_maxima/test_local_maxima.py index 5428945..0ca2159 100644 --- a/hands_on/local_maxima/test_local_maxima.py +++ b/hands_on/local_maxima/test_local_maxima.py @@ -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 @@ -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