ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
from functools import cmp_to_key
print(sorted(['10', '9', '2'], key=cmp_to_key(lambda a, b: int(a) - int(b))))
  1. A. ['10', '9', '2']
  2. B. ['10', '2', '9']
  3. C. ['2', '10', '9']
  4. D. ['2', '9', '10']

Correct answer: D. ['2', '9', '10']

cmp_to_key turns the comparison function into a sort key. The function compares the integer values, so the order is numeric: 2, 9 and then 10.

More Python Programming questions

Exams that ask this topic