COMP 200 Elements of Computer Science &
COMP 130 Elements of Algorithms and Computation
Spring 2012

Practice for Exam 2

The following problems are typical of those that will be on the exam. However, these example problems are not comprehensive of the relevant material. In addition to answering these problems, you should also review the course's online notes and the solutions to assignments 3 and 4. We have also made a study guide.

Problems

What if we had a bunch of regular expressions and a bunch of words, and we wanted to pick the words that match all of the regular expressions. The idea is that the regular expressions represent conditions that we are looking for. Maybe we we are looking for words that both begin with “dis” and end with “tion”. Rather than constructing one regular expression that combines these ideas, we could instead construct two simpler regular expressions, one for each part. This is yet another application of the concept of problem decomposition.

Define a function that takes a list of regular expression strings and a list of strings. Return a list of those strings that match all of the regular expressions.

For example, matchesAllREs(["dis[a-z]*$","[a-z]*tion$"],["distinction", "disaster", "disintegration", "ablation"]) should return ["distinction","disintegration"].

Of the following data structures — strings, lists, dictionaries, and tuples — which are mutable and which are immutable?

Why would one choose to use an immutable data structure?

Consider the following simple social network model. A person is a dictionary with the following keys and values: {"Name": aNameString, "Hometown": aHometownString, "Friends": aListOfPersons, "Groups": aListOfGroups}. A Group is a dictionary with the following keys and values: {"Name": aNameString, "Desc": aDescriptionString, "Members": aListOfPersons}. This is a small example of representing a database in Python, using a dictionary for each database “table”, or “entity”.

Write the following functions, utilizing existing functions whenever possible. Ensure that each of the dictionaries' lists never contain duplicate elements.

The idea is that we want to use the code like this: