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
Write and test Python functions for each of the following. When writing a function, include a docstring.
- Implement the function pairSum 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.
- Implement the function unique that takes as a parameter a two-dimensional list and returns a one-dimensional list containing all the unique entries in the list. The entries in the list returned do not need to be ordered in any particular way. For example, in the list [[1, 0, 1], [0, 1, 0]] there are only two unique values (0 and 1) so the function would return the list [0, 1] (or the list [1, 0] — both are valid). The list passed as a parameter should not be altered by the function.
- Study the count_word function in the table.py file and use it in another function (called display_counts) to print your selection of word counts from a text file (e.g. pride.txt or tale.txt). The function display_counts should take one parameter, a string that represents the name of the file. You may choose what word counts to display (e.g. words longer than 10 characters, or a specific list of words). Does your display show meaningful differences between the Jane Austin novel and the Charles Dickens novel?
- A statement, possibly very brief, that summarizes your completion of the assignment
- For each of your Python scripts or functions:
- A listing of the code
- Running examples that demonstrate that your code works correctly
>>> pairSum([3, 6, 8, 10, 11], 14) 0 4 1 2 >>> pairSum([3, 9, 12], 44) >>> pairSum(, 3) >>> >>> unique([[1, 5, 2, 1], [1, 4, 3, 5], , ]) [1, 5, 2, 4, 3] >>>
Note that the display of the last function is up to you!
Create a text file called assn7.txt that contains the following: