ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq print(heapq.nsmallest(3, [3, 1, 3, 2], key=lambda x: -x))
- A. [3, 2, 1]
- B. [3, 3, 1]
- C. [1, 2, 3]
- D. [3, 3, 2]
Correct answer: D. [3, 3, 2]
The keys are -3, -1, -3, -2. The three smallest keys are -3 (the first 3), -3 (the second 3) and -2 (the 2). nsmallest is equivalent to sorted(...)[:n] with the same key, so ties keep their original order.
More Python Programming questions
- Dijkstra's algorithm with heapq has no decrease-key operation. Which approach is the standard one in Python?
- A program inserts n = 200000 items one at a time with lst.insert(0, x) and never removes them. Which is the time complexity of the whole…
- A solution does if x not in seenlist for each of n = 2 10^5 values, where seenlist grows to n items. Which change gives the best speedup…
- What is printed? python import heapq print(heapq.nsmallest(0, [5, 1]), max([], default=-1), min([3, 1, 2], key=lambda x: -x))
- What is printed? python print(pow(2, -1, 10 9 + 7))
- What is printed? python import math n = 10 30 - 1 print(math.isqrt(n), int(n 0.5))