Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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) 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 ("T", **", "m", "having, some, headache"). 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 kvalue. Please provide the algorithmic complexity of your code. 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) 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 ("T", **", "m", "having, some, headache"). 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 kvalue. Please provide the algorithmic complexity of your code
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