ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
print(sorted(range(10), key=lambda x: (x % 3, -x))[:4])
  1. A. [9, 6, 3, 0]
  2. B. [0, 3, 6, 9]
  3. C. [9, 7, 6, 4]
  4. D. [0, 1, 2, 3]

Correct answer: A. [9, 6, 3, 0]

The key groups numbers by remainder mod 3, and inside each group -x puts the larger values first. Remainder 0 gives 9, 6, 3, 0, which are the first four.

More Python Programming questions

Exams that ask this topic