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 JAVA program that reads in a string from standard input and determines the following:...
Write a JAVA program that reads in a string from standard input and determines the following: - How many vowels are in the string (FOR THE PURPOSE OF THIS PROGRAM 'Y' is NOT considered a vowel)? - How many upper case characters are in the string? - How many digits are in the string? - How many white space characters are in the string? - Modify the program to indicate which vowel occurs the most. In the case of a...
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
Write function words() that takes one input argument—a file name—and returns the list of actual words...
Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
Write a C++ program that does the following: Read and input file containing the following PersonAName,...
Write a C++ program that does the following: Read and input file containing the following PersonAName, PersonBName, XA,YA, XB, YB where the coordinates of PersonA in a 100 by 100 room is XA, YA and the coordinates of PersonB is XB, YB. Use square root function in cmath sqrt() to calculate the shortest distance between two points. A file will be uploaded in this assignment that will list coordinates of two people. The program should use a function call that...
[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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT