← Back to challenges
← Time Around the World·Maximum Product of Digits →

Who Won the League?

PythonHardarraysdata_structuressorting

Instructions

The 2019/20 season of the English Premier League (EPL) saw Liverpool FC win the title for the first time despite their bitter rivals, Manchester United, having won 13 titles!

Create a function that receives an alphabetically sorted array of the results achieved by each team in that season and the name of one of the teams. The function should return a string giving the final position of the team, the number of points achieved and the goal difference (see examples for precise format).

The results table is given in the following format:

TeamPlayedWonDrawnLostG/Diff
Arsenal381414108
Aston Villa389821-26
Bournemouth389722-26
Brighton3891415-15
Burnley3815914-7
Chelsea382061215
Crystal Palace38111017-19
Everton38131015-12
Leicester City381881226
Liverpool38323352
Manchester City38263967
Manchester Utd381812830
Newcastle38111116-20
Norwich385627-49
Sheffield Utd381412120
Southampton3815716-9
Tottenham3816111114
Watford3881020-28
West Ham3810919-13
Wolves381514911

Examples

table = [
  ["Arsenal", 38, 14, 14, 10, 8],
  ["Aston Villa", 38, 9, 8, 21, -26],
  ...
  ...
  ["West Ham", 38, 10, 9, 19, -13],
  ["Wolves", 38, 15, 14, 9, 11]
]

EPLResult(table, "Liverpool")
  ➞ "Liverpool came 1st with 99 points and a goal difference of 52!"

EPLResult(table, "Manchester Utd")
  ➞ "Manchester Utd came 3rd with 66 points and a goal difference of 30!"

EPLResult(table, "Norwich")
  ➞ "Norwich came 20th with 21 points and a goal difference of -49!"

Notes

  • Score 3 points for a win and 1 point for a draw.
  • If teams are tied on points, their position is determined by better goal difference.
  • The input table should be considered immutable - do not make any changes to it!
python3
Loading editor…
⌘ ↵ to run
Walks through the solution with reasoning and edge cases.