ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
rows = [('bob', 90), ('amy', 90), ('cat', 75)]
rows.sort(key=lambda r: (-r[1], r[0]))
print([r[0] for r in rows])
  1. A. ['amy', 'bob', 'cat']
  2. B. ['bob', 'amy', 'cat']
  3. C. ['cat', 'amy', 'bob']
  4. D. ['amy', 'cat', 'bob']

Correct answer: A. ['amy', 'bob', 'cat']

The key sorts by score descending through the negation, and names ascending as the tie-breaker. amy and bob both score 90, and amy comes first alphabetically.

More Python Programming questions

Exams that ask this topic