- #1

- 2,168

- 193

Python:

```
array_values = [[0], [0,0], [1,2], [3,1,2], [1,2,3], [4,6,2,1], [8,9,0,2,1]]
def peakfinder(xarray):
if len(xarray) == 0:
print("You entered an empty error !")
raise ValueError
if len(xarray) == 1:
return xarray[0]
if len(xarray) == 2:
return max(xarray)
mid_element_index = len(xarray) // 2
mid_element = xarray[mid_element_index]
if mid_element >= xarray[mid_element_index + 1] and mid_element >= xarray[mid_element_index - 1] :
return mid_element
elif mid_element < xarray[mid_element_index + 1]:
return xarray[mid_element_index + 1:]
else:
return xarray[ :mid_element_index]
for xarray in array_values:
while type(xarray) != int:
xarray = peakfinder(xarray)
print("The peak value is", xarray)
```

Is this a "good" 1D peak finder code ? Also I have another problem. In the peak finder case do we have to find all the peak values or just one peak value ?

Also in my code I didnt like `while type(array) != int:`, but I couldn't come up with something clever :(

Thanks

Last edited: