Predict the output
What does this print?
import heapq
feeds = [[1, 7], [2, 3, 9], [5]]
print(list(heapq.merge(*feeds)))Choose an answer, then open Show answer below to check it.
Show answer
Answer: A [1, 2, 3, 5, 7, 9]
Right: one sorted output from three sorted feeds.
The standard library's merge keeps only one front value per feed in its heap, so memory stays O(k).
Why not the others?
- B [1, 7, 2, 3, 9, 5]. That is plain concatenation.
- C [[1, 2, 5], [3, 7], [9]]. merge yields single values, not groups.