Answered step by step
Verified Expert Solution
Question
1 Approved Answer
This program will demonstrate your ability to work with dictionaries. A starter file called Lab 0 9 P 1 - starter.py has been provide on
This program will demonstrate your ability to work with dictionaries.
A starter file called LabPstarter.py has been provide on Blackboard. Download that file and rename it to LabPpy
The file consists of two functions.
main DO NOT UPDATE THIS FUNCTION. This function reads in the file words.txt and stores the contents in a variable. That data is passed in to the processfile function.
processfilefiletext This function contains a number of TODO comments that will direct you on what needs to be done.
The TODO comments in processfile function indicate to do the following:
Create an empty dictionary.
Create a list of words from the data passed in to the function.
Update the dictionary with the words as keys and the counts of the words as values.
Print the dictionary.
Determine the words with the maximum count, put them in a list, and print the list.
Use the list to help remove all the words with maximum count from the dictionary.
Put the words from the dictionary in a new list, sort it and print that list.
Remove the last line with the pass statement.
There is a sample word list on Blackboard called words.txt It looks like this:
This is a word list
Giving this word list a test
See how this word list works
Test it and see how a word list file works
Using this file, here's the sample output a finished solution should produce:
THIS: IS: A: 'WORD': 'LIST': 'GIVING': 'TEST': 'SEE': 'HOW': 'WORKS': IT: 'AND': 'FILE':
Words with max count of : WORD 'LIST'
Dictionary with max removed: THIS: IS: A: 'GIVING': 'TEST': 'SEE': 'HOW': 'WORKS': IT: 'AND': 'FILE':
Words sorted: A 'AND', 'FILE', 'GIVING', 'HOW', ISIT 'SEE', 'TEST', 'THIS', 'WORKS'
Your program should include the additional text that indicates what the maximum count for the words is and it should include the other leadin text as well.
Submit the program file LabPpy to Blackboard for credit.
# DO NOT UPDATE ANY PART OF THE MAIN FUNCTION
def main:
inputfile openwordstxtr # Open a file for reading
filetext inputfile.readupper # Read all contents and convert to uppercase
processfilefiletext
inputfile.close
def processfiletext:
# TODO: Create an empty dictionary
# TODO: Use the split method to get a list of words from the input parameter
# TODO: Use a for loop to go through the list by
# and count the occurrence of each word. Add or update
# the entries in the dictionary with the wordcount pairs.
# TODO: Print the dictionary
# TODO: Create a list of words with the maximum count
# and print the list.
# TODO: Remove the words with max count from the dictionary
# and print the dictionary.
# TODO: Put all the words in the dictionary in a list, sort it
# and print the list of sorted words.
pass # TODO: Remove this line before submitting to BB
main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started