ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
from collections import Counter
print(''.join(c * n for c, n in Counter('tree').most_common()))
  1. A. eetr
  2. B. tree
  3. C. rtee
  4. D. eert

Correct answer: A. eetr

most_common lists e first because it occurs twice, and then t and r in first-seen order, each with one copy. Joining c times n gives eetr.

More Python Programming questions

Exams that ask this topic