Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The following function open_file() opens a file called 'example.txt' and returns the file pointer. This function is called within main and the file pointer is
The following function open_file() opens a file called 'example.txt' and returns the file pointer. This function is called within main and the file pointer is used to read lines in the file. Create a dictionary called 'dict_of_words that have words as keys and (integar) counters as values. The counter values keep a count of the number of times a word has appeared in the text file. In the end, store the dictionary key,values in a list, sort and print the list on the screen. Note that the counts are not case-sensitive, that is, Word is the same as word or WORd Also, note that your progrom should account for if a(comma) separates two words, eg. food. water, electricity Example Contents of input text file I do not think there is any thrill that can go through the human heart like that felt by the inventor as he sees some creation of the brain unfolding to success such emotions make a man forget food sleep friends love everything Nikola Tesla Output: [('a, 1), [any, 1), ['as, 1), ('brain, 1), ['by', 1), ['can', 1), ['creation', 1), ('do, 1, ['emotions', 1), (everything, 1), [felt, 1), [food, 1), (forget', 1), ('friends', 1) ['go', 1), ('he, 1), (heart), ['human', 1], (T, 1), ('inventor, 1), ['is, I), (1ike', l), [love. 1) (make, I), [man', 1) [ nikola", 1] [not, I), [Of, 1] ('sees, l], [sleep. I), ['some', 1), [Success. 1). ('such, l), [tesla, 1), (that', 2) ('the', 3], [there. 1), [think. I), [thrill. I), (through." 1), (to', 1), unfolding, 1)) Python 3.5 Code 2- def open_fileO: 4 6 def main fpointer openC'example.txt') return fpointer dictlist0 fp open_fileC) #loop to iterate over lines in file 7 10 for key, value in dict_of_words.items: 12 13 14 15 temp Ckey,value) dictlist.append (temp) print(sorted dictlist)) Expand
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