Use Python: ------------------------------- CVS document download: http://s000.t
ID: 3831204 • Letter: U
Question
Use Python:
-------------------------------
CVS document download: http://s000.tinyupload.com/?file_id=89808774474144421422
Type your codes here:
For this problem you will be dealing with some real-world data: the set of all NCAA American football games from the 2016 football season. The data is stored in an 874-line file called ncaa .csv available on Piazza. A CSV file organizes each "data point" from a data-set on a single line, with the individual variables of the data point as comma-separated values on that line. For example, here is a line from the ncaa.csv file: 09/01/16, 09:00:00 PM, Idaho, 20, Montana State, 17, ESPN3, Kibbie Dome Moscow Idaho This data point represents a football game on September 1st, 2016 between the University of Idado and Montana State University. Idaho won the game 20-17. The game was played in Moscow, Idaho and aired on ESPN3 Every line in the file contains facts about a particular game, always in the order shown below, with commas separating the individual values: 1. date 2. time 3. winning team 4. winning team's points losing team 5- 6. losing team's points 7. TV station that carried the game (or no value if the game was not televised) 8- where the game was played Write a function that creates a dictionary that gives the records of wins and losses) for a set of teams we specify in a list. More specifically, write a function team-records that takes two arguments: the name of the file containing the game data and a list of teams we want to know the records for. The function determines the record for each team given in the list and returns a dictionary that maps team names to their season records written in wins-losses format with a hyphen in between the two numbers. For example, Stanford won 10 of their games and lost 3, so the returned dictionary entry for Stanford would have the key Stanford and the corresponding value for that key would be the string 10-3Explanation / Answer
#!/usr/bin/python
def team_records(filename, teams):
wins = {}
losses = {}
results = {}
if(len(teams) == 0):
return results
for team in teams:
wins[team.lower()] = 0
losses[team.lower()] = 0
for line in open(filename):
record = line.split(",")
currTeam = [record[2], record[4] ]
ret1 = record[2].find("(")
ret2 = record[2].find(")")
if(ret1 != -1 and ret2 != -1):
currTeam.pop(0);
currTeam.insert(0,(record[2].split(" "))[1])
ret1 = record[4].find("(")
ret2 = record[4].find(")")
if(ret1 != -1 and ret2 != -1 ):
currTeam.pop(1)
currTeam.insert(1,(record[4].split(" "))[1])
for team in teams:
if( team.lower() == currTeam[0].lower()):
wins[team.lower()] += 1
if(team.lower() == currTeam[1].lower()):
losses[team.lower()] +=1
for team in teams:
results[team.lower()] = str(wins[team.lower()]) + ":" + str(losses[team.lower()]);
return results
if __name__ == "__main__":
############### Tests ###############
print('Testing team_records for "ncaa.csv", ["Baylor", "Minnesota", "Stanford"]: ')
print(' '*4 + str(team_records('ncaa.csv', ['Baylor', 'Minnesota', 'Stanford'])))
print('Testing team_records for "ncaa.csv", ["Stony Brook", "San Jose State"]: ')
print(' '*4 + str(team_records('ncaa.csv', ['Stony Brook', 'San Jose State'])))
print('Testing team_records for "ncaa.csv", ["Alabama"]: ')
print(' '*4 + str(team_records('ncaa.csv', ['Alabama'])))
print('Testing team_records for "ncaa.csv", ["Akron", "Florida", "Utah", "UCLA"]: ')
print(' '*4 + str(team_records('ncaa.csv', ['Akron', 'Florida', 'Utah', 'UCLA'])))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.