Assignment 5: Lists, Dictionaries, and Files
Due by: Friday, October 6, 2023 at 11:59 p.m.
Your mission, should you choose to accept it, is to implement six functions, using Python features covered in Chapters 4 and 5. All of these functions should be saved in a file called assignment5.py. The file must contain the functions, but it doesn't need to have any testing code included (though you should absolutely should test your code).
Each of these functions returns a value (usually a list). None of them contain any print() statements.
Some of the functions use files. The following two files are used in the examples. Please download them to check your work. Feel free to use other text files for testing purposes as well.
Specification
Below are the headers for the six functions, along with a description of their behavior and sample output.
- def fibonacci(n):
Return a list containing the first n values in the Fibonacci sequence. The Fibonacci sequence begins with the numbers 1 and 1. Each subsequent term is generated by adding the two previous terms together. For this function, you can always assume that the value of n will be at least 2.
Hint: Make a new list containing [1, 1]. Then, append the sum of the previous two terms to this list until you have enough terms.
Sample Use
print (fibonacci(10))
Sample Output
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
- def removeDuplicates(values):
Return a new list containing the values in the list stored in values, except that all duplicate values have been removed.
Hint: Make an empty list and add each item in the original list into it unless the new list already contains it.
Sample Use
values = [17, 9, 10, 4, 2, 10, 16, 20, 11, 9] print (removeDuplicates(values))
Sample Output
[17, 9, 10, 4, 2, 16, 20, 11]
- def mergeSorted(list1, list2):
Given lists list1 and list2, which both contain values already in sorted order, copy the values of these lists into a new list in sorted ordered. You may not use any sort() functions or methods.
Hint: Make an empty list and keep two integer indexes: one for the first list and one for the second. As long as the indexes are both legal (not too large for their respective lists), append the smaller value into the new list. Afterwards, copy whatever's left from list1 and then whatever's left from list2. The simplest solution to this problem uses three separate while loops.
Sample Use
list1 = [7, 15, 23, 24, 25, 27, 37, 44, 53, 60] list2 = [6, 11, 15, 22, 32, 34] print (mergeSorted(list1, list2))
Sample Output
[6, 7, 11, 15, 15, 22, 23, 24, 25, 27, 32, 34, 37, 44, 53, 60]
- def longestWord(filename):
Return the longest word in the file whose name is given by filename. The definition of a word is any sequence of characters separated by whitespace. For the purposes of this function, punctuation can be included as part of a word.
Sample Use
print (longestWord('gettysburg.txt'))
Sample Output
battle-field
- def occurrences(filename):
Return a dictionary whose keys are all the words in a file whose name is filename and whose values are the number of times each word occurs. The definition of a word is the same as for the previous function.
Sample Use
print (occurrences('gettysburg.txt'))
Sample Output
{'Four': 1, 'score': 1, 'and': 6, 'seven': 1, 'years': 1, 'ago': 1, 'our': 2, 'fathers': 1, 'brought': 1, 'forth': 1, 'on': 2, 'this': 3, 'continent,': 1, 'a': 7, 'new': 2, 'nation,': 3, 'conceived': 2, 'in': 4, 'Liberty,': 1, 'dedicated': 3, 'to': 8, 'the': 9, 'proposition': 1, 'that': 13, 'all': 1, 'men': 1, 'are': 3, 'created': 1, 'equal.': 1, 'Now': 1, 'we': 8, 'engaged': 1, 'great': 3, 'civil': 1, 'war,': 1, 'testing': 1, 'whether': 1, 'or': 2, 'any': 1, 'nation': 2, 'so': 3, 'dedicated,': 1, 'can': 5, 'long': 2, 'endure.': 1, 'We': 2, 'met': 1, 'battle-field': 1, 'of': 5, 'war.': 1, 'have': 5, 'come': 1, 'dedicate': 2, 'portion': 1, 'field,': 1, 'as': 1, 'final': 1, 'resting': 1, 'place': 1, 'for': 5, 'those': 1, 'who': 3, 'here': 5, 'gave': 2, 'their': 1, 'lives': 1, 'might': 1, 'live.': 1, 'It': 3, 'is': 3, 'altogether': 1, 'fitting': 1, 'proper': 1, 'should': 1, 'do': 1, 'this.': 1, 'But,': 1, 'larger': 1, 'sense,': 1, 'not': 5, '--': 7, 'consecrate': 1, 'hallow': 1, 'ground.': 1, 'The': 2, 'brave': 1, 'men,': 1, 'living': 1, 'dead,': 1, 'struggled': 1, 'here,': 2, 'consecrated': 1, 'it,': 1, 'far': 2, 'above': 1, 'poor': 1, 'power': 1, 'add': 1, 'detract.': 1, 'world': 1, 'will': 1, 'little': 1, 'note,': 1, 'nor': 1, 'remember': 1, 'what': 2, 'say': 1, 'but': 1, 'it': 1, 'never': 1, 'forget': 1, 'they': 3, 'did': 1, 'here.': 1, 'us': 3, 'living,': 1, 'rather,': 1, 'be': 2, 'unfinished': 1, 'work': 1, 'which': 2, 'fought': 1, 'thus': 1, 'nobly': 1, 'advanced.': 1, 'rather': 1, 'task': 1, 'remaining': 1, 'before': 1, 'from': 2, 'these': 2, 'honored': 1, 'dead': 2, 'take': 1, 'increased': 1, 'devotion': 2, 'cause': 1, 'last': 1, 'full': 1, 'measure': 1, 'highly': 1, 'resolve': 1, 'shall': 3, 'died': 1, 'vain': 1, 'under': 1, 'God,': 1, 'birth': 1, 'freedom': 1, 'government': 1, 'people,': 3, 'by': 1, 'perish': 1, 'earth.': 1}
- def misspellings(textFile, wordFile):
Return a list containing all the misspelled words in the file whose name is given by textFile. For the purposes of this function, a word is considered misspelled if its lowercase version doesn't occur in the the file whose name is given by wordFile.
Hint: Read in all the words in wordFile and store into a list of words. Each word is lowercase and given on a separate line. If you put each line into your list of words, call the strip() method on each line to remove whitespace (specifically, the newline character) from each line. When you loop through all the words in textFile, call the lower() method on each one to get a lowercase version of that word. For more information about these and other string methods, look here.
Sample Use
print (misspellings('gettysburg.txt', 'words.txt'))
Sample Output
['years', 'fathers', 'continent,', 'nation,', 'conceived', 'liberty,', 'dedicated', 'created', 'equal.', 'engaged', 'war,', 'testing', 'nation,', 'conceived', 'dedicated,', 'endure.', 'battle-field', 'war.', 'portion', 'field,', 'resting', 'lives', 'live.', 'fitting', 'this.', 'but,', 'larger', 'sense,', '--', '--', '--', 'ground.', 'men,', 'living', 'dead,', 'struggled', 'here,', 'consecrated', 'it,', 'detract.', 'note,', 'here,', 'here.', 'living,', 'rather,', 'dedicated', 'unfinished', 'nobly', 'advanced.', 'dedicated', 'remaining', '--', 'honored', 'increased', '--', 'highly', '--', 'nation,', 'god,', '--', 'government', 'people,', 'people,', 'people,', 'earth.']
Turn In
Upload assignment5.py
to Blackboard.
All work must be submitted before Friday, October 6, 2023 at 11:59 p.m. unless you are going to use a grace day.
All work must be done individually. You may discuss general concepts with your classmates, but it is never acceptable for you to look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.
Grading
Your grade will be determined by the following weights:
Category | Weight |
---|---|
fibonacci() | 10% |
removeDuplicates() | 10% |
mergeSorted() | 20% |
longestWord() | 20% |
occurrences() | 20% |
misspellings() | 20% |
Under no circumstances should any student look at the code written by another student. Tools will be used to detect code similarity automatically.