ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
data = [('b', 2), ('a', 2), ('c', 1), ('a', 1)]
print(sorted(sorted(data, key=lambda t: t[0]), key=lambda t: t[1], reverse=True))- A. [('b', 2), ('a', 2), ('a', 1), ('c', 1)]
- B. [('a', 2), ('b', 2), ('a', 1), ('c', 1)]
- C. [('a', 2), ('b', 2), ('c', 1), ('a', 1)]
- D. [('b', 2), ('a', 2), ('c', 1), ('a', 1)]
Correct answer: B. [('a', 2), ('b', 2), ('a', 1), ('c', 1)]
The first sort orders by name, and sorted is stable, so later sorts keep that order for ties. Sorting by the number in reverse keeps ties in their existing order: a before b for value 2, and a before c for value 1.
More Python Programming questions
- What is printed? python print(int('0x1F', 16), int('1F', 16), int('0b101', 0), int(' 42 '), int('42'))
- What is printed? python print(int('٣') + 1)
- What is printed? python print(bin(10)[2:].zfill(8), format(-10, '08b'), format(-10 & 0xFF, '08b'), f'{255:#x}')
- What is printed? python print((1 << 70).bitcount(), (270 - 1).bitcount(), (-1).bitcount(), (-8).bitcount())
- What is printed? python print(f'{0.1:.20f}', f'{1e16:g}', f'{123456789.0:e}', f'{2.5:.0f}', f'{3.5:.0f}')
- What is printed? python n = float('nan') print(n in [n], [n] == [n], n == n, n is n)