Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> array = [1,4,2,-2,-9,10,2,12,2,-4,-4,-4,-4,2,6,7]
- >>> index = 0
- >>> peak = array[0]
- >>> output = []
- >>>
- >>> for x in range(1, len(array)):
- ... if peak < 0 and array[x] < peak:
- ... peak = array[x]
- ... index = x
- ... if peak >= 0 and array[x] > peak:
- ... peak = array[x]
- ... index = x
- ... else:
- ... output.append((index, peak))
- ... peak = array[x]
- ... index = x
- ...
- >>> output
- [(1, 4), (2, 2), (4, -9), (4, -9), (5, 10), (7, 12), (8, 2), (9, -4), (10, -4), (11, -4), (12, -4)]
- >>>
- >>>
- >>> # Simplifies to:
- >>>
- >>> array = [1,4,2,-2,-9,10,2,12,2,-4,-4,-4,-4,2,6,7]
- >>> index = 0
- >>> peak = array[0]
- >>> output = []
- >>>
- >>> for x in range(1, len(array)):
- ... if peak < 0 or peak >= array[x]:
- ... output.append((index, peak))
- ... peak = array[x]
- ... index = x
- ...
- >>> output
- [(1, 4), (2, 2), (3, -2), (4, -9), (5, 10), (7, 12), (8, 2), (9, -4), (10, -4), (11, -4), (12, -4)]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement