ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
print(max(-3, 2, key=abs), min(-3, 2, key=abs), sorted([-3, 2, -2], key=abs), sorted([-3, 2, -2], key=lambda v: -abs(v)))
- A. 2 -3 [2, -2, -3] [-3, 2, -2]
- B. -3 2 [2, -2, -3] [-3, 2, -2]
- C. -3 2 [-2, 2, -3] [-3, 2, -1]
- D. -3 2 [2, -2, -3] [-3, -2, 5]
Correct answer: B. -3 2 [2, -2, -3] [-3, 2, -2]
max and min with key=abs compare absolute values, so -3 wins the max and 2 wins the min. Sorting by abs keeps the input order for ties between 2 and -2, and sorting by -abs puts the largest magnitude first.
More Python Programming questions
- What is printed? python print(1 - 0.9 == 0.1, round(1 - 0.9, 1) == 0.1, 1 - 0.9 > 0.09, 1 - 0.9)
- What is printed? python print(len('a1b22c333'.split('2')), ''.join(c for c in 'a1b22c333' if c.isdigit()), len('a1b22c333'))
- What is printed? python print(0.1 + 0.1 + 0.1 == 0.3, 0.1 3, 0.3 / 0.1, int(0.3 / 0.1))
- What is printed? python print(format(0.5, '.0f'), format(1.5, '.0f'), format(2.5, '.0f'), format(-0.5, '.0f'))
- What is printed? python print('ab' 2 == 'abab', 'ab' 0 == '', 'a' 2 + 'b' 0 == 'aa', ('x' 2).count('xx'))
- What is printed? python print(int('٣٣') 2, '٣٣'.isdigit(), '٣٣'.isascii(), '3' == '٣')