ProgrammingHardTCS NQT · Accenture

What is printed?

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

Correct answer: D. ['a100', 'b9', 'b10', 'c2']

The key sorts by the letter first and then by the numeric part, so b9 comes before b10 even though the string '10' is smaller than '9'.

More Python Programming questions

Exams that ask this topic