4 ms·
self documenting code ;-) def descending(lst): if len(lst) < 2: return False first, second = lst[:2] if first > second: return Fa
by efdb 5y ago
self documenting code ;-)
def descending(lst):
if len(lst) < 2:
return False
first, second = lst[:2]
if first > second:
return False
if first < second:
return True
descending(lst[1:])
def monotonic(lst):
if len(lst) < 2:
return lst
result = []
status = 0
temp = [lst[0]]
for v in lst[1:]:
if v >= temp[-1] and status >= 0:
if v > temp[-1]:
status = 1
temp.append(v)
continue
if v <= temp[-1] and status <= 0:
if v < temp[-1]:
status = -1
temp.append (v)
continue
result.append(temp)
temp = [v]
status = 0
result.append(temp)
return [list(reversed(x)) if descending(x) else x for x in result]