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))
- A. [('a', 3), ('d', 4)] [('d', 4)]
- B. [('d', 4), ('b', 2)] [('d', 4)]
- C. [('d', 4), ('a', 3)] [('a', 3)]
- 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
- What is printed? python from functools import cache @cache def cat(n): if n <= 1: return 1 return sum(cat(i) cat(n - 1 - i) for i in…
- What is printed? python from functools import lrucache grid = [[1, 3, 1], [1, 5, 1], [4, 2, 1]] @lrucache(maxsize=None) def best(r, c): if…
- What is printed? python def sigma(n): return sum(d for d in range(1, n + 1) if n % d == 0) perfect = [n for n in range(2, 30) if sigma(n)…
- What is printed? python def phi(n): res, p = n, 2 while p p <= n: if n % p == 0: while n % p == 0: n //= p res -= res // p p += 1 if n >…
- What is printed? python def cntinc(a, k): dp = [[0] (k + 1) for in range(len(a))] for i in range(len(a)): dp[i][1] = 1 for j in range(i)…
- What is printed? python from itertools import count, islice print(list(zip('abc', count(10, 5))), list(islice(count(1, 2), 3)))