CS 1MD3 - Winter 2006 - Assignment #3

Generated for Student ID: 0568063

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:
Oddly, Python doesn't have a built-in function to make a (deep) copy of a list.  The expression
"list2 = list1" merely gives list1 another name--but they both refer to the same list; if
you change one, you change the other.
Write a recursive 'copyList' function which accepts a list and returns a duplicate of it without
affecting the original list.

Question 2:
The dot product of two vectors x = (x1, x2, ..., xn) and y = (y1, y2, ..., yn) is the
sum: x1*y1 + x2*y2 + ... + xn*yn.
Implement the dotProduct function on one line using reduce, map, and two lambda forms.
Your function should accept two parameters (either lists or tuples) and return their dot product.

Question 3:
Write a one-line function that accepts a dictionary and value and returns the set of
keys associated with that value.  Use a list comprehension.