ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
rows = [('bob', 90), ('amy', 90), ('cat', 75)]
rows.sort(key=lambda r: (-r[1], r[0]))
print([r[0] for r in rows])- A. ['amy', 'bob', 'cat']
- B. ['bob', 'amy', 'cat']
- C. ['cat', 'amy', 'bob']
- D. ['amy', 'cat', 'bob']
Correct answer: A. ['amy', 'bob', 'cat']
The key sorts by score descending through the negation, and names ascending as the tie-breaker. amy and bob both score 90, and amy comes first alphabetically.
More Python Programming questions
- What is printed? python print([1, 2] < [1, 2, 0], (1, 2) < (1, 2, 0), 'abc' < 'abd', [2] < [1, 9])
- What is printed? python x = [1, 2, 3, 4] y = x x = x + [5] x.append(6) y.append(9) print(len(x), len(y), y is x)
- What is printed? python from collections import Counter c = Counter([3, 1, 3, 2, 1, 3]) print(c.mostcommon(1), sorted(c.elements()), c[7])
- What is printed? python from math import comb print(comb(5, 2), comb(5, 7), comb(0, 0), comb(3, 1) comb(3, 1))
- What is printed? python x = 0 total = 0 for i in range(1, 6): if i % 2: continue x += i if x > 4: break else: total = -1 print(x, total)
- What is printed? python def f(n): if n <= 1: return n return f(n - 1) + f(n - 2) print(f(20), f.defaults)