ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq h = [1, 3, 5] r = heapq.heapreplace(h, 0) print(r, sorted(h))
- A. 1 [3, 5, 0]
- B. 3 [0, 1, 5]
- C. 1 [0, 3, 5]
- D. 0 [1, 3, 5]
Correct answer: C. 1 [0, 3, 5]
heapreplace pops the smallest element first (1 is returned) and then pushes 0. heappushpop does the opposite order, pushing first and then popping, so it would return 0 directly.
More Python Programming questions
- What is printed? python import heapq h = [1, 3, 5] r = heapq.heappushpop(h, 0) print(r, sorted(h))
- 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?