ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
nums = [13, 22, 5, 31, 8, 3]
print(sorted(nums, key=lambda x: x % 10), sorted(nums, key=lambda x: x % 10, reverse=True))
  1. A. [1, 22, 13, 3, 5, 8] [8, 5, 13, 3, 22, 31]
  2. B. [31, 22, 13, 3, 5, 8] [8, 5, 13, 3, 22, 31]
  3. C. [31, 22, 3, 13, 5, 8] [8, 5, 13, 3, 22, 31]
  4. D. [31, 22, 13, 3, 5, 8] [8, 5, 3, 13, 22, 31]

Correct answer: B. [31, 22, 13, 3, 5, 8] [8, 5, 13, 3, 22, 31]

The keys are 3, 2, 5, 1, 8, 3. 13 and 3 tie on key 3. Both sorts keep equal keys in their original order, including reverse=True, so 13 stays ahead of 3 in both results.

More Python Programming questions

Exams that ask this topic