Question: Write functions that do the same activity of counting unique words, but use three different data structures (list, dictionaries, sets). Also, write a separate function
Write functions that do the same activity of counting unique words, but use three different data structures (list, dictionaries, sets). Also, write a separate function for problem 5 that uses the dictionary that you developed.
1. Read File & Write Word Count: Compose a function named wordcount(in_file, out_file)that reads text from an input file and writes to output file the number of words in the text. For this exercise, a word is a sequence of non-whitespace characters that is surrounded by whitespace. The out_file will have two fields: in_file, word_count
2. List: Keep the unique words found in the file in a list after you read the words. Make sure you understand the implementation completely by tracing the program using the Python Tutor. Now you write out the (word, word count) to the file for all the unique words. Make sure that the words are written out in upper case.
3. Dictionary: Keep the unique words in a dictionary instead of a list, where the key is the word while the value is the number of times the word occurs in the text. You process words from standard input and check if they are already in the dictionary of unique words. If they are found, you increment the value associated with the word. If not, you add the word to the dictionary as a new key and assign the key a value of 1 indicating that you have seen the word one time. Make sure that the words are written out as capitalized.
4. Set: The unique words are key in a set. While reading words from standard input, you check if the words already exist in the set. If they do, you dont need to do anything. If not, you need to add it to the set. Make sure that the words are written out as lower case. 5. Maximum and Minimum Count Words: For the dictionary version, write a function that prints out the (key, value) pair with the minimum count and the and (key, value) pair with the maximum count. This would represent the words that occurred the most and least amount of times.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
