ProgrammingModerateTCS NQT · Wipro pattern
What is printed?
What is printed?
import heapq a, b, c = [1, 4, 7], [2, 5], [0, 3, 6] print(list(heapq.merge(a, b, c))[:5], next(heapq.merge(b, a)))
- A. [0, 1, 2, 3, 4] 1
- B. [0, 1, 2, 3, 4] 2
- C. [1, 2, 3, 4, 5] 1
- D. [0, 1, 2, 3, 4] 0
Correct answer: A. [0, 1, 2, 3, 4] 1
heapq.merge streams sorted inputs lazily, so the first five items are the five smallest overall. Merging [2, 5] with [1, 4, 7] starts with 1.
More Python Programming questions
- What is printed? python import heapq def kth(m, k): h = [(m[0][0], 0, 0)] seen = {(0, 0)} for in range(k - 1): v, r, c = heapq.heappop(h)…
- What is printed? python from itertools import groupby rows = [('b', 2), ('a', 1), ('b', 5), ('a', 3)] ok = {k: sum(v for , v in g) for k…
- What is printed? python def lru(cap, ops): d = {} out = [] for op, k in ops: if op == 'get': if k in d: v = d.pop(k) d[k] = v…
- What is printed? python from itertools import combinations a = [1, 2, 3, 4] cnt = 0 for r in range(1, len(a) + 1): for c in…
- What is printed? python from collections import Counter def pairseq(a, t): c = Counter(a) total = 0 for x in c: y = t - x if x < y: total…
- What is printed? python import bisect def minlen(a, s): pre = [0] for x in a: pre.append(pre[-1] + x) best = float('inf') for i in…