Match Scores to Players

Python for AIHard+10 XP+10

Match Scores to Players

A quiz league keeps two files. One lists the players. The other is a log of points, written down each time someone scores. The two files only share an id column.

filecolumns
playersid, name (in any order, maybe with extra columns)
pointsid, points (in any order, maybe with extra columns)

Task: write player_totals(players_path, players_text, points_path, points_text). It returns a dictionary mapping each player's name to their total points, as an int.

  • A player can appear in the points log many times, or never. A player who never scored has a total of 0.
  • Ignore points rows whose id doesn't belong to any player.
  • Every points value is a whole number, possibly negative (penalties).
  • Names are unique. The order of the keys doesn't matter.

The starter code includes write_text and already uses it to save both files: players_text into players_path, and points_text into points_path. import csv is already at the top.

Examples

players_path = 'players.csv'
players_text = 'id,name\n1,ada\n2,bo\n'
points_path = 'points.csv'
points_text = 'id,points\n1,5\n2,3\n1,4\n'
returns {'ada': 9, 'bo': 3}
players_path = 'players2.csv'
players_text = 'name,id\ncy,7\ndi,8\n'
points_path = 'points2.csv'
points_text = 'points,id,round\n10,8,1\n'
returns {'cy': 0, 'di': 10}

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