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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started