CS 1MD3 - Winter 2006 - Assignment #3

Generated for Student ID: 0551324

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:
Implement the non-destructive reverseList function, as a recursive function.
Input parameter: a list, L.
Output: A new list which contains the elements of L in reverse order.
Notes: Operates recursively and leaves the original list intact.

Question 2:
Write a one-line function to convert a dictionary to a list of ordered (key, value) pairs.

Question 3:
The graph of a (mathematical) function is the set of all pairs (x,y) such that 
x is in the domain of f (i.e. the set of values over which f is defined), and y = f(x).
The range of a function is the set of all values it can attain (i.e. the set of all values
f(x) where x is in the domain of f).
A function is called "injective" (or "one-to-one") if whenever f(x) = f(y) it is necessarily 
the case that x = y.  The function f(x) = 2x is injective, while the function g(x) = x^2 is not
(since g(1) = g(-1) = 1).
Assume f is the graph of a function (no need to specify a domain)

Question 4:
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.