question 140 Built-ins & Sorting · hard
A leaderboard with shared ranks
A weekly quiz league publishes a leaderboard. Players are ordered by points, highest first, and players with equal points are listed alphabetically. Equal points share a rank, and the next rank skips accordingly (the sports-style 1, 2, 2, 4).
Function
leaderboard(scores)scoresa dictionary from player name to points
Returnsa list of tuples
(rank, name, points) in leaderboard orderRanksa player's rank is 1 + the number of players with more points; ties share a rank
Examples
callleaderboard({'Nora': 40, 'Theo': 55, 'Grace': 40, 'Jamal': 31})returns[(1, 'Theo', 55), (2, 'Grace', 40), (2, 'Nora', 40), (4, 'Jamal', 31)]
callleaderboard({'Solo': 12})returns[(1, 'Solo', 12)]
callleaderboard({})returns[]