ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
import bisect
board = []
for name, score in [('a', 5), ('b', 9), ('c', 5)]:
    bisect.insort(board, (-score, name))
print([n for _, n in board])
  1. A. ['b', 'c', 'a']
  2. B. ['b', 'a', 'c']
  3. C. ['a', 'b', 'c']
  4. D. ['c', 'b', 'a']

Correct answer: B. ['b', 'a', 'c']

Storing (-score, name) keeps the list sorted by highest score first and then by name. The 9 comes first, then the two 5s in alphabetical order.

More Python Programming questions

Exams that ask this topic