CS 1MD3 - Winter 2006 - Assignment #3

Generated for Student ID: 0462082

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 reverse order.

Question 2:
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).
Write a one-line "function factory" called functionFromGraph which accepts the graph of a 
function (a set of ordered pairs) and returns the function itself.
For example, the following sequence of commands should produce the indicated output:
>>> f = functionFromGraph(set([(1, 'a'), (2, 'b'), (3, 'c')]))
>>> f(1)
'a'
>>> f(2)
'b'
>>> f(3)
'c'
You may assume that the given parameter is, indeed, the graph of a function.

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).
Write a one-line Python function, ran, which accepts the graph of a mathematical function 
(i.e. a set of ordered pairs) and returns its range.