Due: Thursday 21 October
The reading for this assignment is Chapter 7 of
How to think....
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
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.
- 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.
- 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
- 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.
- 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.
- Write a function called is_abecedarian that returns
True if the letters in a word appear in alphabetical order.
- 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:
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
it should print all the words in the `dictionary' that contain the
letter 'e'. You can specify another filter on the command line, like
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.
include your answers in comments in filters.py.
- 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.
- 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.
- How many words are there that use
all the vowels aeiou? How about aeiouy?
- How many words can you spell using only the letters
- How many abecedarian words are there?
- How many palindromes?