logo Hurry, Grab up to 30% discount on the entire course
Order Now logo

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Cesar MeyerStatistics
(5/5)

525 Answers

Hire Me
expert
Kevin ConnellyComputer science
(4/5)

661 Answers

Hire Me
expert
Allen CrumpStatistics
(5/5)

751 Answers

Hire Me
expert
Deepak BansalAccounting
(5/5)

525 Answers

Hire Me
Others
(5/5)

Formatting a text file. The judges of an essay competition require all essays to be submitted electronically

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

Problem  1  [35  points  ]   Formatting  a  text  file.  The  judges  of  an  essay  competition  require all essays to be submitted electronically in a neatly formatted fashion. However, not all submissions follow the formatting rules, and so the judges would like a program that formats a file according to their formatting rules. The judges also require some statistics for each essay. In particular, they require a count of the number of non-blank lines, the number of words, and the average word length for each essay. In this problem, you are asked to write a program that does both things; that is, a program that formats the files, and then calculates the required statistics.

For the purposes of the program and the discussion, a word is simply a consecutive sequence of non-whitespace characters surrounded by white space. By this definition, a period or a comma by itself, surrounded by white space, would be counted as a word, but we’ll live with that. The formatting rules for each essay are as follows:

 

There should not be any blank spaces at the beginning of a line.

Two or more blank spaces should not appear consecutively.  That is,  there should be only one blank space between consecutive words.

Two or more blank lines should not appear consecutively. That is, there should be only one blank line between consecutive paragraphs.

There should be at most 60 characters per line (including blank spaces). Also, words should not be broken across lines. This implies that in a paragraph, a newline character should appear after the last complete word that will fit in a 60-character line. You may assume that no single word is longer than 60 characters (seems like a reasonable assumption).

 

Format the essay file according to the rules described above and compute the required statis- tics. You should do this in three steps. For the purposes of the following discussion, assume that the essay is in a file called essay.txt.

 

1. First, remove all extra white space (extra spaces between words, extra spaces at the beginning of a line, and extra blank lines between paragraphs) from essay.txt and output the result into a file called essay neb.txt. Keep in mind that only extra white space is removed. In particular, paragraphs should still be separated by one blank line.

2. Next, adjust the length of the lines in essay neb.txt to 60 characters, and output the result into a file called essay final.txt.

3. Finally, count the number of (non-blank) lines, the number of words, and the average word length for the text in essay final.txt. Output the result, with the appropriate headings, into a file called essay  stats.txt. The average word length is simply the sum of the word lengths divided by the number of words.

 

Your program should contain the following functions:

 

A function called remove extra whitespaces with two parameters: infile, the name of the file to be read from and outfile, the name of the file to be written to. Both these parameters are strings as they are file names.  This function should open and close files as necessary and carry out the task described in step (1) above.

A function called adjust linelength with two parameters: infile, the name of the file to be read from (this is the file from which all extra white spaces have been removed) and outfile, the name of the file to be written to. Both these parameters are strings as they are file names. This function should open and close files as necessary and carry out the task described in step (2) above. Keep in mind that words should not be broken across lines. This implies that in the output file, a newline character should appear after the last complete word that will fit in a 60-character line. This should be true for every line of a paragraph. Furthermore, each line should contain the maximum number of complete words that can fit in that line. Also make sure that consecutive paragraphs continue to be separated by a blank line.

A function called essay statistics with two parameters: infile, the name of the file to be read from (this is the file in which line lengths have been adjusted) and outfile, the name of the file to be written to. Both these parameters are strings as they are file names. This function should open and close files as necessary and carry out the task described in step (3) above.

 

 

(5/5)
Attachments:

Related Questions

. The fundamental operations of create, read, update, and delete (CRUD) in either Python or Java

CS 340 Milestone One Guidelines and Rubric  Overview: For this assignment, you will implement the fundamental operations of create, read, update,

. Develop a program to emulate a purchase transaction at a retail store. This  program will have two classes, a LineItem class and a Transaction class

Retail Transaction Programming Project  Project Requirements:  Develop a program to emulate a purchase transaction at a retail store. This

. The following program contains five errors. Identify the errors and fix them

7COM1028   Secure Systems Programming   Referral Coursework: Secure

. Accepts the following from a user: Item Name Item Quantity Item Price Allows the user to create a file to store the sales receipt contents

Create a GUI program that:Accepts the following from a user:Item NameItem QuantityItem PriceAllows the user to create a file to store the sales receip

. The final project will encompass developing a web service using a software stack and implementing an industry-standard interface. Regardless of whether you choose to pursue application development goals as a pure developer or as a software engineer

CS 340 Final Project Guidelines and Rubric  Overview The final project will encompass developing a web service using a software stack and impleme