ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
d = {'pen': 3, 'cap': 5, 'ink': 3, 'box': 1}
print(sorted(d.items(), key=lambda kv: (-kv[1], kv[0]))[:3])- A. [('ink', 3), ('pen', 3), ('cap', 5)]
- B. [('cap', 5), ('ink', 3), ('box', 1)]
- C. [('cap', 5), ('ink', 3), ('pen', 3)]
- D. [('cap', 5), ('pen', 3), ('ink', 3)]
Correct answer: C. [('cap', 5), ('ink', 3), ('pen', 3)]
The key sorts by count descending, using the negation, and by name ascending on ties. cap is the largest, then ink and pen tie at 3 and are ordered by name.
More Python Programming questions
- What is printed? python from collections import deque n = 4 edges = [(0, 2), (1, 2), (2, 3), (0, 3)] indeg = [0] n adj = [[] for in…
- What is printed? python def cnt(xs, T): xs = sorted(xs) lo, hi, total = 0, len(xs) - 1, 0 while lo < hi: if xs[lo] + xs[hi] <= T: total +=…
- What is printed? python def knap(items, cap): dp = [0] (cap + 1) for w, v in items: for c in range(cap, w - 1, -1): dp[c] = max(dp[c]…
- What is printed? python import heapq h = [] for x in [4, 9, 1, 7]: heapq.heappush(h, -x) print(-heapq.heappop(h), -h[0])
- What is printed? python from functools import partial def power(base, exp): return base exp sq = partial(power, exp=2) cubeofthree =…
- What is printed? python import itertools it = itertools.count(10, 5) print(list(itertools.islice(it, 3)), next(it))