Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Jupyter notebook coursenotes/Topic2/Topic2_exercise2.ipynb

Views: 162
Kernel: Anaconda (Python 3)

Lab exercise 2:

The file PremierLeague2011.txt contains a list of all fixtures played in the Barclays Premier League between 13th August 2011 and 12th December 2011.

The data in the file looks like (this example is the first line in the file):


13/08/2011   2  19   1   2  A

where:

ItemDescription
13/08/2011is the date that the match was played (not needed)
2 | is the *team code* of the home team 19 | is the *team code* of the away team 1 | is the number of goals that the home team scored 2 | is the number of goals that the away team scored A | is the final result (‘H’=home win, ‘D’=draw, ‘A’=away win)

So, e.g. in this case, on 13th August 2011, team 2 (Blackburn, at home) lost 1-2 to team 19 (Wolves, away)

** Note ** To make this assessment easier, the teams have been ordered alphabetically and each team has been given a team code which is a positive integer from 0 to 19 as follows:


0: "Aston Villa"
1: "Arsenal"
2: "Blackburn"
3: "Bolton" 
4: "Chelsea"
5: "Everton"
6: "Fulham"
7: "Liverpool"
8: "Man City"
9: "Man Utd"
10: "Newcastle"
11: "Norwich"
12: "QPR"
13: "Stoke"
14: "Sunderland"
15: "Swansea"
16: "Tottenham"
17: "West Brom"
18: "Wigan"
19: "Wolves"
  1. Create a PremierLeagueTeam (or similar named) class

  2. Create instances of this class for all 20 teams

  3. Add methods to the class to to keep track of each team’s match statistics, i.e.:

  • total number of matches played

  • total number of matches won

  • total number of matches drawn

  • total number of matches lost

  • total number of goals scored

  • total number of goals conceded

  • total number of points (where a win is worth 3 points and a draw is worth 1 point)

  1. Print out a copy of the Premier League table as it stood on 12th December 2011. Note that teams with the same number of points are ordered by goal difference. The table will look like:


(First 3 lines)
        Team  PL   W   D   L  GF  GA PTS
    Man City  15  12   2   1  49  15  38
     Man Utd  15  11   3   1  35  14  36

(Last 2 lines)
   Blackburn  15   2   4   9  22  34  10
      Bolton  15   3   0  12  20  36   9
class team(object): def __init__(self,code, name):#define class for team information self.name=str(name) self.code=int(code) self.played=0 self.won=0 self.lost=0 self.draw=0 self.scored=0 self.conceded=0 self.points=0 def playgame(self, team, scored, conceded):#method for playing a game self.played +=1 #increment played counter by 1 self.scored +=scored #increment goals scored counter by number of goals scored self.conceded += conceded #increment goals conceded counter by goals conceded #calculate game outcome and increment won/lost/draw and conceded appropriately if scored>conceded: self.won += 1 self.points += 3 elif scored<conceded: self.lost +=1 elif scored==conceded: self.draw += 1 self.points += 1 teamlist=[] #make empty list for team names teamnames=("Aston Villa", "Arsenal", "Blackburn", "Bolton", "Chelsea", "Everton", "Fulham", "Liverpool", "Man City", "Man Utd", "Newcastle", "Norwich", "QPR", "Stoke", "Sunderland", "Swansea", "Tottenham", "West Brom", "Wigan", "Wolves") for i, name in enumerate(teamnames):#add team names and number (using enumerate) into teamlist TeamList=(i, name) teamlist.append(TeamList) TeamInfo=[] #new empty lists for instances of team class for value in teamlist: element=team(value[0],value[1])#values 0&1 from 'teamlist' are the team name and code TeamInfo.append(element) Inf=open("PremierLeague2011.txt")#open file of game data gamedata=[]#empty list for game data for line in Inf: gamedata=line.split()#split game data file by line TeamInfo[int(gamedata[1])].playgame(TeamInfo[int(gamedata[1])], int(gamedata[3]), int(gamedata[4]))#change hometeam data for given game TeamInfo[int(gamedata[2])].playgame(TeamInfo[int(gamedata[2])], int(gamedata[4]), int(gamedata[3]))#change awayteam data for given game Inf.close() #close file print('%-12s %-3s %-3s %-3s %-3s %-3s %-3s %-3s '%("Team", "PL", "W", "D", "L", "GF", "GA", "PTS"))#print headings TeamInfo.sort(key=lambda x: (x.points, x.scored-x.conceded), reverse=True)#sort teams according to points first, then goal difference. reverse=True gives sorting from high to low for element in TeamInfo: print('%-12s %-3d %-3d %-3d %-3d %-3d %-3d %-3d ' %(element.name, element.played, element.won, element.draw, element.lost, element.scored, element.conceded, element.points))#print sorted team information
Team PL W D L GF GA PTS Man City 15 12 2 1 49 15 38 Man Utd 15 11 3 1 35 14 36 Chelsea 15 10 1 4 33 18 31 Tottenham 14 10 1 3 30 18 31 Arsenal 15 9 2 4 31 23 29 Liverpool 15 7 5 3 18 13 26 Newcastle 15 7 5 3 21 19 26 Stoke 15 6 3 6 16 24 21 Aston Villa 15 4 7 4 18 19 19 Norwich 15 5 4 6 24 28 19 Swansea 15 4 5 6 16 20 17 Everton 14 5 1 8 15 18 16 QPR 15 4 4 7 15 26 16 Fulham 15 3 6 6 16 18 15 West Brom 15 4 3 8 14 23 15 Sunderland 15 3 5 7 18 18 14 Wolves 15 4 2 9 16 28 14 Wigan 15 3 3 9 14 29 12 Blackburn 15 2 4 9 22 34 10 Bolton 15 3 0 12 20 36 9