CSC 221: Introduction to Programming
Fall 2023

HW 4: Files and Strings


Did Charles Dickens have a penchant for using words with multiple consonants? Did Louisa May Alcott find shorter words with multiple vowels irresistible? These question may seem a bit fanciful, but considerable research has gone into studying the collected works of authors and discerning patterns. The theory is that each author has a unique way of using words and letters in his or her writing, which identifies that author's works the same way fingerprints identify people. These literary fingerprints not only provide insight into an author's methods, but also can be (and have been) used to identify the author of anonymous or disputed works of literature.

For this assignment, you will write two Python functions for analyzing a text file and generating a report on consonant and vowel usage.

  1. The profile function should take a filename as input and return a list containing four numbers: the number of consonants, vowels, spaces, and total characters in the file, respectively. To keep things simple, we will assume that the letter 'y' is always a consonant, not a vowel. For example,
    >>> profile("poe.txt") [16797, 10131, 6008, 34007]
  2. The report function should take a filename as input and should call the profile function to count the different types of characters in that file. It should then print a report of the following form, with the consonant/vowel ratio and the percentage of spaces rounded to three decimal places:
    >>> report("poe.txt") poe.txt contains 16797 consonants and 10131 vowels. The consonant/vowel ratio is 1.658. poe.txt contains 6008 spaces out of 34007 characters. The percentage of spaces is 17.667%.

    Your program should not crash due to a division-by-zero error. In the case of a file containing no characters, it should simply print a message stating that the file is empty. In the case of a file with no vowels, it should report the consonant/vowel ratio as "undefined."

You should test your code on small files for which you can hand-calculate stats. Once you are confident it works as desired, you can test your code on the following public-domain texts:

Save your functions in a module named lastnameProfile, where lastname is your last name. As always, be sure to include a comment block at the top of your module that includes the file name and your name. Also have a comment string in the function to document its behavior.