ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
print(sorted(['b10', 'b9', 'a100'], key=lambda s: (s[0], int(s[1:]))))
  1. A. ['b9', 'b10', 'a100']
  2. B. ['b10', 'b9', 'a100']
  3. C. ['a100', 'b9', 'b10']
  4. D. ['a100', 'b10', 'b9']

Correct answer: C. ['a100', 'b9', 'b10']

The key sorts by the letter first and then by the number as an int, so b9 comes before b10. Sorting the raw strings would put b10 before b9.

More Python Programming questions

Exams that ask this topic