Assignment 7
More loops and dictionaries
Submit before 11:30 PM Friday November 3
Overview
You will write two functions that practice the use of loops and lists, including 2-dimensional lists (lists of lists). You will also study the use of the dictionary type and use it in a simple word count exercise.
Python Functions and Scripts
This assignment requires use of code in the zipped a7 folder .
Write and test Python functions for each of the following. When writing a function, include a docstring.
(0) Include the exotic_fruits function from lab 8.
- Implement the function pair_sum that takes as parameters a list of distinct integers and a target value n and prints the indices of all pairs of values in the list that sum up to n. If there are no pairs that sum up to n, the function should not print anything. Note that the function does not duplicate pairs of indices. Your solution must use the check_sum function defined in a7.py. Since this function returns a boolean value, you will want to use it in an if-statement. At first, use the print output to study how your function is working. Once it works, comment out the print statements.
- Implement the function repeated that takes as a parameter a two-dimensional list of strings and returns a one-dimensional list containing all the entries that are repeated at least once in the list. The entries in the list returned do not need to be ordered in any particular way. For example, in the list [['dog', 'emu', 'cat'], ['cat', 'emu', 'cat']], the function would return the list ['emu', 'cat'] (or the list ['cat', 'emu'] -- both are valid). The list passed as a parameter should not be altered by the function. To complete this function, you may use a list or a set to keep track of the previous values.
- Study the functions
in the word_counter.py file in the a7 folder. Try running
the commented demo example to see how it works. Once you understand the code,
write a few sentences that summarizes your understanding of the display_counts function.
write
a new function called word_analysis that is based on the display_counts function (copying
the code will give you a good start). Do the following:
- Construct of list of 10 words (all lower case) that you think will distinguish the writing styles between the text in pride.txt and tale.txt.
- Modify your function so that it prints out the word counts for each of the words in the list, each divided by the total number of words in the file.
- Add a try and except to your function so that if the file doesn't exist, it prints a meaningful message before exiting from the function.
- Experiment with your list of 10 words. Write a short summary of your results.
- A statement that summarizes your completion of the assignment. It should contain
the following information:
- Any help or resources that you used in completing the assignment. Be specific!
- A summary of your experience possibly including your approach, difficulties and time you spent.
- For each of your Python scripts or functions:
- A listing of the code
- Running examples that demonstrate that your code works correctly
Deliverables
Create a text file called assn7.txt that contains the following:
Submit your assn7.txt file to D2L.