33 lines
734 B
Python
33 lines
734 B
Python
def find_maxima(x):
|
|
"""Find local maxima of x.
|
|
|
|
Input arguments:
|
|
x -- 1D list of real numbers
|
|
|
|
Output:
|
|
idx -- list of indices of the local maxima in x
|
|
"""
|
|
maxima = []
|
|
for idx in range(len(x)):
|
|
if idx == 0:
|
|
if x[idx] >= x[idx+1]:
|
|
maxima.append(idx)
|
|
elif idx == len(x)-1:
|
|
if x[idx] >= x[idx-1]:
|
|
maxima.append(idx)
|
|
else:
|
|
if x[idx] >= x[idx+1] and x[idx] >= x[idx-1]:
|
|
maxima.append(idx)
|
|
return maxima
|
|
|
|
|
|
if __name__ == "__main__":
|
|
values = [
|
|
[1, 3, -2, 0, 2, 1],
|
|
[4,2,1,3,1,5],
|
|
[],
|
|
[1,2,2,1],
|
|
[1,2,2,3,1],
|
|
]
|
|
for value in values:
|
|
print(find_maxima(value))
|