ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
import heapq
from collections import Counter
words = 'a b c a b a d d d d'.split()
c = Counter(words)
print(heapq.nlargest(2, c.items(), key=lambda kv: kv[1]), c.most_common(1))
  1. A. [('a', 3), ('d', 4)] [('d', 4)]
  2. B. [('d', 4), ('b', 2)] [('d', 4)]
  3. C. [('d', 4), ('a', 3)] [('a', 3)]
  4. D. [('d', 4), ('a', 3)] [('d', 4)]

Correct answer: D. [('d', 4), ('a', 3)] [('d', 4)]

nlargest orders the (word, count) pairs by count, and most_common uses the same ordering. d appears four times and a appears three times.

More Python Programming questions

Exams that ask this topic