CS 1MD3 - Winter 2006 - Assignment #3

Generated for Student ID: 0244156

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 combine a list of dictionaries into one.
The function should accept a list of dictionaries [d1, d2, ..., dn].
It should return a new dictionary containing everything from d1 through dn.
If there are any keys in common among the dictionaries, the value from the last one should
be used in the new dictionary.  For example, if d3['cs1md3']=='dilatory',
d5['cs1md3']=='delightful', then the new dictionary should use the value from d5.  In other
words later dictionaries override previous dictionaries.

Question 2:
The norm of a vector x = (x1, x2, ..., xn) is the square root of the sum:
x1^2 + x2^2 + ... + xn^2.
Implement the norm function on one line using reduce, map, and two lambda forms.
Your function should accept one parameter (either a list or a tuple) and return its norm.

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