# Spencer Smith (based on code by William Farmer)
# macid: smiths
# student number: #######
# January 30, 2011
#
# This is a solution for Unmarked Assignment 3 (Lab 04).

def main():

    # Description
    print """\

This program implements the unix wc program that computes the number
of lines, words, and characters in a text file.  
"""
    # Input
    inFileName = raw_input("Enter the name of the input file: ")
    print
    inFile = open(inFileName, "r")
    myList = inFile.readlines()
    inFile.close()

    # Processing
    lines = len(myList)
    words = 0
    characters = 0
    for i in range(lines):
        words = words + len(myList[i].split())
        characters = characters + len(myList[i])

    # Output
    outFileName = "wc_output.txt"
    outFile = open(outFileName, "w")
    outFile.write("x= "+str(lines) + "\n")    
    outFile.write("y= "+str(words) + "\n")
    outFile.write("z= "+str(characters))
    outFile.close()
    print 'The output is written in the file named "wc_output.txt".\n'	
    
main()

# Design Question
# 
# Obviously it will take about three times as long to read a file three
# times as it does to read it once.  For a small file, the difference
# between reading it once and three times may not be significant, but
# for a large file, the difference may be quite significant.  Therefore,
# it is best to read the file only once.
# 
# Testing Question
# 
# Question 1
# No.  How the program counts the lines, words, and characters in a file
# is independent of the size of the file.  Hence, if the program can
# correctly count the lines, words, and characters in some small files
# that contains space and newline characters, it should be able to
# correctly count the lines, words, and characters in all files.
#
# Question 2
# The number of characters (with spaces) counted by Microsoft Word does not
# match the number of characters counted by our program. The reason for this
# discrepancy is because our program counts the newline character ("\n") as a
# character. There will be an extra character counted by our program for
# every newline character.