ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
pairs = [(1, 'b'), (1, 'a'), (0, 'c')] print(sorted(pairs, key=lambda p: p[0]), sorted(pairs))
- A. [(0, 'c'), (1, 'a'), (1, 'b')] [(0, 'c'), (1, 'b'), (1, 'a')]
- B. [(0, 'c'), (1, 'b'), (1, 'a')] [(0, 'c'), (1, 'a'), (1, 'b')]
- C. [(1, 'a'), (1, 'b'), (0, 'c')] [(0, 'c'), (1, 'a'), (1, 'b')]
- D. [(0, 'c'), (1, 'b'), (1, 'a')] [(0, 'c'), (1, 'b'), (1, 'a')]
Correct answer: B. [(0, 'c'), (1, 'b'), (1, 'a')] [(0, 'c'), (1, 'a'), (1, 'b')]
Sorting by the first element only keeps the original order of the two 1s, giving b before a. Sorting whole tuples compares the second element for ties, so a comes before b.
More Python Programming questions
- What is printed? python print(int('0b11', 0) + int('0o11', 0) + int('0x11', 0), int('11', 16), int('-0b11', 0))
- What is printed? python from math import inf print(inf - 1 == inf, 2 inf, -inf < -10 400, inf > 10 400)
- What is printed? python print(sum(1 for c in 'a1 b2' if c.isalpha()), sum(1 for c in 'a1 b2' if c.isdigit()), sum(c.isspace() for c in 'a1…
- What is printed? python print(round(2.675, 2), round(2.665, 2), round(1.0049999999999999, 3), round(-2.5, 0))
- What is printed? python print(max('apple', 'banana', key=len), max(['x', 'yy', 'zz'], key=len), min([3, 1, 1.0], key=abs), max([1, 1.0]…
- What is printed? python print(float('0.1') + float('0.2') == 0.3, float('0.30000000000000004') == 0.1 + 0.2…