ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq nums = [7, 2, 9] heapq.heapify(nums) print(nums[0], heapq.nlargest(1, nums)[0], heapq.nsmallest(2, nums))
- A. 2 9 [2, 9]
- B. 2 9 [2, 7]
- C. 2 7 [2, 9]
- D. 7 9 [2, 7]
Correct answer: B. 2 9 [2, 7]
heapify puts the smallest value at index 0. nlargest(1) returns the largest value, 9. nsmallest(2) returns the two smallest values in ascending order.
More Python Programming questions
- What is printed? python print(any(x > 3 for x in range(10)), next(x for x in range(10) if x x > 20), sum(x for x in range(0, 10, 4)))
- What is printed? python s = '12:34:56' h, m, sec = map(int, s.split(':')) print(h 3600 + m 60 + sec, divmod(h 3600 + m 60 + sec, 60))
- What is printed? python print([x % 3 for x in [-7, -3, 0, 4]])
- What is printed? python def counter(): count = 0 def step(): nonlocal count count += 2 return count return step c1 = counter() c2 =…
- What is printed? python values = {'x': [1, 2], 'y': [3]} total = sum(len(v) for v in values.values()) keys = sorted(values, key=lambda k…
- What is printed? python print(int(' -0012 '), int('+5'), float('-.5'), float('5.'))