Bill Manaris : Spring 2015 / CSCI 220 Homework 2

Assigned Date: Friday, Feb. 13, 2015
Due Date: Friday, Feb. 20, 2015
Due Time: 12:20pm

Last modified on February 23, 2015, at 01:39 PM (see updates)

Assignment

Write a program that reads in a file and calculates the number of lines, words, and characters contained. The program should accept a filename as input and then print three numbers showing the count of lines, words, and characters in the file. (This is the same as exercise 15, p. 121 of the textbook.)

Learning Objectives

Input and Output

The input to the program is a filename of a text file, e.g., "text.txt".

This file may contain zero or more lines, with zero or more words.

Below are some examples.

Note: The output of your program should be formatted precisely as shown below, as your program may be tested automatically.

Example 1

If the input file contains this:

Hello world!

your program's output should be this:

1 2 12

Example 2

If the input file contains this:

Python is a widely used general-purpose, high-level programming language.
Its design philosophy emphasizes code readability, and its syntax allows
programmers to express concepts in fewer lines of code than would be possible in
languages such as C++ or Java. The language provides constructs intended to
enable clear programs on both a small and large scale.

your program's output should be this:

5 55 360

Example 3

If the input file is completely empty (i.e., no newlines, nothing):


your program's output should be this:

0 0 0

Bonus

If the user makes an error, output a meaningful error and terminate. If the input is correct, perform the calculations and output the result.

If you do the bonus, make sure you update the header documentation to reflect that.

Documentation

In general, you should comment any variable, obscure statement, block of code, etc. you create.

Additionally, your code should always have header documentation that explains what the program does, in general terms.

Follow the documentation instructions from Homework 1. In particular, you should modify these comments, to fit the new assignment.

Submissions

You will submit your assignment via OAKS. Your submission consists of:

  1. Your Python program. Call it, wc.py.
  2. A printout of your program - to be submitted in class on the due date.
  3. If you do the bonus, submit a separate, additional program. Give this program a different name, i.e., wc.bonus.py

Grading

Your grade will be based on how well you followed the above instructions.

(Printable View of http://www.cs.cofc.edu/~manaris/?n=Spring2015.CSCI220Homework2)