Editor

Rank the Leaderboard

#319 · Sorting and searching · Intermediate · 75 XP · about 15 min

A game's final scores are a list of (name, score) pairs. Write rank_players(scores, method) that returns the leaderboard as a list of (rank, name, score) tuples: highest score first, and players with the same score in alphabetical order of name.

Players with equal scores always share a rank, and method says how ranks are counted:

  • 'standard': 1 plus the number of players with a higher score. Scores 90, 80, 80, 70 get ranks 1, 2, 2, 4.
  • 'modified': the number of players with a score at least as high, the player included. Scores 90, 80, 80, 70 get ranks 1, 3, 3, 4.
  • 'dense': 1 plus the number of different scores higher than this one. Scores 90, 80, 80, 70 get ranks 1, 2, 2, 3.

Examples

scores = [('Ana', 80), ('Ben', 90), ('Cy', 80), ('Dee', 70)]
rank_players(scores, 'standard')
# returns [(1, 'Ben', 90), (2, 'Ana', 80), (2, 'Cy', 80), (4, 'Dee', 70)]
rank_players(scores, 'dense')
# returns [(1, 'Ben', 90), (2, 'Ana', 80), (2, 'Cy', 80), (3, 'Dee', 70)]

Constraints

  • 0 <= len(scores) <= 10000
  • Names are all different; compare them with Python's normal string order.
  • Scores are whole numbers.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.