a) Find word in a text file. Implement the function isWord InFile(fileName, word), which given a...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
a) Find word in a text file. Implement the function isWord InFile(fileName, word), which given a string fileName and a string word, checks whether or not word is in the file named fileName. (Hint: Read the file in single shot into a strings and feel free to use the the membership test operator in: word in s) Test it on the file named "test.txt" consisting of: This is a test for Problem 3 of Programming Assignment 3 Test program: print (isWordInFile("test.txt", "Programming")) print (isWordInFile("test.txt", "programming")) b) Find word in text file: return line number. This is a variation of Part (a) which requires reading the file line by line. Implement the function wordSearch (fileNane, word), which given a string fileName and a string word (not containing the new line character), searches for word in the file named fileName. If found, it should return the line number of the first occurrence. Otherwise, it should return 0. Feel free to use the membership test operator in for strings. Test it on the file "test.txt" given in Part (a). Test program: print (wordSearch ("test.txt", "Programming")) print (wordSearch ("test.txt","programming")) Output: True False c) Duplicate lines. Implement the function duplicateLines (fileName), which given a string fileName, opens the file name fileName for reading and creates a new file whose content is like fileName but with all lines duplicated. This function assumes that extension of fileName is ".txt", i.e., as a string, fileName is of the form name.txt, for some string name. The new file should be called nameDuplicated.txt. Use assert to stop the program if the extension of fileName is not "txt". Display an appropriate error message. This is a test This is a test for for Output: 3 Test your function on the file "test.txt" given in Part (a). It should create a new file named "test Duplicated.txt" consisting of: Problem 3 of Programming Assignment 3 Problem 3 of Programming Assignment 3 Test it also a file whose extension is not ".txt". a) Find word in a text file. Implement the function isWord InFile(fileName, word), which given a string fileName and a string word, checks whether or not word is in the file named fileName. (Hint: Read the file in single shot into a strings and feel free to use the the membership test operator in: word in s) Test it on the file named "test.txt" consisting of: This is a test for Problem 3 of Programming Assignment 3 Test program: print (isWordInFile("test.txt", "Programming")) print (isWordInFile("test.txt", "programming")) b) Find word in text file: return line number. This is a variation of Part (a) which requires reading the file line by line. Implement the function wordSearch (fileNane, word), which given a string fileName and a string word (not containing the new line character), searches for word in the file named fileName. If found, it should return the line number of the first occurrence. Otherwise, it should return 0. Feel free to use the membership test operator in for strings. Test it on the file "test.txt" given in Part (a). Test program: print (wordSearch ("test.txt", "Programming")) print (wordSearch ("test.txt","programming")) Output: True False c) Duplicate lines. Implement the function duplicateLines (fileName), which given a string fileName, opens the file name fileName for reading and creates a new file whose content is like fileName but with all lines duplicated. This function assumes that extension of fileName is ".txt", i.e., as a string, fileName is of the form name.txt, for some string name. The new file should be called nameDuplicated.txt. Use assert to stop the program if the extension of fileName is not "txt". Display an appropriate error message. This is a test This is a test for for Output: 3 Test your function on the file "test.txt" given in Part (a). It should create a new file named "test Duplicated.txt" consisting of: Problem 3 of Programming Assignment 3 Problem 3 of Programming Assignment 3 Test it also a file whose extension is not ".txt".
Expert Answer:
Answer rating: 100% (QA)
a Find word in text file def isWordInFilefilesearchstring with openfile ... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
Please remember, there are space(s) between operands/operators in the ex[1]pression. So your solution needs to think of this aspect. You will solve the problem as stated below:- (1) [Design a simple...
-
Write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of...
-
Write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of...
-
What is the benefit to a company from a securities underwriter? They study the market and advise companies on where to set their IPO share price. They help companies to reduce the risk associated...
-
Determine the force in members EI and JI of the truss which serves to support the deck of a bridge. State if these members are in tension or compression. Units Used: kip = 103 lb Given: F1 = 4000 lb...
-
Could exchange rate fluctuations affect the regression models offered by Dr. Dubin in the Bali case? Hint: if exchange rate fluctuations were not included in the regression models by Dubin (or by...
-
With reference to Exercise 4.5, find an expression for the distribution function \(F(x)\) of the random variable. Data From Exercise 4.5 k 4.5 Given that f(x): = is a probability distribution for 2x...
-
1. Using the following information, compute the cost of direct materials used. Raw materials inventory, January 1 ........ $30,000 Raw materials inventory, December 31 ...... $60,000 Work in process,...
-
Using the Internet, find an advertisement or other promotion that resulted in miscommunication. Describe what caused the miscommunication and how the business handled it.
-
Q1. Calculate the amounts that should be reported for (L) and (Z) on the 9/28/2008 balance sheet: (L) = ___________ million (Z) = ___________ million Q2. What was the beginning balance of the...
-
An analysis of the transactions of Rutherford Company for the year ended December 31, yields the following information: sales revenue, $166,400; insurance expense, $4,300; interest income, $10,800;...
-
A beam sending neutrinos from CERN, Geneva-Switzerland, to the Gran Sasso Laboratories in Italy, near LAquila, was operational between 2006 and 2012. The distance as the crow flies (i.e., along the...
-
______ The risk of a type II error is directly controlled in a hypothesis test by establishing a specific significance level.
-
An automobile A travels on a straight road at the constant speed \(v_{A}=75 \mathrm{~km} / \mathrm{h}\). A second car B arrives at the speed \(v_{B}=130 \mathrm{~km} / \mathrm{h}\) and starts braking...
-
______ If the null hypothesis is false, increasing the level of significance () for a specified sample size will increase the probability of rejecting the null hypothesis.
-
_______________If two events are mutually exclusive, then P(A or B) = P(A) + P(B).
-
left ALL BETA Can't read the text? Switch theme 1. Alternate Prefix Sums For an array of integers, arr[n], its prefix sum array, psum[n], is constructed as psum[i] = sum(arr[0]...arr[i]) where 0 i
-
Calculate the electrical conductivity of a fiber-reinforced polyethylene part that is reinforced with 20 vol % of continuous, aligned nickel fibers.
-
Create a Web page named randSeq.html that generates a random 3-letter sequence. When the user clicks on a button, a function named GenerateSequence should be called to generate and display a sequence...
-
Describe two factors that contributed to Microsoft's dominance of the browser market?
-
What do you think would happen if you forgot to place a HALT instruction at the end of a machine-language program? How would the Control Unit react? Use the simulator to test your prediction, then...
-
Explain the working of domestic refrigeration system with a neat sketch?
-
Fluid is a substance that: (a) Cannot be subjected to shear force (b) Always expands until it fills any container (c) Has the same shear stress at a point regardless of its motion (d) Cannot remain...
-
Discuss about psychrometry and their properties?
Study smarter with the SolutionInn App