''' Solution to Problem 7. ''' # k and v are lists def make( k, v ) : if (len(k) != len(v)): return None # It must be the case that the 2 lists are the same length new_dict = {} for i in range(0, len(k)): k_value = k[i] # This will be the new key v_value = v[i] # This will be the new value new_dict[k_value] = v_value # We have now added this key to the dictionary and mapped it to the value return new_dict # d is a dictionary def onto( d ) : # This function will return true if all the values in the dictionary are unique v = d.values() v = set(v) # set() returns a set with only one copy of each thing in the original v k = d.keys() # This returns an iterable object of all the keys in the dictionary if (len(v) == len(k)): # If v is still the same length, then all the values were unique in the first place return True else: return False if ( __name__ == '__main__' ) : try : d1 = make( [1, 2, 3], [ 'a', 'b', 'c', 'd' ] ) d2 = make( [1, 2, 3, 4], [ 'a', 'b', 'c', 'd' ] ) print( d1 ) print( d2 ) except : print( 'make() blows up' ) try : b1 = onto( { 1: 'odd', 2: 'even', 3: 'prime' } ) b2 = onto( { 1: 'odd', 2: 'even', 3: 'odd' } ) print( b1, b2 ) except : print( 'surjection() blows up' )