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]))
- A. [('x', 3), ('z', 1), ('y', 2)] [('z', 1), ('y', 2), ('x', 3)]
- B. [('x', 3), ('y', 2), ('z', 1)] [('x', 3), ('y', 2), ('z', 1)]
- C. [('x', 3), ('y', 2), ('z', 1)] [('y', 2), ('z', 1), ('x', 3)]
- 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
- What is printed? python print(10 % 3 2, 10 % (3 2), (10 % 3) 2, -10 % 3 2)
- What is printed? python print(list(range(10, 0, -3)), list(range(-1, -10, -4)), len(range(0, 10, 3)), range(5)[-1])
- What is printed? python print(len('anb'), len('anb'), len(r'anb'), len('arb'))
- What is printed? python print(8 % -3, -8 % -3, 8 // -3, -8 // -3)
- What is printed? python t = str.maketrans('abc', 'xyz') print('aabbcc'.translate(t), len(t))
- What is printed? python import heapq h = [5, 1, 3, 1] heapq.heapify(h) print(h[0], heapq.heappop(h), heapq.nsmallest(2, [4, -1, 3])…