Embercode
✕
SortingQuick quiz

Comparator contracts

8 questions. Covers Bad comparator bugs in Python.

Questions 1 to 5 of 8

  1. 1. A class defines __eq__ but no ordering method. What happens when sorted() runs on a list of its objects?
  2. 2. What does this code do? ``python sorted([3, None, 1]) ``
  3. 3. What does this program print? ``python tasks = [(1, 2), (2, 2), (3, 1), (4, 2)] # (task id, priority) by_priority = {} for task_id, priority in tasks: by_priority[priority] = task_id print(len(by_priority)) ``
  4. 4. What is the name of the contract rule that says the order must chain, so that if a comes before b and b comes before c, then a comes before c?

    Type your answer.

  5. 5. A comparator returns -1 for both compare(a, b) and compare(b, a). Which contract rule does it break?

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.