Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created November 9, 2021 21:14
Show Gist options
  • Select an option

  • Save les-peters/d65292d7d5d3a7112b6df08e5533af81 to your computer and use it in GitHub Desktop.

Select an option

Save les-peters/d65292d7d5d3a7112b6df08e5533af81 to your computer and use it in GitHub Desktop.
Local Peaks
question = """
Given an array of integers, return the index of each local peak in the array.
A “peak” element is an element that is greater than its neighbors.
$ localPeaks([1,2,3,1])
$ [2]
$ localPeaks([1,3,2,3,5,6,4])
$ [1, 5]
"""
def localPeaks(a):
peaks = []
for i in range(1, len(a) - 1):
if a[i] > a[i-1] and a[i] > a[i+1]:
peaks.append(i)
return peaks
print(localPeaks([1,2,3,1]))
print(localPeaks([1,3,2,3,5,6,4]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment