Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Reverse Every Ascending by defensor
def reverse_ascending(items):
# your code here
try:
# identify the index numbers of where to separate the sublists
sep = [i for i, num in enumerate(items) if items[i-1] >= items[i]]
# divide "items" into the ascending sublists using the separators
first = [items[:sep[0]]]
mid = [items[sep[i]:sep[i+1]] for i in range(len(sep)-1)]
last = [items[sep[-1]:]]
# and put them back together
new_list = first + mid + last
# sort and merge the sublists
return sum([sorted(i, reverse=True) for i in new_list], [])
except:
# if the list has nothing to sort nor contains nothing return items
return items
if __name__ == '__main__':
print("Example:")
print(reverse_ascending([5, 7, 10, 4, 2, 7, 8, 1, 3]))
# 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!")
July 6, 2021