2. Write a program that reads a text file from the same directory that your main...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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. 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.
Expert Answer:
Answer rating: 100% (QA)
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 the full answer
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Posted Date:
Students also viewed these programming questions
-
Examine the following program: class Exercise1 { public static void main (String[] args ) { int[] val = {0, 1, 2, 3}; sum = System.out.println( "Sum of all numbers = " + sum ); } } Complete the...
-
Microkernel operating systems aim to address perceived modularity and reliability issues in traditional "monolithic" operating systems. (i) Describe the typical architecture of a microkernel...
-
An analyst predicted the following: 1. Sales of $1,276 million. 2. Core profit margin of 5 percent. 3. Asset turnover of 2.2. 4. Core other operating income and unusual items are zero. The firm's...
-
In each of the following independent situations, determine the amount of the child and dependent care tax credit. (Assume that both taxpayers are employed and the year is 2013). a. Brad and Bonnie...
-
What immediate measures might be taken to relieve the stress and health issues from which the care managers suffer?
-
How does make determine whether a target needs to be rebuilt?
-
Analyze Take- Twos 1998-2000 financial data included in Exhibit 1. Compute the following financial ratios for each of those years: age of accounts receivable, age of inventory, gross profit...
-
A center that has been in operation for five years needs Group of answer choices only an operating budget for the upcoming year only a long-range financial plan that includes a 10 year projection on...
-
Cardinal is acquiring 100% of Gold. Describe 3 examples in accounting/financial reporting that are different when: (1) Gold is dissolved immediately after the acquisition and does not continue as a...
-
6. a) Suppose matrix A is the following product: [1 0 0 1 0 0 51 A = 2 1 0 2 1 0 0 3 1 1 3 1 1 0 For what values of t (if any) are there solutions to Ax = (1, 1, t)? 12 (CO2) (POI)
-
Hemingway Inc. hired Mr. King on December 31st, 2016. Mr. King will retire in 5 years (i.e., on December 31st, 2021) and, at that time, will receive a lump sum payment equal to 2% of his final salary...
-
Reviewing the scenarios given bellow, we need to make sure that independence requirements of APES 110 (Code of Ethics for Profession Accountants) are being met. If not, identify potential threats to...
-
A BGC Office Building was recently sold for P80,000,000. Given the following information: Gross income: factor: 10% Expenses: 40% of effective gross income What is the equity rate or cash-on-cash...
-
How much life insurance does Magdalena have to report as taxable income?
-
Determine the investigation you will do with regard to the physical infrastructure of the orchards, What other orchard shacks, and the cold storage building.
-
Give the products of the following reaction, where T is tritium: dioldehydrase Ad- CH CH3C-COH CoIII) coenzyme B12
-
Write a program that displays a flag rising up, as shown in Figure 15.14. As the national flag rises, play the national anthem. (You may use a flag image and anthem audio file from Listing 16.15.)...
-
Write a program that displays a rectanguloid, as shown in Figure 14.47a. The cube should grow and shrink as the window grows or shrinks. (a)
-
Suppose a right triangle is placed in a plane as shown below. The right-angle point is placed at (0, 0), and the other two points are placed at (200, 0), and (0, 100). Write a program that prompts...
-
In the section of his 2007 letter to the shareholders of Berkshire Hathaway titled Fanciful FiguresHow Public Companies Juice Earnings, Warren Buffett referred to the investment return assumption...
-
Based on 2012 revenues, the six largest providers of oilfield services are: 1. Schlumberger Ltd. (NYSE: SLB) Revenues: $42.1 billion Net income: $5.5 billion 2. Halliburton (NYSE: HAL) Revenues:...
-
On 21 September 2000, Intel Corporation (NASDAQ -GS: INTC)3 issued a press release containing information about its expected revenue growth for the third quarter of 2000. The announced growth fell...
Study smarter with the SolutionInn App