ProgrammingVery hardProduct-company MCQ pattern
What is printed on the second line?
What is printed on the second line?
nums = [5, -3, 0, 2, -3] print(sorted(nums, key=lambda x: x < 0)) print(sorted(nums, key=lambda x: (x < 0, -x)))
- A. [5, 2, 0, -3, -3]
- B. [5, 0, 2, -3, -3]
- C. [-3, -3, 5, 2, 0]
- D. [5, 2, -3, -3, 0]
Correct answer: A. [5, 2, 0, -3, -3]
The tuple key puts non-negatives first and sorts them by descending value, and the two -3 values share a key so they keep input order. The first line only splits by sign and keeps the original order.
More Python Programming questions
- What is printed on the first line? python data = [('x', 3.0), ('y', 3), ('z', 2.5)] print(sorted(data, key=lambda t: t[1]))…
- What is printed? python s = 'level' t = ''.join(reversed(s)) print(s == t, s[::-1] is s, s[::-1] == t, s.count('l') s.count('e'))
- What is printed? python a = 1e308 b = a 10 print(b, -b, b - b if False else 0, b > a, b == float('inf'))
- What is printed? python import math print(math.sqrt(16), math.sqrt(2) 2 == 2, math.isqrt(2) 2 == 2, math.pow(2, 0.5) == 2 0.5)
- What is printed? python print(int(0.1 10) == 1, int(0.7 10), int(4.35 100), round(4.35 100))
- What is printed? python print(len(str(264)), 264 // 1019, 264 % 1019, len(str(264 - 1)))