Advertisement
codegoblin

Untitled

Mar 12th, 2020
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PyCon 0.98 KB | None | 0 0
  1. >>> array = [1,4,2,-2,-9,10,2,12,2,-4,-4,-4,-4,2,6,7]
  2. >>> index = 0
  3. >>> peak = array[0]
  4. >>> output = []
  5. >>>
  6. >>> for x in range(1, len(array)):
  7. ...     if peak < 0 and array[x] < peak:
  8. ...         peak = array[x]
  9. ...         index = x
  10. ...     if peak >= 0  and array[x] > peak:
  11. ...         peak = array[x]
  12. ...         index = x
  13. ...     else:
  14. ...         output.append((index, peak))
  15. ...         peak = array[x]
  16. ...         index = x
  17. ...        
  18. >>> output
  19. [(1, 4), (2, 2), (4, -9), (4, -9), (5, 10), (7, 12), (8, 2), (9, -4), (10, -4), (11, -4), (12, -4)]
  20. >>>
  21. >>>
  22. >>> # Simplifies to:
  23. >>>
  24. >>> array = [1,4,2,-2,-9,10,2,12,2,-4,-4,-4,-4,2,6,7]
  25. >>> index = 0
  26. >>> peak = array[0]
  27. >>> output = []
  28. >>>
  29. >>> for x in range(1, len(array)):
  30. ...     if peak < 0 or peak >= array[x]:
  31. ...         output.append((index, peak))
  32. ...     peak = array[x]
  33. ...     index = x
  34. ...
  35. >>> output
  36. [(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