(no title)
efdb | 4 years ago
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]
No comments yet.