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)))
  1. A. [0, 1, 2, 3, 4] 1
  2. B. [0, 1, 2, 3, 4] 2
  3. C. [1, 2, 3, 4, 5] 1
  4. 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

Exams that ask this topic