Multi-Level Scoreboard Update

Hard ⏱ 18 min 29% acceptance ★★★★★ 4.7
A scoreboard is a nested dict: {team_name: {player_name: points}}. Write a function add_points(board, team, player, points) that adds points to player's score under team, creating the team dict and/or player entry if they do not already exist. Mutate board in place and also return it.

Examples

Example 1
Input
board = {'Red': {'Ravi': 10}}; add_points(board, 'Red', 'Ravi', 5); board
Output
{'Red': {'Ravi': 15}}
Explanation

Existing player gains 5 more points.

Example 2
Input
board = {}; add_points(board, 'Blue', 'Sam', 20); board
Output
{'Blue': {'Sam': 20}}
Explanation

Both team and player are newly created.

Constraints

  • Must mutate board in place.
  • Must handle missing team and/or missing player gracefully.

Topics

Dictionariesnested dictsleaderboard

Companies

NetflixUber

Hints

Hint 1

`board.setdefault(team, {})` gets or creates the team's dict.

Hint 2

Then `team_dict[player] = team_dict.get(player, 0) + points`.

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