-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpick_peaks.py
More file actions
31 lines (25 loc) · 937 Bytes
/
pick_peaks.py
File metadata and controls
31 lines (25 loc) · 937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Link to kata: https://www.codewars.com/kata/5279f6fe5ab7f447890006a7
def pick_peaks(arr):
pos = []
peaks = []
if len(arr) == 0:
return { "pos": pos, "peaks": peaks }
plateau = [arr[1], arr[-1]]
for i in range(1, len(arr)-1):
if arr[i-1] < arr[i] and arr[i] >= arr[i+1]:
if arr[i] == arr[i+1]:
if arr[i] != plateau[1]:
can_add = True
for j in range(i+1, len(arr)):
if arr[i] > arr[j]:
break
if arr[i] < arr[j]:
can_add = False
break
if can_add:
pos.append(i)
peaks.append(arr[i])
else:
pos.append(i)
peaks.append(arr[i])
return { "pos": pos, "peaks": peaks }