...

/

Puzzles 38 to 41

Puzzles 38 to 41

Examine the lambda function, multi-line strings, escaping, and Fibonacci in Python.

Puzzle 38

What is the output of the following code?

Note: Enter the output you guessed. If your guess is multi-lined, enter each value separated by the spacebar. Then, press the Run button, and your new Elo will be calculated. Don’t forget to save your Elo rating.

Press + to interact
Elo
1000
#############################
## id 370
## Puzzle Elo 1558
## Correctly solved 89 %
#############################
def make_incrementor(n):
return lambda x: x + n
f = make_incrementor(42)
print(f(0))
print(f(1))

Enter the input below

The lambda function

This puzzle introduces an advanced language feature: lambda functions. Lambda functions exist in a wide range of languages for functional programming.

A lambda function is an anonymous function without an identifier.

After the lambda keyword, the function takes one or more arbitrary arguments. The arguments are comma-separated and finished by a colon. After, the colon follows a single expression. Yet, this expression can consist of complex calculations using the specified argument variables. The lambda function then returns the result of this expression. Hence, lambda functions are syntactical shortcuts for a subclass of normal Python functions.

In the puzzle, the function make_incrementor creates a lambda function at runtime. The created lambda function increases an element x by a fixed value n. For example, the incrementor function in the puzzle increments a value by 42. We assign this function to the variable f. Then, we print the results when incrementing the values 0 and 1 by the incrementor 42 ...