Project: Overview
- Algorithms course
- Beginner
- Lesson 49/80
- 4 min
Plan a recursive mini app for powers and combinations.
Project: Recursive calculations
In this project you will build an application that computes power, combination and fast power recursively. Each lesson builds on the previous one.
You will first add power, then combination, then result printing and finally fast power.
Power logic
Power: base^exp = base * base^(exp-1). The base case is 1 when exp=0. This is a classic recursion example.
Each call decreases exp by one and eventually reaches 0. This is the termination condition provided by the base case.
Practice Power (recursive)
Write power(base, exp). If exp=0 return 1, else base * power(base, exp-1). Print 32 with print(power(2, 5)).
Starter code
def power(base, exp):
# Compute powerSample solution
def power(base, exp):
# Compute power
if exp == 0:
return 1
return base * power(base, exp - 1)
print(power(2, 5))Expected output
32
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the first step of the project?
- Printing
- Power
- Fast power
- Combination Correct answer
Explanation: The first step is power.
2 Project: Overview: match the terms
| Term | Meaning |
|---|---|
power |
Power |
combination |
Combination |
print_results |
|
fast_power |
Fast power |
3 Base case
If exp = ____, return 1.
If exp = 0, return 1.
Explanation: 0 is right.