Challenge - 5 Problems
Sorting and Reversing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Output of sorting and reversing a list
What is the output of this Python code?
Python
numbers = [5, 3, 8, 1, 2] numbers.sort() numbers.reverse() print(numbers)
Attempts:
2 left
๐ก Hint
Remember that sort() arranges the list in ascending order, then reverse() flips it.
โ Incorrect
The list is first sorted to [1, 2, 3, 5, 8], then reversed to [8, 5, 3, 2, 1].
โ Predict Output
intermediate2:00remaining
Output of sorted() vs list.sort()
What is the output of this code snippet?
Python
fruits = ['banana', 'apple', 'cherry'] sorted_fruits = sorted(fruits) print(fruits) print(sorted_fruits)
Attempts:
2 left
๐ก Hint
sorted() returns a new sorted list, list.sort() changes the list itself.
โ Incorrect
sorted() returns a new sorted list without changing the original list.
๐ง Conceptual
advanced2:00remaining
Effect of reverse parameter in sort()
What will be the output of this code?
Python
nums = [4, 1, 7, 3] nums.sort(reverse=True) print(nums)
Attempts:
2 left
๐ก Hint
The reverse=True parameter sorts the list in descending order.
โ Incorrect
sort(reverse=True) sorts the list from highest to lowest.
โ Predict Output
advanced2:00remaining
Output after multiple reverses
What is the output of this code?
Python
letters = ['a', 'b', 'c', 'd'] letters.reverse() letters.reverse() print(letters)
Attempts:
2 left
๐ก Hint
Reversing twice returns the list to original order.
โ Incorrect
Two reverses cancel each other out, so the list is back to original.
โ Predict Output
expert3:00remaining
Output of sorting with a custom key and reversing
What is the output of this code?
Python
words = ['apple', 'banana', 'pear', 'kiwi'] words.sort(key=len) words.reverse() print(words)
Attempts:
2 left
๐ก Hint
The list is sorted by word length ascending, then reversed to descending length.
โ Incorrect
sort(key=len) sorts by length ascending: ['kiwi', 'pear', 'apple', 'banana'], then reverse flips it.