AddisCoder: Week 2
Lecture 7A:
Recursion
Lecture Idea Outline
Context
We know how to define and call functions.
def sumTwo(x, y): return x + y def sumFour(a, b, c, d): return a + b + c + d print(sumTwo(1, 2)) # == 3 print(sumFour(9, 8, 7, 6)) # == 30 |
Context
We know how to call functions from other functions.
def sumTwo(x, y): return x + y def sumFour(a, b, c, d): return sumTwo(a, b) + sumTwo(c, d) print(sumTwo(1, 2)) # == 3 print(sumFour(9, 8, 7, 6)) # == 30 |
Functions Calling Themselves
Let’s make a more general sum function without using iteration (for loops).
General Steps for Recursion
When to Use Recursion?
Recursion is used when a problem can be easily divided into smaller problems of the same form.
Are each of these well-suited to be written recursively?
each step is different: find brush, apply paste, etc.
pick 1 coin, make change for remaining amount
add 1 number to sum of remaining list
no substeps: single conditional decision only
Why do we do this?
Many function can be written either as a loop or a recursion. However, for some more complex problems, the recursive implementation is much more elegant and easy to reason about.
Fibonacci
Fibonacci Sequence
Special sequence of numbers where a number is the sum of last two numbers
Start with 0 and 1
0, 1
Fibonacci Sequence
Special sequence of numbers where a number is the sum of last two numbers
Start with 0 and 1
0, 1,
+
0
1
1
Fibonacci Sequence
Special sequence of numbers where a number is the sum of last two numbers
Start with 0 and 1
0, 1,
+
1
1
1,
2
Fibonacci Sequence
Special sequence of numbers where a number is the sum of last two numbers
Start with 0 and 1
0, 1,
+
1
2
1,
2,
3
Fibonacci Sequence
Special sequence of numbers where a number is the sum of last two numbers
Start with 0 and 1
0, 1,
+
2
3
1,
2,
3,
5
Fibonacci Sequence
Special sequence of numbers where a number is the sum of last two numbers
Start with 0 and 1
0, 1,
+
3
5
1,
2,
3,
5,
8
Fibonacci Sequence
Special sequence of numbers where a number is the sum of last two numbers
Start with 0 and 1
0, 1,
+
5
8
1,
2,
3,
5,
8,
13
Fibonacci Sequence
Special sequence of numbers where a number is the sum of last two numbers
Start with 0 and 1
0, 1,
+
8
13
1,
2,
3,
5,
8,
13,
21
Fibonacci Sequence
Special sequence of numbers where a number is the sum of last two numbers
Start with 0 and 1
0, 1, 1, 2, 3, 5, 8, 13, 21...
Code Fibonacci Sequence
Function: fibonacci()
Input: number n
Output: nth value in Fibonacci Sequence
Examples:
fibonacci(0) -> 0
fibonacci(1) -> 1
fibonacci(2) -> 1
fibonacci(3) -> 2
fibonacci(4) -> 3
fibonacci(5) -> 5
fibonacci(6) -> 8
fibonacci(7) -> 13
Recursion
Way of describing problems
Way of designing solutions
Base case - simplest version of problem
Inductive case - how reduce problem to simpler versions of same problem
Base Case
Gives direct answer
Simple
Recursive / inductive case
Example: Exponentiation
Example: Exponentiation
def exponent(b, n):
if n == 0:
return 1
return b * exponent(b, n - 1)
Another Exponentiation
Example: Exponentiation
def exponent(b, n):
if n == 0:
return 1
else:
m = n//2
bm = exponent(b, m)
if n % 2 == 0:
return bm*bm
else:
return bm*bm*b