🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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)))
  1. A.['b', 'a', 'n']
  2. B.['a', 'b', 'n']
  3. C.['a', 'n', 'b']
  4. 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 →