Question: ). # # ### Problem A1, Loading Cow Data # # First we need to load the cow data from the data file `ps1_cow_data.txt`. #

). #

# ### Problem A1, Loading Cow Data # # First we need to load the cow data from the data file `ps1_cow_data.txt`. # # The file `ps1_cow_data_2.txt` is given as another file that you can read and test from (You are encouraged to write your own cow list files for testing!), but for now, just work with `ps1_cow_data.txt` # # You can expect the data to be formatted in pairs of x,y on each line, where x is the name of the cow and y is a number indicating how much the cow weighs in tons. Here are the first few lines of `ps1_cow_data.txt`: # # Maggie,3 # Herman,7 # Betsy,9 # # You can assume that all the cows have unique names. # # #### Hint: # # If you dont remember how to read lines from a file, check out the online python documentation, which has a chapter on Input and Output that includes file I/O here: https://docs.python.org/3/tutorial/inputoutput.html # # Some functions that may be helpful: # - `str.split` # - `open` # - `file.readline` # - `file.close`

# In[ ]:

def load_cows(filename): """ Read the contents of the given file. Assumes the file contents contain data in the form of comma-separated cow name, weight pairs, and return a dictionary containing cow names as keys and corresponding weights as values.

Parameters: filename - the name of the data file as a string

Returns: a dictionary of cow name (string), weight (int) pairs """ # TODO: Your code here pass

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!