Case File: The Tied Table
The monthly safety quiz needs a ranked table for the notice board, and several people have the same score. Sort by points (most first) and then by name, give tied players the same rank and the next player the rank of their place (1, 1, 1, 4), and list the winners. Learn how to sort with a tuple key, why a fair tie-break matters, and how a ranking differs from a sort. The scores are generated for you alone.
Sabi Logistics · Python 7
The Tied Table
Two people both think that they won the safety quiz, and the printed table shows them in different orders every month
Ifeoma Lead developer ·
Ifeoma. The monthly safety quiz results go on the notice board. Last month two people both said that they had won, because the table is printed from a spreadsheet that sorts ties in no particular order, so the order changes every time it is opened.
Please make the table repeatable: most points first, and among people with the same points, by name from A to Z. And show a rank: people with the same points share a rank, and the next person gets the rank of their place, so three people tied for first are all 1 and the next one is 4. Then list the winners: everybody with rank 1.
The brief
What you need to know first.
- Sorting by a key.
sorted(players, key=...)sorts by the value of the key function. To sort by two things, return a tuple:key=lambda p: (-p["points"], p["name"]). Python compares tuples element by element: first the points (the minus sign turns the order round, so most points come first), and among equal points the names from A to Z. - Why a tie-break matters. A sort that has no rule for ties leaves them in whatever order the data happened to be in, so the same data can print differently. Adding the name as a second key makes the result repeatable: the same input gives the same table every time.
- Ranking is not sorting. Walk through the sorted list with
for i, p in enumerate(ordered):. For the first player, and for any player whose points differ from the one before, the rank isi + 1(the place, counting from 1); otherwise it is the same as the previous rank. Keep the previous rank in a variable. This is “competition ranking”: 1, 1, 1, 4. - The result. A list of
{"rank", "name", "points"}in table order. The winners are the names whererank == 1, in table order; for no players, an empty table and no winners. - Fill in
table(req), thenleagueandwinners. The checks runtableon lists that you cannot see: three players tied for first (the order must be A, B, C), and an empty list.
The lab gives you the data for this case, invented and generated for you alone.
Preparing your data…
Ifeoma: “The table is repeatable now. But somebody asks whether ranking 1, 1, 1, 4 is fair, or whether the next person should be 2.”
🔒 The debrief unlocks when you close the case. Finish the lab above.
The debrief
Ifeoma: “So even the ranking is a choice.”
What an experienced person would do next. Tell people the ranking rule on the board (“tied players share a rank; the next rank skips”) because it is a choice: some competitions use 1, 1, 1, 2 (dense ranking) and some use 1, 2, 3, 4 with a tie-break. Pick one before the results are known. When a prize depends on the rank, add a real tie-break that people accept (a quicker time, an earlier submission) instead of a name order, which is repeatable but arbitrary. Keep the table sorted in a deterministic way whenever it is printed, so that two people looking at it see the same thing.
The trap. Sorting only by points and trusting the order of the ties, and giving a tied player the rank of the previous row plus one instead of the rank of the place.
Write the table note
Write the note for the lead developer for the notice board: how the table is sorted (most points first, then name A to Z) and why the second key makes it repeatable, how the rank is given (tied players share a rank and the next one gets the rank of the place, as in 1, 1, 1, 4), the winners this month, and the choices that should be agreed beforehand (competition or dense ranking, a real tie-break for prizes).
🧪 Want to practise this on your own computer? See how to set up your lab →