File I/O and the Dictionary Mapping Type

Introduction. This homework is intended to review and refresh your knowledge of Python’s mapping type, the dictionary, and Python’s file manipulation functionality. Instructions. Create a module named hw2.py. Below is the spec for eight functions. Implement them and upload your module to the D2L dropbox. Testing. Download hw2_test.py and put it in the same folder as your hw2.py module. Run it from the command line to see your current correctness score. Each of the 8 functions is worth 12.5% of your correctness score. You can examine the test module in a text editor to understand better what your code should do. The test module is part of the spec. The test file we will use to grade your program will be different and may uncover failings in your work not evident upon testing with the provided file. Add any necessary tests to make sure your code works in all cases.
Documentation. Your module must contain a header docstring containing your name, your section leader’s name, the date, ISTA 350 Hw2, and a brief summary of the module. Each function must contain a docstring. Each function docstring should include a description of the function’s purpose, the name, type, and purpose of each parameter, and the type and meaning of the function’s return value. Grading. Your module will be graded on correctness, documentation, and coding style. Code should be clear and concise. You will only lose style points if your code is a real mess. Include inline comments to explain tricky lines and summarize sections of code (not necessary on this assignment). Collaboration. Collaboration is allowed. You are responsible for your learning. Depending too much on others will hurt you on the tests. “Helping” others too much harms them in reality. Cite any sources/collaborators in your header docstring. Leaving this out is dishonest. Resources. https://docs.python.org/3.4/tutorial/index.html https://docs.python.org/3.4/tutorial/datastructures.html#dictionaries https://docs.python.org/3/library/stdtypes.html#mapping-types-dict ltranslate: (short for list translate). This list function is a slightly easier analog of Python’s translate method for strings. This function takes two arguments, a list and a dictionary. For every key in the dictionary, it replaces any occurrences of the key in the list with the key’s corresponding value in the dictionary. For instance, if the list is [‘cat’, ‘dog’] and the dictionary is {‘cat’:‘fish’, ‘dog’:‘rabbit’}, after execution the list is [‘fish’, ‘rabbit’]. See the test module for more examples. word_count: This function has two parameters – a filename and dictionary. The dictionary has a default argument of None. If the dictionary is None, initialize it to the empty dictionary. Read the file, using each word in it as a key in lowercase. Add the key to the dictionary if necessary; otherwise increment the count for that word. Store all words in lowercase. Assume the file has no punctuation. Return the dictionary. If calling code passes in a pre-existing dictionary, returning the updated dictionary, while unnecessary, harms nothing. average_wc: The average or mean of a collection of numbers is the sum of the numbers divided by the number of numbers. This function takes a word count dictionary and returns the average word count. If the dictionary is empty, it returns 0. max_wc: This function takes a word count dictionary and returns the maximum word count. dreverse: (short for dictionary reverse). This function takes a forward dictionary as its sole argument and creates and returns a reverse dictionary. You may assume that all values in the forward dictionary are immutable. Each value in the forward dictionary will be a key in the reverse dictionary. Since values may appear more than once in the forward dictionary, the values in the reverse dictionary will be lists. For example, if the forward dictionary is {‘cat’:‘dog’, ‘rabbit’:‘dog’}, the function returns {‘dog’:[‘cat’, ‘rabbit’]}. bird_weights: This function takes a single argument, a filename. Each line of the file contains the name of a species of bird, followed by a colon, followed by a sequence of floats separated by spaces. You may assume that the file has at least one line in it and that all lines follow this format. Read the data in the file into a dictionary that contains the capitalized names of the birds as keys that map to lists of weights. A given species of bird may have more than one line in the file. median: This function takes a list as an argument and returns its median, without changing the list. If the list is empty, return None. Otherwise, the median is defined as the middle number of a sorted sequence of numbers if the length of the sequence is odd; the mean of the middle two numbers if the length of the sequence is even. median_bird_weights: This function takes a bird weight dictionary and returns a new dictionary that maps each bird species to the median of the list of weights for that bird.
Powered by