Question: Python 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.

Python

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 teams points 5. losing team 6. losing teams 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-3.

Opening a file and reading it line-by-line is pretty easy in Python. Suppose the variable filename contains the name of a file we wish to open. We can write this code to accomplish this task:

for line in open(filename): # the variable line is a string with the "current" line of the file # all data-processing code goes here inside the for-loop 

Given a line from the file, we need to split it apart into its constituent eight parts as defined above. Luckily, the split() method can help us here. Instead of splitting each line into parts using spaces, we want to split each line using the commas. This is as simple as putting a comma inside of the parentheses:

parts = line.split(,) # the quotation marks around the comma are reqd # the parts[] list now contains the 8 strings from a line of the file 

As you read in each line, first check if the winner or loser of the game is a team whose record we care about. Remember that the second argument to the team records() function is a list of team names. If the winner or loser of a particular game is a team in the list, we need to pay attention to that line of the file. You might wish to

create two helper dictionaries: one that maps team names to the number of wins for that team, and another that mapsteamnamestothenumberoflossesforthatteam.Forexample,supposewehavedictionarieswins = {} and losses = { } for this purpose. Also assume that we manage to get the name of the winning team for a particular game into a variable called winner. Then we can simply write wins[winner] += 1 to indicate that we found another game that this particular team won.

One little issue you will need to deal with is that some team names are preceded by a ranking. For instance, Stanford is ranked 7th, so (7) (including the space) precedes the universitys name. You will need to find a way to ignore the rankings, but heres a hint: use the index() method to find the position of the space after the name and use string slicing to extract the universitys name.

Once the two dictionaries called wins and losses have been assembled, we can then build a new dictionary with the team records. This is the dictionary that our function will ultimately return. Lets call this dictio- nary results. For instance, if Stanford were one of the team names in the list of teams we care about, then results[Stanford] would be set to the string 10-3, where the 10 comes from wins[Stanford] and the 3 comes from losses[Stanford]. Can you figure how we could build such a dictionary for an arbitrary list of team names? (You can safely assume that every name given in the list is a valid team name that appears in the CSV file.)

When your work is graded, a different set of (fictional) scores will be used, but the file will still have exactly 874 lines. The examples below were generated using the ncaa.csv file provided on Piazza. Make sure you put this CVS file in same folder as your homework2.py file. Note that the order of the keys in the returned dictionary is not important and will have no bearing on grading.

Examples:

Python For this problem you will be dealing with some real-world data:

def team_records(filename, teams): wins = {} losses = {} results = {} for line in open(filename): pass # delete this line and put your own code here   return results 
if __name__ == "__main__": 
 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"]: ') 

Function Call team records ncaa. csv Baylor Stanford' Minnesota Return Value 1' Baylor 7-6 Minnesota 9-4 Stanford 10-3 Function Call team-records ncaa. csv Stony Brook San Jose State Return Value 1' Stony Brook 7 0-1 San Jose State 4-8' Function Call: team records ('ncaa. csv', ['Alabama' Return value: Alabama' B 14-1 Function Call team records ncaa. csv Akron Florida Utah UCLA' Return Value UCLA' 4-8 Florida 9-4 Utah 9-4 Akron 5-7

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!