ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
print(sorted([(-1, 'a'), (1, 'b'), (-1, 'c')], key=lambda t: t[0], reverse=True))
- A. [(-1, 'c'), (-1, 'a'), (1, 'b')]
- B. [(1, 'b'), (-1, 'c'), (-1, 'a')]
- C. [(1, 'b'), (-1, 'a'), (-1, 'c')]
- D. [(-1, 'a'), (1, 'b'), (-1, 'c')]
Correct answer: C. [(1, 'b'), (-1, 'a'), (-1, 'c')]
Reverse order puts 1 first. The two -1 entries tie, and sorted keeps tied items in their input order, so a comes before c.
More Python Programming questions
- What is printed? python x = 'abc' y = x.upper() z = x.upper().lower() print(y, z == x, x.upper().upper() == y, len(y + z))
- What is printed? python print(int('0009'), float('009.50'), int(' -0 '), str(-0))
- What is printed? python print(len(str(2 64)), str(2 64)[:4], 2 64 % 1000)
- What is printed? python print('%c%c' % (72, 105), chr(0x263A) == 'u263a', ord('A') + 1, chr(ord('a') + 25))
- What is printed? python print(sum(i i for i in range(10)), sum(range(10)) 2 == sum(i 3 for i in range(10)), sum(range(10)) 2)
- What is printed? python print(f'{3.14159:.2}', f'{"abc":.2}', f'{0.5:.0%}')