Leaderboard With Shared Ranks
Implement a class Leaderboard that keeps each player's best score and ranks the players, with equal scores sharing a rank. Write these methods:
Leaderboard()starts with no players.submit(name, score)records a score forname. A player's best score is the highest score they have ever submitted. Returnsname's best score after this submission.rank_of(name)returnsname's rank: one more than the number of players whose best score is higher thanname's. Players with the same best score share a rank. Returns-1ifnamehas never submitted a score.top_players(k)returns the names of thekplayers with the highest best scores, from highest to lowest. Players with the same best score are listed in alphabetical order. If there are fewer thankplayers, it returns all of them.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
Leaderboard()
submit("ana", 50) returns 50
submit("ben", 70) returns 70
submit("cal", 50) returns 50
submit("ana", 40) returns 50
rank_of("ben") returns 1
rank_of("ana") returns 2
rank_of("cal") returns 2
top_players(2) returns ["ben","ana"]
rank_of("dan") returns -1
ana keeps her best score 50 after submitting 40. Only ben is higher, so ana and cal share rank 2. The top two are ben, then ana, who comes before cal alphabetically. dan never played.
Constraints
0 <= score <= 100nameis1to8lowercase letters.1 <= k <= 6- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.