ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq h = [5, 7, 9] heapq.heapify(h) r1 = heapq.heappushpop(h, 6) r2 = heapq.heappushpop(h, 1) print(r1, r2, sorted(h))
- A. 6 1 [5, 7, 9]
- B. 5 6 [1, 7, 9]
- C. 5 1 [5, 7, 9]
- D. 5 1 [6, 7, 9]
Correct answer: D. 5 1 [6, 7, 9]
heappushpop pushes the new item first and then pops the smallest. Pushing 6 returns the old minimum 5 and leaves 6 in the heap, and pushing 1 returns 1 without changing the heap.
More Python Programming questions
- What is printed? python import bisect a = [1, 3, 3, 5, 7] bisect.insort(a, 3) bisect.insortright(a, 4, lo=0, hi=3) print(a…
- What is printed? python def f(x): out = [] try: out.append('t') if x: raise ValueError except ValueError: out.append('e') else…
- What is printed? python def find(g, t): for i, row in enumerate(g): for j, v in enumerate(row): if v == t: break else: continue return (i…
- What is printed? python from functools import totalordering @totalordering class V: def init(self, n): self.n = n def eq(self, o): return…
- What is printed? python def make(): memo = {} def fib(n): if n in memo: return memo[n] memo[n] = n if n < 2 else fib(n - 1) + fib(n - 2)…
- What is printed? python words = 'the cat and the dog and the end'.split() cnt = {} for w in words: cnt[w] = cnt.get(w, 0) + 1 top =…