ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
from collections import Counter
c = Counter(['x', 'y', 'x', 'z', 'y', 'x'])
print(c.most_common(), sorted(c.items(), key=lambda kv: kv[1]))
  1. A. [('x', 3), ('z', 1), ('y', 2)] [('z', 1), ('y', 2), ('x', 3)]
  2. B. [('x', 3), ('y', 2), ('z', 1)] [('x', 3), ('y', 2), ('z', 1)]
  3. C. [('x', 3), ('y', 2), ('z', 1)] [('y', 2), ('z', 1), ('x', 3)]
  4. D. [('x', 3), ('y', 2), ('z', 1)] [('z', 1), ('y', 2), ('x', 3)]

Correct answer: D. [('x', 3), ('y', 2), ('z', 1)] [('z', 1), ('y', 2), ('x', 3)]

most_common sorts by count in descending order, giving x, y, z. sorted by count ascending puts z (1), then y (2), then x (3), since sorting by count keeps the insertion order only for ties.

More Python Programming questions

Exams that ask this topic