Question: 2. Write a program that reads a text file from the same directory that your main program is stored and then perform some operations

2. Write a program that reads a text file from the same directory that your main program is stored and then perform some operations on it. Concretely: 1) 2) 3) 4) Your program reads from the console (1) the name of the file ("test.txt" for instance) and (2) an integer k. Your program then reads the file. While reading the file (NOT after the file has been read), you need to count the occurrence of each word. Note that "cat" and "cats" could be considered as two different words. In addition, special characters such as ./*? are all separate words. For instance, "I'm having some headache" has six words ["I", "", "m", "having", "some", "headache"]. 66699 Once the file-reading is completed, your program should output the EXACT (not approximate) the top k most frequent words. In case there are ties in terms of word frequencies, return all. You must ensure that your program runs as efficiently as possible because, in reality, your program might be reading a file with billions of words and a very large k value. Please provide the algorithmic complexity of your code.
Step by Step Solution
There are 3 Steps involved in it
from collections import Counter import string def getfilename filenameinputEnter the Filename return filename def readfilefilename Reading file with openfilenametxtr as f datafread Strip punctuation i... View full answer
Get step-by-step solutions from verified subject matter experts
