Skip to content

Question 24

What is the output of the following code?

1
2
3
4
5
6
7
8
def perimeter(sides):
    if sides == 0:
        return lambda r: 2 * 3.142 * r
    if sides >= 3:
        return lambda l: l * sides
    return 0

print(perimeter(5)(28))
Solution

By guessing, you could imply 5 * 28 = 140 and simply pick the answer from there.

Realistically, perimeter() becomes a circumference function if sides = 0, a normal perimeter function for all regular polygons if sides = 3, and returns 0 otherwise. Either one of the circumference or regular polygon perimeter function then takes one parameter each to use for calculating its respective values.

Here, this means that perimeter(5) becomes a perimeter function for all regular polygons. By passing 28 into this function, perimeter(5)(28) leads to l = 28 and hence it returns l * sides = 28 * 5 = 140.

Answer
140