beginner

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.

45 min

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 is i + 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 where rank == 1, in table order; for no players, an empty table and no winners.
  • Fill in table(req), then league and winners. The checks run table on 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…

🔒 The debrief unlocks when you close the case. Finish the lab above.

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).

0 words

🧪 Want to practise this on your own computer? See how to set up your lab →

🤖

Need clarification? Ask GT

Hi, I'm GT — your AI tutor for Case File: The Tied Table. What would you like to know?
📝

Knowledge Check

1 of 3

How do you sort by points (most first) and then by name?

⚑ Capture The Flag

Report a problem

About: /lessons/case-the-tied-table