Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Reverse Every Ascending (with explaining comments) solution in Clear category for Reverse Every Ascending by SpeakingCucumber
def reverse_ascending(items):
# Create empty 'result' list
result = list()
# If 'items' list is empty (its length = 0) we return 'result' list (which is empty now but it definitely fits now)
if len(items) == 0:
return result
# If 'items' list contains 1 value, we assign it to 'result' list (as there is nothing need to be reversed)
elif len(items) == 1:
result = items
return result
# In all other cases we perform the code
else:
# Here we create 'value_for_comparison' and assign it first value from the 'items' list (as we need to compare with something during iteration)
# 'temp_list' will contain ascending items that will be reversed on purpose
value_for_comparison = items[0]
temp_list = list()
temp_list.append(value_for_comparison)
# We will start iteration starting from second element of 'items' list (as we already assigned 1st element during creation of 'value' for comparison
for index, every_item in enumerate(items[1:]):
# Check if every_item is not a final element from our partial 'items' list.
# We subtract 2 because we start iteration from second element of the list
if index != len(items) - 2:
# We execute this part of the code if value if bigger than value for comparison
if every_item > value_for_comparison:
temp_list.append(every_item)
value_for_comparison = every_item
# We execute this part of the code if value is lesser than value for comparison
# In this way we check that ascending streak ends.
else:
temp_list.reverse()
result.extend(temp_list)
value_for_comparison = every_item
temp_list = list()
temp_list.append(value_for_comparison)
# We execute next part of the code if every_item is final element from our partial 'items' list
# We need this to not 'forget' to add last element to our calculations
else:
# We need to append last element to the temp_list
temp_list.append(every_item)
# If the last element is bigger than value_for_comparison: we reverse list and extend the 'result' list
if every_item > value_for_comparison:
temp_list.reverse()
result.extend(temp_list)
# If the last element is not bigger than value_for_comparison: we just extend the 'result' list
else:
result.extend(temp_list)
return result
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!")
Nov. 21, 2021
Comments: