Sort Player Scores by Points

Medium ⏱ 12 min 53% acceptance ★★★★★ 4.6
Implement rank_players(scores), where scores is a list of (player_name, points) tuples. Return a new list sorted by points descending, breaking ties alphabetically by player_name ascending. Use sorted() with a key function.

Examples

Example 1
Input
rank_players([('Zoe', 10), ('Amit', 15), ('Bo', 15)])
Output
[('Amit', 15), ('Bo', 15), ('Zoe', 10)]
Explanation

Amit and Bo tie on 15 points and are ordered alphabetically; Zoe trails on 10.

Constraints

  • scores is a non-empty list of (str, int) tuples.
  • Must not mutate the input list.

Topics

TuplesSorting

Companies

NetflixAdobe

Hints

Hint 1

sorted(scores, key=lambda t: (-t[1], t[0])) sorts by descending points, then ascending name.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.