ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
people = [("ann", 30), ("bob", 25), ("cid", 30), ("dan", 25)]
people.sort(key=lambda p: p[0], reverse=True)
people.sort(key=lambda p: p[1])
print([p[0] for p in people])- A. ['dan', 'bob', 'cid', 'ann']
- B. ['bob', 'dan', 'ann', 'cid']
- C. ['dan', 'bob', 'ann', 'cid']
- D. ['bob', 'dan', 'cid', 'ann']
Correct answer: A. ['dan', 'bob', 'cid', 'ann']
Sorting twice with a stable sort gives a multi-key order: the second sort (by age) keeps the reverse-name order from the first sort among equal ages.
More Python Programming questions
- What is printed? python nums = [4, -1, 3, -1, 2] print(sorted(nums, key=lambda v: (v < 0, -v)), sorted(set(nums), reverse=True)[:2])
- What is printed? python def walk(d, depth=0): out = [] for k, v in d.items(): out.append(f"{k}{depth}") if isinstance(v, dict): out +=…
- What is printed? python def hanoi(n): return 0 if n == 0 else 2 hanoi(n - 1) + 1 def calls(n): return 1 if n == 0 else 1 + 2 calls(n - 1)…
- What is printed? python import sys def depth(n): try: return depth(n + 1) except RecursionError: return n d = depth(0) print(d > 500, d <…
- What is printed? python x = 0 def a(): global x x = 1 def b(): global x x += 10 b() return x print(a(), x)
- What is printed? python class A: n = [] m = 0 a, b = A(), A() a.n.append(1) a.m += 1 b.n = [2] print(A.n, A.m, a.n, b.n, a.m, b.m)