ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
students = [('Ravi', 88), ('Asha', 92), ('Bala', 88), ('Chitra', 92)]
print([n for n, s in sorted(students, key=lambda t: (-t[1], t[0]))], [n for n, s in sorted(students, key=lambda t: -t[1])])- A. ['Asha', 'Chitra', 'Bala', 'Ravi'] ['Asha', 'Chitra', 'Bala', 'Ravi']
- B. ['Asha', 'Bala', 'Chitra', 'Ravi'] ['Asha', 'Chitra', 'Ravi', 'Bala']
- C. ['Asha', 'Chitra', 'Bala', 'Ravi'] ['Asha', 'Chitra', 'Ravi', 'Bala']
- D. ['Ravi', 'Bala', 'Asha', 'Chitra'] ['Asha', 'Chitra', 'Ravi', 'Bala']
Correct answer: C. ['Asha', 'Chitra', 'Bala', 'Ravi'] ['Asha', 'Chitra', 'Ravi', 'Bala']
The first sort uses score then name, so ties break alphabetically. The second sorts only by score, and sorted is stable, so ties keep the input order: Ravi came before Bala.
More Python Programming questions
- What is printed? python print('Hello'[1:3] + 'Hello'[-1], len('Hello'[10:]), 'Hello'[::2])
- What is printed? python print(10 / 3 3 == 10, 10 // 3 3 == 10, 10 % 3)
- What is printed? python x = 0.1 + 0.2 print(x > 0.3, round(x, 15) == 0.3, x - 0.3 < 1e-9, '%.17g' % x)
- What is printed? python parts = [] for i in range(3): parts.append(str(i) i) print(repr(''.join(parts)), ''.join(map(str, range(3))))
- What is printed? python print(int('07'), int('07')) try: print(int('07', 0)) except ValueError: print('leading zero')
- What is printed? python x = 1 / 3 print(x 3 == 1, 1 - 2 / 3 == 1 / 3, 0.1 + 0.2 - 0.3 == 0)