ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
print(sorted([-3, 2, -1, 4], key=lambda v: (abs(v) % 3, v)))
  1. A. [-3, 4, -1, 2]
  2. B. [-3, -1, 4, 2]
  3. C. [-1, -3, 4, 2]
  4. 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

Exams that ask this topic