Lab Exercise 10

Writing and Using a Module


CS 1510
Introduction to Computing


Introduction

Create a directory on your USB device for this lab, say, lab10, and launch IDLE.

You will need two text files (plays by Shakespeare!), a Python program, a partially-completed Python program, and a responses.txt file to do today's lab. Download this zip file and place the contents of the directory into your lab10 directory.

At the end of the lab, you submit your responses.txt file along with the programs you wrote.



Task 1: Write Some String Utilities

Sometimes, we like to have functions that let us write our code in shorthand. String processing is one of those times.

Write the following string-processing functions.

starts_with(word, prefix)

Returns true if word starts with prefix, and false otherwise.
    >>> starts_with('alas', 'A')
    True
    >>> starts_with('Alas', 'a')
    True
is_length_of(word, number)

Returns true if word has exactly number characters, and false otherwise.
    >>> is_length_of('Eugene', 6)
    True
    >>> is_length_of('Mother Night', 6)
    False
is_palindrome(word)

Returns true if word reads exactly the same forward and backward, and false otherwise.
    >>> is_palindrome('Eugene')
    False
    >>> is_palindrome('racecar')
    True

Name your file str_utils.py.



Task 2: Use Your str_utils Module

The file str_utils.py contains only function definitions, so if we run the file, nothing observable happens.

Instead, we can import the functions by treating the file as a module! For example:

    >>> import str_utils
    >>> str_utils.starts_with('Alas', 'a')
    True
    >>> str_utils.is_palindrome('Eugene')
    False

We can also import individual functions ans use them directly with the from...import... construction:

    >>> from str_utils import is_length_of
    >>> is_length_of('Mother Night', 12)
    True

(Cool, huh?)

Next, run the program words_of_length.py. on the text files for Macbeth and Midsummer Night's Dream. Use a number of different word lengths for each.

Copy the results into your responses file.



Task 3: Extend the Text Analysis Program

Directly comparing the word counts between Macbeth and Midsummer Night's Dream is misleading, because the plays have different numbers of words.

Make a copy of words_of_length.py called word_percentages.py.

Modify word_percentages.py so that it reports the percentage of the words that are of the target length.

For example:

    >>>
    Enter name of text file : macbeth.txt
    Enter target word length: 7
    In the play in file macbeth.txt,  7.53% of the words are 7 characters long.



Task 4: Extend the Text Analysis Program Again

Now we can compare the word-length counts of Macbeth and Midsummer Night's Dream. Most authors tend to use similar word types whatever they write. Do these two plays have similar percentages for different word lengths?

Fill in the blanks in the partially-completed program word_comparison.py.

It moves the computation of the percentage of the words that are of the target length into a function named percentage_for_play(filename, word_length). When complete, the program runs like this:

    >>>
    Enter name of one file    : macbeth.txt
    Enter name of another file: midsummer-nights-dream.txt
    Enter target word length  : 7

    words of length  7
    ------------------
     7.53%   macbeth.txt
     7.36%   midsummer-nights-dream.txt

Use the code from word_percentages.py as your starting point. You may copy code from word_percentages.py and paste it into word_comparison.py.



Task 5: The Challenge Round

If you get this far in lab, give yourself a standing ovation.

(Seriously. Do it now.)

As it stands, comparing Macbeth and Midsummer Night's Dream across a range of word-length counts requires us to run word_comparison.py multiple times and look for trends in the differences between the percentages. A program can do that for us.

Extend the word comparison program to compare two plays for all word lengths up to and including the value entered by the user.

Modify your output so that it shows the difference between the two percentages at each word length. I'm thinking something like this.

Name your file extended_word_comparison.py.



Finishing Up

Make sure that your program files are complete and saved. Save your responses.txt file.

Submit your files for grading on the electronic submission system, at lab10 -- Writing and Using a Module.

As always, make sure you see the verification screen that says The files listed above were uploaded.

If you need any help, let me know.



Eugene Wallingford ..... wallingf@cs.uni.edu ..... October 29, 2014