Lesson 6 of 15

Functions

Functions as Mappings

A function f:ABf: A \to B assigns to each element of AA (the domain) exactly one element of BB (the codomain). The range (or image) is f(A)={f(a):aA}f(A) = \{f(a) : a \in A\}.

Classification

NameDefinitionCondition
Injective (one-to-one)f(a)=f(b)a=bf(a) = f(b) \Rightarrow a = bdistinct inputs → distinct outputs
Surjective (onto)bB, a, f(a)=b\forall b \in B,\ \exists a,\ f(a) = bevery output is reached
Bijectiveinjective and surjectiveperfect pairing

A bijection from AA to BB implies A=B|A| = |B|.

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

(gf)(x)=g(f(x))(g \circ f)(x) = g(f(x)). A function ff has an inverse f1f^{-1} 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.