With this dictionary representation of functions in place wr
With this dictionary representation of functions in place, write the following three Python functions:
is_injective: This function accepts a mathematical function (in dictionary form) and returns a Boolean value -- True if the mathematical function is injective and False otherwise. Examples:
Note that the examples illustrate that your is_injective must work on mathematical functions from any finite set to any other finite set; don\'t assume that we only use numbers.
is_surjective: This function accepts a mathematical function (in dictionary form) and a set, written as a list, that gives the codomain of the function and returns a Boolean value -- True if the mathematical function is surjective and False otherwise. Examples:
is_bijective: This function accepts a mathematical function (in dictionary form) and a set, written as a list, that gives the codomain of the function and returns a Boolean value -- True if the mathematical function is bijective and False otherwise. Examples:
Solution
def is_injective(dict):
    vals = dict.values()
    # set does not contain duplicates. injective should not contain duplicates.
    if(len(vals) == len(set(vals))):
        return True
    return False
 print \"-----Injective----\"
 print is_injective({0: 8, 1: 8, 2: 9, 3: 9})
 print is_injective({\'a\': \'x\', \'b\': \'y\', \'c\': \'z\'})
# each domain value should be a value in dict, else return false
 def is_surjective(dict,domain):
    for i in domain:
        if i not in dict.values():
            return False
    return True
 print \"-------Surjective---------\"
 print is_surjective({0: 8, 1: 8, 2: 9, 3: 9}, [7,8,9])
 print is_surjective({0: 8, 1: 8, 2: 9, 3: 9}, [8,9])
 print is_surjective({\'a\': \'x\', \'b\':\'y\', \'c\':\'z\'}, [\'x\',\'y\',\'z\',\'t\'])
# bijective only if injective and surjective.
 def is_bijective(dict,domain):
    if(is_injective(dict) and is_surjective(dict,domain)):
        return True
    else:
        return False
       
 print \"--------------Bijective----------------\"
 print is_bijective({0: 8, 1: 8, 2: 9, 3: 9}, [7,8,9])
 print is_bijective({0: 8, 1: 8, 2: 9, 3: 9}, [8,9])
 print is_bijective({\'a\': \'x\', \'b\':\'y\', \'c\':\'z\'}, [\'x\',\'y\',\'z\',\'t\'])
 print is_bijective({\'a\': \'x\', \'b\':\'y\', \'c\':\'z\'}, [\'x\',\'y\',\'z\'])
# sample output

