ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
from collections import Counter
c = Counter('mississippi')
print(c.most_common(2), sorted(c.items(), key=lambda kv: kv[1])[0])- A. [('s', 4), ('i', 4)] ('m', 1)
- B. [('i', 4), ('s', 4)] ('p', 2)
- C. [('i', 4), ('p', 2)] ('m', 1)
- D. [('i', 4), ('s', 4)] ('m', 1)
Correct answer: D. [('i', 4), ('s', 4)] ('m', 1)
most_common keeps the first-inserted letter first when counts tie, so i (seen before s) comes first. sorted is stable, so the smallest count with the earliest insertion is m.
More Python Programming questions
- What is printed? python x = 5 y = x if x > 3 else 0 + 1 if x else -1 print(y, (lambda a, b=2: a b)(3), (lambda a: sum(a))(1, 2, 3))
- What is printed? python print(sorted([2.0, 2, True, 1.5]))
- What is printed? python print(len({1: 'a', True: 'b', 1.0: 'c'}), list({True: 1, 1: 2}.keys()))
- What is printed? python print(int(1e308) > 10308, float('1e-400'), 5e-324 / 2, 5e-324 / 2 == 0)
- What is printed? python print(1e-7 == 0.0000001, 1e-7, 0.0000001, 1.0e5 == 100000.0, 123e-2)
- What is printed? python print(7 // 2 2 + 7 % 2 == 7, -(7 // 2) 2 + -(7 % 2) == -7, (-7) // 2 2 + (-7) % 2)