Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
2 lines proc, weird solution in Creative category for Reverse Every Ascending by CDG.Axel
def reverse_ascending(itm, red=__import__('functools').reduce):
x = red(lambda r, e: r if itm[e] > itm[e-1] else [r[0] + itm[r[1]:e][::-1], e], range(1, len(itm)), ([], 0))
return x[0] + itm[x[1]:][::-1]
"""
# old solution with more strings
def reverse_ascending(items):
st, res = 0, []
for idx in range(len(items) - 1):
if items[idx + 1] <= items[idx]:
st, res = idx+1, res + items[st:idx + 1][::-1]
return res + items[st:][::-1]
"""
if __name__ == '__main__':
print("Example:")
print(reverse_ascending([1, 2, 3, 4, 5]))
# These "asserts" are used for self-checking and not for an auto-testing
assert list(reverse_ascending([1, 2, 3, 4, 5])) == [5, 4, 3, 2, 1]
assert list(reverse_ascending([5, 7, 10, 4, 2, 7, 8, 1, 3])) == [10, 7, 5, 4, 8, 7, 2, 3, 1]
assert list(reverse_ascending([5, 4, 3, 2, 1])) == [5, 4, 3, 2, 1]
assert list(reverse_ascending([])) == []
assert list(reverse_ascending([1])) == [1]
assert list(reverse_ascending([1, 1])) == [1, 1]
assert list(reverse_ascending([1, 1, 2])) == [1, 2, 1]
print("Coding complete? Click 'Check' to earn cool rewards!")
Dec. 5, 2021
Comments: