Description
Jay Tang
Reading
Read Chapter 6 in Introduction to Computing using Python: An Application Development Focus, Second Edition by Ljubomir Perković.
Logistics
You need to do this assignment on a computer which has Python 3 installed on it. Python 3.8.5 download page can be found here.
A submission that includes code which does not run will not get any points for the part unless specifically documented reason of the error.
Assignment
1. (70pt) Dictionary
a. (35pt) English dictionary
Write a function lookupWord(filename) that takes as a parameter a string representing the file name. Each line of the file contains a word followed by three colons (:::) followed by the word’s definition. Assume the file won’t contain multiple entries for the same word. The function reads the words and associated definitions from the file and repeatedly allows the user to enter a word, printing the corresponding definition in response. If the word is not in the file, the function prints Word Not Found in response to that query. The function stops when the user presses enter without typing any text.
I suggest make the word lower case before adding it to the dictionary as a key. You don’t want your dictionary to be case sensitive.
You can find an example dictionary.txt file in the submission folder.
b. (35pt) Index
At the end of this and other textbooks, there usually is an index that lists the pages where a certain word appears. In this problem, you will create an index for a text but, instead of page number, you will use the line numbers. Implement function index(filename, words) that takes as input the name of a text file and a list of words. For every word in the list, your function will find the lines in the text file where the word occurs and print the corresponding line numbers (where the numbering starts at 1). You should open and read the file only once.
You can find python.txt and raven.txt in the submission folder. Note that punctuations in python.txt is limited to only comma and period, while in raven.txt there are more punctuations. Remember to remove those punctuations when parsing for the words. Your output format does not need to match exactly as shown in the example. But the indexes should all be correct.
2. (30pt) Set
a. (15pt) Duplicates
Write a function hasDuplicates(lst) that takes a list as a parameter and returns True if there are duplicates in the list and False otherwise.
b. (15pt) Unique
Write a function unique() that will keep asking user for inputs and return a list of unique inputs from the user. Use can end the input by hitting enter.
Submission
Submit the assignment using Assignment 5 folder. Submit only a single python file using your name as file name (e.g. Jay_Tang_Assign_5.py).
Reviews
There are no reviews yet.