Answered step by step
Verified Expert Solution
Link Copied!

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 Lab09P1-starter.py has been provide on Blackboard. Download that file and rename it to Lab09P1.py.
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 process_file function.
process_file(file_text) This function contains a number of TODO comments that will direct you on what needs to be done.
The TODO comments in process_file 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': 3,'IS': 1,'A': 3, 'WORD': 4, 'LIST': 4, 'GIVING': 1, 'TEST': 2, 'SEE': 2, 'HOW': 2, 'WORKS': 2,'IT': 1, 'AND': 1, 'FILE': 1}
Words with max count of 4: ['WORD', 'LIST']
Dictionary with max removed: {'THIS': 3,'IS': 1,'A': 3, 'GIVING': 1, 'TEST': 2, 'SEE': 2, 'HOW': 2, 'WORKS': 2,'IT': 1, 'AND': 1, 'FILE': 1}
Words sorted: ['A', 'AND', 'FILE', 'GIVING', 'HOW', 'IS','IT', '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 lead-in text as well.
Submit the program file Lab09P1.py to Blackboard for credit.
# DO NOT UPDATE ANY PART OF THE MAIN FUNCTION
def main():
input_file = open('words.txt','r') # Open a file for reading
file_text = input_file.read().upper() # Read all contents and convert to uppercase
process_file(file_text)
input_file.close()
def process_file(text):
# 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 1-by-1
# and count the occurrence of each word. Add or update
# the entries in the dictionary with the word/count 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

10th Edition

0137916787, 978-0137916788

More Books

Students also viewed these Databases questions