An interviewer asks: "Players post scores, and GET /leaderboard?top=3 shows the best ones. What do you do when two players have the same score?" The expected answer: players with the same score share a rank, and the next rank skips the places they took, so the ranks go 1, 1, 3. This is how sports results are ranked, and it is called standard competition ranking.
The idea in one sentence: sort the players from the highest score down, give each one its position as its rank, and let a player who ties the one above copy that player's rank instead.
For example, four runners finish a race in 10.1, 10.3, 10.3 and 10.6 seconds. The two runners at 10.3 both finish 2nd, and the last runner is 4th, because three runners were faster:
time 10.1 10.3 10.3 10.6 rank 1 2 2 4
The starting code already keeps each player's best score (a lower score doesn't replace it) and builds the board with the position as the rank. Your code must answer:
POST /scores {"player": "ana", "score": 90} → 200 {"player": "ana", "best": 90}
POST /scores {"player": "ana", "score": 70} → 200 {"player": "ana", "best": 90}
GET /leaderboard → 200 [{"rank": 1, "player": "ben", "score": 120}, {"rank": 1, "player": "eve", "score": 120}, {"rank": 3, "player": "ana", "score": 90}, {"rank": 3, "player": "cara", "score": 90}, {"rank": 5, "player": "dan", "score": 80}]
GET /leaderboard?top=3 → 200 [{"rank": 1, "player": "ben", …}, {"rank": 1, "player": "eve", …}, {"rank": 3, "player": "ana", …}, {"rank": 3, "player": "cara", …}]
GET /leaderboard?top=1 → 200 [{"rank": 1, "player": "ben", "score": 120}, {"rank": 1, "player": "eve", "score": 120}]
Your task: sort the entries with by_score, let an entry that ties the one before it copy the rank of the board's previous row, and stop the loop once a rank is bigger than top. Players tied at the last rank still make the board, so top=3 returns four players.
Assignment
Follow the Coding Tutorial to sort the leaderboard, let ties share a rank and cut the board at top.
Hint
Check i > 0 before you compare entries[i] with entries[i - 1]: the first entry has nobody before it. The entries are sorted, so ranks only go up, and the first rank bigger than top means you can break.
Free plan: 30 tutor messages. PRO is unlimited.
