A function f: A → B on finite sets is given by the image of each element of A. Is it injective (one-to-one), surjective (onto) and bijective?
Input
Three lines of words separated by spaces: the elements of A, the elements of B, and the images f(a) of A's elements, in the same order as A. A and B have 1 to 50 elements each, and no element is listed twice.
Output
If some image is not an element of B, print not a function. Otherwise print three lines: injective: yes (or no), surjective: ... and bijective: ....
Examples
1 2 3 x y z y z x
injective: yes surjective: yes bijective: yes
1 2 3 x y x y x
injective: no surjective: yes bijective: no
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Pair each domain element with its image: dict(zip(domain, images)).
Hint 2
If some image is not in the codomain, f is not a function from A to B.
Hint 3
Injective: no two images are equal (len(set(images)) == len(images)). Surjective: set(images) == set(codomain).
Solution
Show one correct solution (try it yourself first)
A = input().split()
B = input().split()
images = input().split()
if any(y not in B for y in images):
print("not a function")
else:
injective = len(set(images)) == len(images)
surjective = set(images) == set(B)
yes = lambda b: "yes" if b else "no"
print("injective:", yes(injective))
print("surjective:", yes(surjective))
print("bijective:", yes(injective and surjective))