ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq h = [1, 3, 5] r = heapq.heappushpop(h, 0) print(r, sorted(h))
- A. 1 [0, 3, 5]
- B. 0 [0, 1, 3, 5]
- C. 1 [3, 5]
- D. 0 [1, 3, 5]
Correct answer: D. 0 [1, 3, 5]
heappushpop pushes first and then pops. The pushed item 0 is smaller than the heap's minimum, so it is returned at once and the heap is not changed.
More Python Programming questions
- What is printed? python import heapq h = [] try: heapq.heappush(h, (1, {'a': 1})) heapq.heappush(h, (1, {'b': 2})) except TypeError as e…
- What is printed? python import heapq print(heapq.nlargest(2, ['pear', 'fig', 'apple'], key=len))
- What is printed? python import heapq h = [] for t in [(2, 'b'), (2, 'a'), (1, 'z')]: heapq.heappush(h, t) print([heapq.heappop(h) for in…
- What is printed? python import heapq print(heapq.nsmallest(3, [3, 1, 3, 2], key=lambda x: -x))
- Dijkstra's algorithm with heapq has no decrease-key operation. Which approach is the standard one in Python?
- A program inserts n = 200000 items one at a time with lst.insert(0, x) and never removes them. Which is the time complexity of the whole…