ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq h = [1, 3] heapq.heapify(h) print(heapq.heapreplace(h, 0), sorted(h), heapq.heappushpop(h, 0), sorted(h))
- A. 1 [0, 3] 1 [0, 3]
- B. 1 [0, 3] 0 [0, 3]
- C. 0 [0, 3] 0 [0, 3]
- D. 1 [1, 3] 0 [0, 3]
Correct answer: B. 1 [0, 3] 0 [0, 3]
heapreplace pops the smallest item first and then pushes, so it returns 1 and leaves [0, 3]. heappushpop pushes 0 and immediately pops it because it is the smallest, so the heap is unchanged.
More Python Programming questions
- What is printed? python try: print(list(zip([1, 2], [3], strict=True))) except ValueError: print('strict')
- What is printed? python print(round(1234, -2), round(1250, -2), round(1350, -2))
- What is printed? python def lcsub(a, b): best = end = 0 dp = [[0] (len(b) + 1) for in range(len(a) + 1)] for i in range(1, len(a) + 1)…
- A solution must decide whether a number n up to 10^12 is prime within about one second in CPython. Which approach is correct and fast…
- What is printed? python d = {'a': 1, 'b': 2} print(d.popitem(), d)
- What is printed? python print('{0}{1}{0}'.format('x', 'y'))