ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq
h = []
for x in [4, 9, 1, 7]:
heapq.heappush(h, -x)
print(-heapq.heappop(h), -h[0])- A. 1 7
- B. 9 9
- C. 9 7
- D. 9 4
Correct answer: C. 9 7
Pushing negated values turns the min-heap into a max-heap. The first pop gives -9, so the largest value is 9. The new top after the pop is -7, so h[0] negated is 7.
More Python Programming questions
- What is printed? python from functools import partial def power(base, exp): return base exp sq = partial(power, exp=2) cubeofthree =…
- What is printed? python import itertools it = itertools.count(10, 5) print(list(itertools.islice(it, 3)), next(it))
- What is printed? python words = ['b', 'a', 'c'] pairs = list(zip(sorted(words), range(10, 13))) print(pairs, dict(pairs)['c']…
- What is printed? python s = 'aabbbc' out = [] i = 0 while i < len(s): j = i while j < len(s) and s[j] == s[i]: j += 1 out.append(s[i] +…
- What is printed? python import math print(math.comb(10, 3) % 7, math.perm(5, 2), math.factorial(5) // math.factorial(3))
- What is printed? python x = [1, 2, 3] it = iter(x) print(next(it), sum(it), list(it))