Editor

Soccer League Table

#639 · Working with data · Advanced · Mini-project · 240 XP · about 100 min

Build the table of a soccer league from the season's results. Each result is a line such as 'Red Lions 3-1 Blue Sharks': the home team, the score, and the away team. A win is worth 3 points, a draw 1 point and a loss nothing.

The table is a dictionary that maps each team to a row. The starter code gives you new_row(), which makes an empty row: {'played': 0, 'won': 0, 'drawn': 0, 'lost': 0, 'for': 0, 'against': 0, 'points': 0}, where for and against are goals scored and goals let in. Use it for every new team, so the rows always have their columns in this order.

Parts

  • Part 1 parse_result(line): return (home, home_goals, away_goals, away), for example ('Red Lions', 3, 1, 'Blue Sharks'). Team names may contain spaces and digits, but the score is the only word made of a number, a dash and a number.
  • Part 2 record(table, result): add one result from Part 1 to the table, for both teams, adding a team with new_row() the first time it plays (the home team first). It changes table and returns nothing.
  • Part 3 build_table(lines): a new table made from a list of result lines, using Parts 1 and 2.
  • Part 4 standings(table): the team names in league order: most points first, then the bigger goal difference (for - against), then more goals scored, then alphabetical order.
  • Part 5 format_table(table): a list of lines in league order, each like '1. Red Lions 7 pts (+4)': the position, the team, the points and the goal difference with its sign. Teams level on points, goal difference and goals scored share a position, and the next team's position counts every team above it, so positions can go 1, 2, 2, 4.

Examples

parse_result('Red Lions 3-1 Blue Sharks')   # returns ('Red Lions', 3, 1, 'Blue Sharks')
table = build_table(['Lions 3-0 Sharks', 'Eagles 1-0 Sharks', 'Lions 0-1 Eagles'])
table['Lions']
# {'played': 2, 'won': 1, 'drawn': 0, 'lost': 1, 'for': 3, 'against': 1, 'points': 3}
standings(table)        # returns ['Eagles', 'Lions', 'Sharks']
format_table(table)     # returns ['1. Eagles 6 pts (+2)', '2. Lions 3 pts (+2)', '3. Sharks 0 pts (-4)']

Constraints

  • A season has at most 1,000 results, and team names never contain a dash.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.