Question

In: Computer Science

Write a function getStats(fname) that determines the following statistics for the input file, fname, and write...

Write a function getStats(fname) that determines the following statistics for the input file,
fname, and write the results to the text file, FileStats.txt.
• Number of occurrences of each day of the week in the file.
• Number of lines (determined by end of line character “\n”)
• Number of words
• Numbers of characters (excludes spaces between words)
The output from getStats() should
• print the message ‘To view the results go to FileStats.txt’
• return ‘Thanks for using the getStats() function’

Include the name of the file and your name in the first line of FileStats.txt. For example,
I ran getStats(“Emma.txt”) and got the following results:

Solutions

Expert Solution

The problem does not specify which programming language to use, so have used Python. Below is the code:

def getStats (fname):
with open (fname) as f:
contents = f.read ()
  
daysofweek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
dayscount = []
for day in daysofweek:
dayscount.append (contents.lower().count(day.lower()))
  
nlines = contents.count ('\n')
nwords = len (contents.split ())
nchars = len (''.join (contents.split() ) )
with open ("FileStats.txt", 'w') as f:
i = 0
for day in daysofweek:
s = "Number of occurrences of " + day + " is " + str(dayscount [i]) + "\n"
f.write (s)
i += 1
f.write ("The number of lines is " + str (nlines) + "\n")
f.write ("The number of words is " + str (nwords) + "\n")
f.write ("The number of characters is " + str (nchars) + "\n")
  
print ('To view the results, go to FileStats.txt')
return 'Thanks for using the getStats () function'
  
print (getStats ("D:\greatexpectations.txt"))

##### Below is a sample run of the code:

To view the results, go to FileStats.txt
Thanks for using the getStats () function

##### Below are the contents of FileStats.txt (run on the novel Great Expectations as a text file):

Number of occurrences of Monday is 19
Number of occurrences of Tuesday is 5
Number of occurrences of Wednesday is 8
Number of occurrences of Thursday is 3
Number of occurrences of Friday is 1
Number of occurrences of Saturday is 9
Number of occurrences of Sunday is 33
The number of lines is 8233
The number of words is 187594
The number of characters is 821753


Related Solutions

In python write a function whose input is a string. This function determines the data type...
In python write a function whose input is a string. This function determines the data type of the input string. The data types can be a float, int, or string. Most pass the following assertions: assert determine_data_type('1.2') == float assert determine_data_type('4') == int assert determine_data_type('EAS503') == str
Write a C program that, given a file named Program_2.dat as input, determines and prints the...
Write a C program that, given a file named Program_2.dat as input, determines and prints the following information: The number of characters in the file. The number of uppercase letters in the file. The number of lowercase letters in the file. The number of words in the file. The number of lines in the file. Your program should assume that the input file, Program_2.dat, may contain any text whatsoever, and that text might be, or might not be, the excerpt...
Write a user-defined MATLAB function, with two input and two output arguments that determines the height...
Write a user-defined MATLAB function, with two input and two output arguments that determines the height in centimeters (cm) and mass in kilograms (kg)of a person from his height in inches (in.) and weight in pounds (lb). (a) Determine in SI units the height and mass of a 5 ft.15 in. person who weight 180 lb. (b) Determine your own height and weight in SI units.
Write a function redact() takes as input a file name. See secret.txt for the initial test....
Write a function redact() takes as input a file name. See secret.txt for the initial test. The function should print the contents of the file on the screen with this modification: Every occurrence of string 'secret' in the file should be replaced with string 'xxxxxx'. Every occurrence of “agent’ should be replaced with ‘yyyyyy’. Every occurrence of carrier should be replaced with ‘zzzzzz’. This function should catch exceptions. FOR PYTHON
Write a function called fillList that takes three parameters, an integer array, input file, and size....
Write a function called fillList that takes three parameters, an integer array, input file, and size. The function should fill the integer array with randomly generated values between two numbers lowLim and highLim read from the input file. in C++
Write a program that determines whether an input string is a palindrome; that is, whether it...
Write a program that determines whether an input string is a palindrome; that is, whether it can be read the same way forward and backward. At each point, you can read only one character of the input string; do not use an array to first store this string and then analyze it (except, possibly, in a stack implementation). Consider using multiple stacks. In Pseudocode please
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Create and complete a function M-file that will receive an input, ? , and output the...
Create and complete a function M-file that will receive an input, ? , and output the corresponding conversion to radians within the range of a complete circle. For each 30? increment, the output should be displayed as a string, (i.e. pi/2 , pi/4 ,etc.), and any other degree to be displayed numerically. I'm using Matlab, explanations are appreciated.
Write a complete Java program that does the following: Open an input file named data.txt that...
Write a complete Java program that does the following: Open an input file named data.txt that consists of a series of unknown number of integers. If data.txt does not exist, give an appropriate error message and terminate the program. Define a constant MAX of value 100 and create an array of size MAX to hold items from the input file. Make sure your program will not generate ArrayIndexOutOfBounds exception. Open an output file named result.txt and write the array elements...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two...
ASSIGNMENT: Write a program and use the attached file (babynames.txt) as input file, and create two output tiles. One file listing out all boys names, and the other file listing out all girls name. CODE: (teacher gave some of the code below use it to find the answer please String B is the boy names String E is girl names) import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.Scanner; /** This program reads a file with numbers, and writes the numbers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT