Computation and Cognition - Fall 2006

Homework 4

Due by class time Wednesday, October 4

Programming Problems

  1. Write a program called avgWordLen() that prompts the user for the name of a text file and reports the average word length of all words in the file to three decimal places. For example, using the text file haunting.txt, the output would look like this:

    >>> avgWordLen()
    Enter file name: haunting.txt
    The average word length for haunting.txt is 5.048 characters
    >>> 
    
  2. Write a program called count() that prompts the user for the name of a text file and reports the total number of characters, words, and lines in the file. For example, using the text file haunting.txt, the output would look like this:

    >>> count()
    Enter file name: haunting.txt
    haunting.txt contains 502 characters, 83 words, and 7 lines
    >>>
    
  3. Write a program called copyfile() that prompts the user for the name of a text file and creates a copy of the file. The name of the new file should be the original name prefixed by "copy-of-". For example, to make a copy of the file haunting.txt, we would do this:

    >>> copyfile()
    Enter file name: haunting.txt
    Made a new copy of haunting.txt called copy-of-haunting.txt
    >>> 
    
  4. The file elementsData.txt contains information about chemical elements. Each line corresponds to a single element, and consists of the following information:

    Write a program called elements() that reads in the information from a file of this form and prints it out in a nicely-formatted table, exactly as shown below:

                   Element (symbol)    Atomic Weight
              --------------------------------------
                1. Hydrogen (H)            1.008
                2. Helium (He)             4.003
                3. Lithium (Li)            6.939
                4. Beryllium (Be)          9.012
                5. Boron (B)              10.811
                6. Carbon (C)             12.011
                7. Nitrogen (N)           14.007
                8. Oxygen (O)             15.999
                9. Fluorine (F)           18.998
               10. Neon (Ne)              20.183
    

Turning in Your Homework

Put all of your program definitions into a single file called assign4.py. Print out this file and turn it in to me in class on Wednesday. If you have questions about anything, don't hesitate to ask!