Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Create a Configurable Power Function

Build a function factory that returns power functions with optional modular arithmetic.

Python practice15 minClosures & Function FactoriesIntermediateLast updated April 13, 2026

Problem statement

Implement pow_factory(exp, modulus=None) which returns a function that takes one argument base and returns the result of raising base to the power exp. If modulus is provided (an integer), the returned function should compute modular exponentiation using Python's built-in pow(base, exp, modulus). For non-modular mode (modulus is None) the function should return base ** exp (supporting int and float exponents). Document any type/behavior assumptions in comments.

Task

Create a pow_factory that captures exponent and optional modulus, producing functions that compute x**exp or modular exponentiation efficiently.

Examples

Square function

Input

pow_factory(2)(3)

Output

9

pow_factory(2) returns a function that squares its input: 3**2 == 9.

Input format

A call expression: pow_factory(exp, modulus)(base) or pow_factory(exp)(base).

Output format

A numeric value: int or float, or int when modulus is used.

Constraints

If modulus is provided, it must be an integer and inputs used with that returned function should be suitable for modular exponentiation (integers). For modulus=None, exponent may be int or float.

Samples

Sample 1

Input

pow_factory(0.5)(9)

Output

3.0

exp=0.5 returns square-root function; sqrt(9) == 3.0