Lecture 46:

posted Apr 30, 2013, 6:16 AM by Samuel Konstantinovich   [ updated Apr 30, 2013, 7:23 AM ]
Homework: When you go home tonight (not before end of day), check your grades on the HW server, then comment here if there are any issues:  https://docs.google.com/forms/d/1bhaMdcFj57EYPiwwXcH3PIvhvDR4A-rC-zLCNeZKqTI/viewform

Do now:

Practice some paper based problems

1.What is the range of values of random.random() ?

2. What is the range of values of random.randint(a,b) ?

3. What is the range of values of random.randint(a,b)+random.random() ?

4. Find the errors: (There are 5 errors)
include random
    B = {}
for i in range(15):
print B

C = []
for a in range(15):


B[random.randint(0,10)]='On the first day of Christmas'
C[random.randint(0,10)]='Blizzard gave to me...'
B[random.random()] = random.randint(1,100)
C[random.random()] = random.randint(1,100)

5a. Plan how to complete this function with your neighbors. 
5b. Write this on paper, check it with your neighbors, and re-write as needed. 
5c. Write a function in python and test it. 

Important: If I see you didn't plan it and don't have a version on paper I will <insert threat here>!!!!!!

You are writing a function to invert a dictionary invert(D) that accepts a dictionary as a parameter.
(Assume all original values are immutable) 
The result of the function is a new dictionary with all old values as the keys, and all old keys are contained in lists as the new values:

D={ 1:2 , 2:7 , 3:23 }
invert(D) -> {2:[1] , 7:[2] , 23:[3] }
#Notice: All new values are lists originally 1 mapped to 2, and the inverted one makes 2 map to [1]

A = { 1:'a',2:'b',3:'c',4:'a',5:'a',6:'b'}
invert(A) -> {'a': [1, 4, 5], 'c': [3], 'b': [2, 6]}:
#Notice: Originally both 2 and 6 maped to 'b' but in the inverted dictionary
#'b' maps to a list [2,6]