Compsci 6/101, Spring 2011, Lab 10, Re[gex|cursion]

In recitation/lab this week we'll practice with regular expressions and list comprehensions.

Regular Expressions

Use this downloadable regex tool to help answer these questions. The answers go on the handin page

In answering questions about regular expressions you should provide both the answer, and the regular expression you used to answer the question. For example.

Question: How many words end in the letter 'a'.

Answer: There are 898 such words, the regular expression is a$, the first word is ababa and the last word is zomba.

You can use more than one regular expression to answer the questions. You can do calculations, record intermediate results, and so on. Be sure to report carefully the methods you used to arrive at your answer as well as the answer itself.

See the handin page for questions.

List Comprehensions

Assume that letters is a list of strings and that table is a dictionary of strings (as keys) to lists of strings (as values). For example:

letters = ["snap", "crackle", "pop", "broom", "knee"]

table = {"fruit":["apple", "cherry", "guava"],
         "candy":["snickers", "milky way", "jujube"]
        }

Write a list comprehension for each question (see the handin page).

For example:

Question: the list of strings in letters that begin with the letter 'p'.

Answer:

 [ w for w in letters if w.startswith('p') ]

Question: The keys in table that contain the string 'apple' in the value.

Answer:

 [ w for w in table.keys() if 'apple' in table[w] ]

See the handin page for questions.