Skip to the content

Project: top three

Python · Unit 23: Searching and sorting

A dominoes tournament stores every player as [name, points]. At the end the podium has to be published.

The tournament rule: most points first and, if two of them tie, the one that comes first alphabetically wins.

people = [
    ["Luke", 5],
    ["Ana", 5],
    ["Sophie", 9],
]
print(people[2][0], people[2][1])

Prints

Sophie 9

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Complete the code

    Complete the criterion that sorts by the tournament rule.

  2. 2. Write the code

    Write top_three(people). It receives a list of [name, points] and returns the names of the top three, from most points to fewest, and ties from A to Z. If there are fewer than three players, return the ones there are.

  3. 3. Write the code

    For the winner there is no need to sort: max with a key finds it in a single pass. Write best(people), which returns the name of whoever has the most points, or "" if the list arrives empty.

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →