0
0
Pythonprogramming~20 mins

reversed() function in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Reversed Function Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of reversed() on a string
What is the output of this Python code?
Python
s = 'hello'
result = ''.join(reversed(s))
print(result)
AError: reversed() cannot be used on strings
Bolleh
C['o', 'l', 'l', 'e', 'h']
Dhello
Attempts:
2 left
๐Ÿ’ก Hint
reversed() returns an iterator that goes through the input backwards.
โ“ Predict Output
intermediate
2:00remaining
Using reversed() on a list
What is the output of this code snippet?
Python
lst = [1, 2, 3, 4]
rev_lst = list(reversed(lst))
print(rev_lst)
A[1, 2, 3, 4]
B[1, 4, 2, 3]
C[4, 3, 2, 1]
DError: reversed() only works on strings
Attempts:
2 left
๐Ÿ’ก Hint
reversed() works on any sequence, including lists.
โ“ Predict Output
advanced
2:00remaining
Output of reversed() on a dictionary keys view
What happens when you run this code?
Python
d = {'a': 1, 'b': 2, 'c': 3}
print(list(reversed(d.keys())))
ATypeError: 'dict_keys' object is not reversible
B['a', 'b', 'c']
C['c', 'b', 'a']
D['a', 'c', 'b']
Attempts:
2 left
๐Ÿ’ก Hint
Check if dict_keys supports reversed() directly.
โ“ Predict Output
advanced
2:00remaining
Reversing a range object
What is the output of this code?
Python
r = range(1, 5)
print(list(reversed(r)))
A[4, 3, 2, 1]
B[1, 2, 3, 4]
CTypeError: 'range' object is not reversible
D[5, 4, 3, 2]
Attempts:
2 left
๐Ÿ’ก Hint
range objects support reversed() because they are sequences.
๐Ÿง  Conceptual
expert
3:00remaining
Behavior of reversed() on custom iterable
Consider this class: class CountDown: def __init__(self, start): self.start = start def __iter__(self): n = self.start while n > 0: yield n n -= 1 What happens if you call reversed(CountDown(3))?
A[] (empty list)
B[3, 2, 1]
C[1, 2, 3]
DTypeError: object is not reversible
Attempts:
2 left
๐Ÿ’ก Hint
reversed() requires __reversed__ or __len__ and __getitem__ methods.