This is my input
mylist = [2, 7, 8, 11, 7, 9, 10, 15, 22, 30, 32]
- from 2 to 11, it's increasing, so we need to grab the min max
[2, 11]
- from 7 to 10 it's increasing, but we need to ignore it because the range (7 to 10) is inluded in the first grabed list
- from 15 to 32, it's increasing, so we need to grab the min max:
[15, 32]
the final list should be : [[2, 11], [15, 32]]
I tried something like below but it does not :
final = []
mi = mylist[0]
ma = mylist[1]for i, j in zip(mylist, mylist[1:]):if i < j:ma = jelif i > j:mi = icontinueelif mi == ma:continuefinal.append([mi, ma])
update:
Let me add more scenarios :
- for
[5, 8, 10, 3, 4, 5, 7]
we should get[[5,10]]
because even if[3, 7]
is overlapping with[5, 10]
, the start of[3, 7]
is behind[5, 10]
- for
[5, 8, 10, 8, 9, 12]
we should get[[5,12]]
which is[5,10] ∪ [8, 12]
because[8, 12]
is overlapping with[5, 10]
from the right (ahead of it) - for
[1, 3, 5, 4, 3, 2, 1]
we should get[[1, 5]]
because from 4 to 1 it is a decreasing sequence, so we have to ignore it
I'm not necessarily looking for a Python code. I just need the algorithm or the correct way to approach this problem.