ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
print(sorted([-3, 2, -1, 4], key=lambda v: (abs(v) % 3, v)))
- A. [-3, 4, -1, 2]
- B. [-3, -1, 4, 2]
- C. [-1, -3, 4, 2]
- D. [-3, -1, 2, 4]
Correct answer: B. [-3, -1, 4, 2]
The key for each value is (abs(v) % 3, v). The keys are (0, -3), (2, 2), (1, -1) and (1, 4), and sorting them in order gives -3, -1, 4 and then 2.
More Python Programming questions
- What is printed? python print(f'{1234.5:,.2f}', f'{0.000123:.2e}', f'{-0.0:+.1f}', f'{0x123456:x}')
- What is printed? python print(int('٠٠٢٩') + 1, '٢٩'.isdigit())
- What is printed? python s = 'abc' print(s 0 == '', s[::-1][::-1] == s, s[1:-1] 2, s[-3:-1])
- What is printed? python print(sorted(['x10', 'x9', 'x1'], key=lambda s: int(s[1:])))
- What is printed? python print(0.1 3 == 0.3, 0.1 3 == 3 0.1, round(0.1 3, 1) == 0.3, 1 - 0.9 == 0.1)
- What is printed? python print('%s' % [1, 'a'], '%r' % 'a', '%5.2s|' % 'hello', '%c' % 65)