ProgrammingModerateTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
import heapq print(heapq.nlargest(2, ['pear', 'fig', 'apple'], key=len))
- A. ['pear', 'fig']
- B. ['apple', 'pear']
- C. ['pear', 'apple']
- D. ['apple', 'fig']
Correct answer: B. ['apple', 'pear']
nlargest with key=len picks the two longest words: 'apple' (5 letters) and 'pear' (4). The result is in descending order of key.
More Python Programming questions
- What is printed? python import heapq h = [] for t in [(2, 'b'), (2, 'a'), (1, 'z')]: heapq.heappush(h, t) print([heapq.heappop(h) for in…
- What is printed? python import heapq print(heapq.nsmallest(3, [3, 1, 3, 2], key=lambda x: -x))
- 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))