2025-plovdiv-testing-debugging/hands_on/local_maxima/local_maxima.py
2025-09-23 16:22:34 +03:00

24 lines
558 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
"""
idx = []
for i in range(len(x)):
if i == 0 and x[i] > x[i+1]:
idx.append(i)
elif i != len(x)-1 and x[i] > x[i-1] and x[i] > x[i+1]:
idx.append(i)
elif i== len(x)-1 and x[i] > x[i-1]:
idx.append(i)
return idx
if __name__ == "__main__":
x = [1, 2, 3]
# x = [1,2,2,3,1]
print(find_maxima(x))