ProgrammingHardCognizant GenC · TCS NQT
What is printed?
What is printed?
people = [('Ann', 30), ('Bob', 25), ('Cid', 30), ('Dan', 25)]
s1 = sorted(people, key=lambda p: p[0])
s2 = sorted(s1, key=lambda p: p[1], reverse=True)
print([p[0] for p in s2])- A. ['Cid', 'Ann', 'Dan', 'Bob']
- B. ['Ann', 'Bob', 'Cid', 'Dan']
- C. ['Ann', 'Cid', 'Dan', 'Bob']
- D. ['Ann', 'Cid', 'Bob', 'Dan']
Correct answer: D. ['Ann', 'Cid', 'Bob', 'Dan']
The first pass sorts by name and the second sorts by age descending. Sorting is stable, so people with equal ages keep their name order: Ann before Cid, and Bob before Dan.
More Python Programming questions
- What is printed? python from itertools import accumulate h = [3, 1, 4, 1, 5, 9, 2, 6] vis = [x for x, m in zip(h, [0] + list(accumulate(h…
- For an array of 10^6 numbers, which approach finds the largest sum of k consecutive elements fastest and correctly?
- Which statement about the bisect module is FALSE?
- What is printed? python def mink(piles, h): lo, hi = 1, max(piles) while lo < hi: mid = (lo + hi) // 2 if sum(-(-p // mid) for p in piles)…
- What is printed? python import heapq def huff(freqs): h = list(freqs) heapq.heapify(h) cost = 0 while len(h) > 1: a = heapq.heappop(h) b =…
- What is printed? python def zeros(n): c = 0 while n: n //= 5 c += n return c print(zeros(100), zeros(25))