Introductory Programming
Fall 2004

Homework 6

Due: Thursday 21 October

The reading for this assignment is Chapter 7 of How to think....

Palindromes, Lipograms and Tautonyms

You might already have a program named filters.py, but if not, you should create one. In it, you should write a function named has_e that takes a string as a parameter and that returns True if the string contains an 'e', and False otherwise.

A boolean function like this is sometimes called a `filter' because you can use it to filter out the strings that meet a particular criterion.

For each of the following filters, you should write a function that implements the specification, and then write a few lines of code that test your functions with at least one word that meets the criteria and at least one word that doesn't.

  1. Read the handout regarding lipograms (attached), and then write a filter called has_no_e that returns True if the given word doesn't have the letter `e' in it.

  2. Write a function named avoids that takes a word and a string of forbidden letters, and that returns True if the word doesn't use any of the forbidden letters.

  3. Write a function named uses_all that takes a word and a string of required letters, and that returns True if the word uses all the required letters at least once.

  4. Write a function named uses_only that takes a word and a string of letters, and that returns True if the word contains only letters in the list.

  5. Write a function called is_abecedarian that returns True if the letters in a word appear in alphabetical order.

  6. Write a function called is_palindrome that returns True if the given word is a palindrome (a palindrome reads the same way forward and backward, like 'otto' and 'radar').

Once you have written your filters and you are pretty sure they are correct, you can use them to search for words in the dictionary that meet various criteria. Download word_filter.py from the class web page:

wget http://wb/ip/code/word_filter.py
word_filter.py reads through the list of words in /usr/share/dict/words and prints only those words that meet the given criteria. By default, it uses the filter has_e, so if you run

python word_filter.py
it should print all the words in the `dictionary' that contain the letter 'e'. You can specify another filter on the command line, like this:

python word_filter.py has_no_e
and you should get all the words that don't have an e. And, you can specify a string of words that should be avoided (or included, etc):

python word_filter.py avoids qwxyz
Use your filters to answer the following questions. You can include your answers in comments in filters.py.

  1. What percentage of the words in the dictionary do not contain the letter 'e'? HINT: pipe the output from your program into wc. If there are any print statements in filters.py you might want to comment them out.

  2. Using avoids, can you find the 5 least useful letters; that is, the combination of 5 letters that you can omit and eliminate the smallest number of words? You don't have to find the best combination; just try a few and report the best one you find.

  3. How many words are there that use all the vowels aeiou? How about aeiouy?

  4. How many words can you spell using only the letters acefhlo?

  5. How many abecedarian words are there?

  6. How many palindromes?