Skip to content

Instantly share code, notes, and snippets.

@babo
Created Aug 13, 2021
Embed
What would you like to do?
Find the runs of increasing and decreasing values in a list, and produce a sequence of the runs, but reverse the decreasing runs, so that they are also increasing.
def ud(numbers):
rtv = list()
c = list()
for n in numbers:
assert isinstance(n, int)
if len(c) < 2 or c[0] == c[-1] or c[-1] == n:
pass
elif c[0] < c[-1] and c[-1] > n:
rtv.append(c)
c = list()
elif c[0] > c[-1] and c[-1] < n:
c.reverse()
rtv.append(c)
c = list()
c.append(n)
else:
if len(c) > 1 and c[0] > c[-1]:
c.reverse()
rtv.append(c)
print(rtv)
return rtv
def test_ud(f):
assert f([]) == [[]]
assert f([3, 2, 1]) == [[1, 2, 3]]
assert f([1, 2, 3, 2, 1, 4, 5, 6, 7]) == [[1, 2, 3], [1, 2], [4, 5, 6, 7]]
assert f([1, 2, 3, 3, 2, 2, 1, 4, 5, 6, 7]) == [[1, 2, 3, 3], [1, 2, 2], [4, 5, 6, 7]]
if __name__ == '__main__':
test_ud(ud)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment