Lesson 6 of 15
Functions
Functions as Mappings
A function assigns to each element of (the domain) exactly one element of (the codomain). The range (or image) is .
Classification
| Name | Definition | Condition |
|---|---|---|
| Injective (one-to-one) | distinct inputs → distinct outputs | |
| Surjective (onto) | every output is reached | |
| Bijective | injective and surjective | perfect pairing |
A bijection from to implies .
def is_injective(f_dict):
values = list(f_dict.values())
return len(values) == len(set(values))
def is_surjective(f_dict, codomain):
return set(f_dict.values()) == codomain
def is_bijective(f_dict, codomain):
return is_injective(f_dict) and is_surjective(f_dict, codomain)
f = {1: 'a', 2: 'b', 3: 'c'}
print(is_bijective(f, {'a', 'b', 'c'})) # True
Composition and Inverse
. A function has an inverse if and only if it is bijective.
Your Task
Implement is_injective, is_surjective, and is_bijective as shown above.
Pyodide loading...
Loading...
Click "Run" to execute your code.