Skip to the content

Project: leaderboard

Python · Unit 10: More on functions

You'll build the leaderboard for a tournament. Each player is a tuple (name, points).

With -p[1] in the key, whoever has the most points wins. But Leo and Bea tie, and they stay in the order they came in.

table = [
    ("Leo", 7),
    ("Ana", 9),
    ("Bea", 7),
]
order = sorted(
    table, key=lambda p: -p[1]
)
print(order)

Prints

[('Ana', 9), ('Leo', 7), ('Bea', 7)]

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

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

  1. 1. Complete the code

    Fill in the key so whoever has the most points wins, and in a tie, the name that comes first alphabetically.

  2. 2. Write the code

    Write standings(table). Each player in table is (name, points). Return the list of names from most to fewest points, and if they tie, in alphabetical order.

  3. 3. Write the code

    Write leader(table): return the name of whoever is in first place, with the same tie-break. If the table is empty, return "".

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 →