The CTP Book

A book for teaching Computational Thinking and Programming skills to people with a background in the Humanities

View on GitHub

Chapter “Recursion”, exercise 1

Text

Define a recursive function def exponentiation(base_number, exponent) for implementing the exponentiation operation. Test (by implementing the related test case) it on the following inputs: 34, 171, and 20.

Answer

# Test case for the function
def test_exponentation(base_number, exponent, expected):
    result = exponentation(base_number, exponent)
    if expected == result:
        return True
    else:
        return False


# Code of the function
def exponentation(base_number, exponent):
    if exponent == 0:
        return 1
    else:
        return base_number * exponentation(base_number, exponent - 1)


# Tests
print(test_exponentation(3, 4, 81))
print(test_exponentation(17, 1, 17))
print(test_exponentation(2, 0, 1))
print(test_exponentation(0, 15, 0))
print(test_exponentation(0, 0, 1))

Additional material

The runnable Python file is available online.