Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Explained solution in Clear category for Reverse Every Ascending by Selindian
def reverse_ascending(items):
# your code here
sorteditems = list() # Create empty list for sorted items
start = 0 # Set start to 0
for idx in range(1, len(items)): # Loop over our list of items
if items[idx-1] >= items[idx]: # If last item is >= current item
sorteditems.extend(items[start:idx][::-1]) # ... extend sorteditems with sorted [::-1] part between start (incl.) and current index (excl.)
start = idx # ... set start to current index.
sorteditems.extend(items[start:][::-1]) # After the loop: Add the remaining rest (sorted [::-1]) to sorteditems
return sorteditems # Return sorted list
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!")
March 9, 2022
Comments: