Save the Leaderboard

Python for AIHard+10 XP+10

Save the Leaderboard

A game keeps scores as a simple dictionary, like {"Ana": 40, "Ben": 55}. To share it, you'll save a leaderboard as JSON in this nested shape:

json
1{"players": [{"name": "Ben", "points": 55}, {"name": "Ana", "points": 40}]}

Write save_leaderboard(path, scores) that:

  1. builds that dictionary, with one {"name": ..., "points": ...} dictionary per player
  2. orders the "players" list by points, highest first. If two players have the same points, the name that comes first A to Z goes first.
  3. saves it to path with json.dump
  4. reads the file back with json.load and returns the "players" list from what it read

If scores is empty, save {"players": []} and return []. Don't change the scores dictionary you were given.

Examples

path = "board.json"
scores = {"Ana": 40, "Ben": 55}
returns [{'name': 'Ben', 'points': 55}, {'name': 'Ana', 'points': 40}]
path = "solo.json"
scores = {"Kim": 7}
returns [{'name': 'Kim', 'points': 7}]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run