Skip to content

Instantly share code, notes, and snippets.

@tnsicdr
Created October 23, 2015 14:44
Show Gist options
  • Save tnsicdr/1422b0756757afc01378 to your computer and use it in GitHub Desktop.
Save tnsicdr/1422b0756757afc01378 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
def is_function(d):
"""Takes a dictionary and determines if function"""
for k,v in d.items():
if len(v) == 0:
# Not a Function if No Output for Input
return False
if len(v) > 1:
# Not a Function if Input has Multiple Output
return False
return True
def is_injective(d):
used = []
# Is function?
if(is_function(d)):
for k in d.items():
if k[1] in used:
return False
else:
used.append(k[1])
return True
else:
return False
def is_surjective(d):
# Put values in comparison list
lrange = []
# codomain list
codomain = [0,1,2,3,4,5,6,7,8,9]
# Is function?
if(is_function(d)):
# put all items into range
for k,v in d.items():
lrange.append(v.pop())
# sort range
lrange.sort()
# remove dupes
lrange = list(set(lrange))
if lrange == codomain:
return True
else:
return False
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment