Skip to main content
ThePythonBook/Assessment

Functions Assessment

Test your mastery of Python functions including definitions, arguments, scope, recursion, lambdas, higher-order functions, and closures.

Progress
960 XP0/12
#1Greeting Generator
Write Code

Write a function greet(name, greeting="Hello") that returns a string in the format "{greeting}, {name}!". The greeting parameter should default to "Hello".

Loading editor...
#2The Mutable Default Trap
Fix the Bug

This function is supposed to append an item to a new list each time it is called without an explicit list. But calling it multiple times without a list accumulates items across calls. Fix the bug.

Loading editor...
#3Predict the *args Output
Predict Output

Read the code carefully and predict exactly what will be printed.

def show_args(*args):
    print(type(args).__name__)
    print(len(args))
    print(args[1])

show_args(10, 20, 30)
Loading editor...
#4Build a Profile
Write Code

Write a function build_profile(name, **kwargs) that returns a dictionary with the key "name" set to name, plus every key-value pair from kwargs.

Loading editor...
#5Predict the Scope Output
Predict Output

Read the code carefully and predict exactly what will be printed.

x = 10

def outer():
    x = 20
    def inner():
        print(x)
    inner()
    print(x)

outer()
print(x)
Loading editor...
#6Recursive Factorial
Write Code

Write a recursive function factorial(n) that returns the factorial of a non-negative integer n. By definition, factorial(0) is 1.

Loading editor...
#7The Silent Function
Fix the Bug

This function is supposed to apply a discount and return the new price, but it always returns None. Fix it so it returns the discounted price as a float rounded to 2 decimal places.

Loading editor...
#8Refactor to Lambda + Map
Refactor

Refactor the loop below into a single line using map() with a lambda. The variable squared should be a list of the squared values.

Loading editor...
#9Predict the Closure Output
Predict Output

Read the code carefully and predict exactly what will be printed.

def make_counter(start=0):
    count = start
    def increment():
        nonlocal count
        count += 1
        return count
    return increment

c = make_counter(5)
print(c())
print(c())
print(c())
Loading editor...
#10Function Composer
Write Code

Write a function compose(f, g) that returns a new function which, when called with an argument x, returns f(g(x)). In other words, it applies g first, then f to the result.

Loading editor...
#11Broken Filter
Fix the Bug

This code tries to filter a list to keep only even numbers, but it returns the odd numbers instead. Fix the lambda so it correctly keeps even numbers.

Loading editor...
#12Write a Memoize Decorator
Refactor

The recursive fib function below is slow for large inputs because it recalculates the same values. Write a memoize decorator function that caches results, then apply it to fib using @memoize. The decorator should work with any single-argument function.

Loading editor...