3 ms·
Oops, it doesn't reverse decreasing runs. Make that: import operator as op import itertools as it def mono_split(cmp, lst): start = le
by mrtranscendence 5y ago
Oops, it doesn't reverse decreasing runs. Make that:
import operator as op
import itertools as it
def mono_split(cmp, lst):
start = len(list(it.takewhile(lambda t: cmp(t[0], t[1]), zip(lst, lst[1:]))))
return lst[:start + 1], lst[start + 1:]
def get_mono_splits(lst):
while len(lst) >= 2:
if lst[0] == lst[1]:
yield [lst[0]]
lst = lst[1:]
else:
cmp = op.lt if lst[0] < lst[1] else op.gt
run, lst = mono_split(cmp, lst)
if cmp == op.gt:
run = list(reversed(run))
yield run
if len(lst) == 1:
yield lst