CS 1MD3 - Winter 2006 - Assignment #3

Generated for Student ID: 0443166

Due Date: Monday, February 27th

(You are responsible for verifying that your student number is correct!)

The following exercises involve writing very short (but tricky) functions to manipulate data structures. One of them is a recursive function which may be written in only three lines, while the rest may be written in only one line each (and are not recursive). Function signatures (the "def functionName(x,y,z):" lines) are not counted when enumerating the number of lines. So in this context, a "one-line function" actually occupies two lines: the signature on the first line, and the body on the next line.

Note: Not all students get the same number of questions because some questions are more difficult than others. We have tried to make all the assignments of roughly equal difficulty.

Question 1:
Write a recursive function to print the elements of a list in order.

Question 2:
If a dictionary, D, does not contain a particular key, k, the expression D[k] produces an error 
message.  This is not always desirable.  Often it would be more convenient for the dictionary to
return a default value for any keys unknown to it.
Write a one-line "dictionary looker-upper-maker" called makeLookup.
makeLookup should accept a dictionary and a default value, and return a function that will handle
dictionary lookups, returning the given default value in the event of a failure.
Sorry.  This is very confusing.  Let me show you what I mean...
>>> D = {1: 'a', 2: 'b', 3: 'c'}
>>> D[1]
'a'
>>> D[4]
Traceback (most recent call last):
 File "", line 1, in ?
KeyError: 4
>>> lookup = makeLookup(D, 'unknown value')
>>> lookup(1)
'a'
>>> lookup(4)
'unknown value'

Question 3:
Write a one-line function called invertDictionary that accepts a dictionary and 
returns a copy of it with the keys and values interchanged.  For example, suppose
d1 = {1: 'a', 2: 'b', 3: 'c'}.
Then the command d2 = invertDictionary(d1), would produce:
d2 = {'a': 1, 'b': 2, 'c': 3}.
You may assume that no two keys in the given dictionary map to the same value.