ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
print(sorted([('b', 1), ('a', 1), ('c', 2)], key=lambda t: t[1], reverse=True))- A. [('a', 1), ('b', 1), ('c', 2)]
- B. [('c', 2), ('b', 1), ('a', 1)]
- C. [('c', 2), ('a', 1), ('b', 1)]
- D. [('b', 1), ('a', 1), ('c', 2)]
Correct answer: B. [('c', 2), ('b', 1), ('a', 1)]
reverse=True does not reverse equal elements: sorted keeps ties in their original order, so b comes before a. Only the sort order of the keys is reversed.
More Python Programming questions
- What is printed? python print(max(['abc', 'x', 'yz'], key=len), min(['b', 'B', 'a']))
- What is printed? python from decimal import Decimal from fractions import Fraction print(0.1 + 0.2 == 0.3, Decimal('0.1') + Decimal('0.2')…
- What is printed? python print(1e22, 1e21, 1e16, 1e15, 1e-5, 0.0001)
- What is printed? python print(int(1e16 + 1), 253 + 1 == float(253 + 1), float(253 + 1) == 253)
- What is printed? python print(pow(10, 18, 7), (-3) 3 % 7, pow(-3, 3, -7), -3 3 % 7)
- What is printed? python print('%.3s|%5.1s|' % ('abcdef', 'xyz'))