ProgrammingHardCognizant GenC · TCS NQT
What is printed?
What is printed?
words = 'the cat and the dog and the end'.split()
cnt = {}
for w in words:
cnt[w] = cnt.get(w, 0) + 1
top = sorted(cnt, key=lambda w: -cnt[w])[:3]
print(top)- A. ['the', 'and', 'end']
- B. ['the', 'and', 'dog']
- C. ['the', 'cat', 'and']
- D. ['the', 'and', 'cat']
Correct answer: D. ['the', 'and', 'cat']
Sorting by negative count is stable, so among words with one occurrence the earliest inserted word, cat, comes first. Dict insertion order decides ties.
More Python Programming questions
- What is printed? python def merge(iv): iv.sort() out = [iv[0]] for s, e in iv[1:]: if s <= out[-1][1]: out[-1][1] = max(out[-1][1], e)…
- What is printed? python import heapq def dijk(g, s): d = {s: 0} h = [(0, s)] while h: du, u = heapq.heappop(h) if du > d[u]: continue for…
- What is printed? python from collections import deque def topo(n, edges): indeg = [0] n adj = [[] for in range(n)] for u, v in edges…
- What is printed? python dp = [0] 16 for i in range(1, 16): dp[i] = dp[i >> 1] + (i & 1) print(dp[13], sum(dp))
- What is printed? python print([format(i ^ (i >> 1), '02b') for i in range(4)])
- What is printed? python from functools import reduce import operator a = [4, 1, 2, 1, 2] print(reduce(operator.xor, a)…