← Quiz: Counting Sort and Custom Sorting: all 13 questions
Quiz: Counting Sort and Custom Sorting · Question 8 of 13
What does this print? (code)
What does this print?
from collections import Counter
c = Counter("banana")
print(sorted(c, key=lambda ch: (-c[ch], ch)))
- A.['b', 'a', 'n']
- B.['a', 'b', 'n']
- C.['a', 'n', 'b']
- D.['n', 'a', 'b']
Answer & explanation
Correct answer: ['a', 'n', 'b']
a appears 3 times, n twice and b once, so the order is most frequent first.
Can you get the rest right without peeking?
Take the timed Quiz: Counting Sort and Custom Sorting quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →