Recursion
Recursion
CSCI - 135
Spring 2023
Anietie Andy
Announcements
Function defined
here!
Function calls
itself!
https://repl.it/@DrSkary/RecursionIntro
But, how does this work?
We need to know how functions work first! Look at the a non-
recursive example below.
Function called with
num=3
When function_b is
called and it returns,
how do we know the
value of y and num?
https://repl.it/@DrSkary/NonRecursion
Call Stack (Run-time Stack)
Function called with
num=3
Call Stack
Call Stack (Run-time Stack)
Call Stack
Local variable y
created and value
changed to 10.
Call Stack (Run-time Stack)
Call Stack
Call Stack
fucntion_b returns
value of
6.
Call Stack
return 16.
function_a will
be popped from
stack too.
Call Stack and Recursion
Call Stack
Call Stack and Recursion
Call Stack
Call Stack and Recursion
Call Stack
Call Stack and Recursion
Call Stack
Call Stack and Recursion
Call Stack
Call Stack and Recursion
Call Stack
Call Stack and Recursion
Call Stack
Call Stack and Recursion
Call Stack
Call Stack and Recursion
Call Stack
Output
6
Recursive Algorithm Design - How to think?
1. Which is the smallest sub-problem that can solved?
Base Case
How are solutions of smaller problems combined to form answer to the larger
problem?
Composition
Recursion Design Example: Factorial
Factorial
Factorial is a math operation denoted with the !
operator.
The factorial of a number is equal to: multiplying
the number by every number between it and 1.
Factorial of a negative number is undefined
1! = 1
5! = 1 * 2 * 3 * 4 * 5 = 120
Factorial
Factorial is a math operation denoted with the !
operator.
The factorial of a number is equal to: multiplying
the number by every other number between it and
1.
Factorial of a negative number is undefined
Base Case
Recursive
Case
In-Class Exercise
A = [1,3,5,6,7,10]
def list_sum(num_List):
if len(num_List) == 1:
return num_List[0]
else:
return num_List[0] + list_sum(num_List[1:])
print(list(A))
Write python code to calculate the value of 'x' to the power of 'y'
def power(num, top):
if top == 0:
return 1
else:
return num * power(num, top - 1)
def power(a,b):
if b==0:
return 1
elif a==0:
return 0
elif b==1:
return a
else:
return a*power(a,b-1)
Palindrome
Given a string return true if it’s a palindrome, otherwise return false.
A string is a palindrome if it’s the same forwards as backwards
Base case:
1) empty string or string size 1
Recursive case:
Remove first & last characters recursive call with a shorter string
Palindrome
Base Case 1
def is_palindrome(test):
if not text or len(text) == 1:
return True
if (text[0] != text[-1]): Base Case 2
return False
return is_palindrome(text[1:-1])
Recursive Case 2
Recursion Design Review: Recursive sum of
all numbers < num
Base (or Termination)
Case
Recursive Case
Composition happens
here
Decomposition happens
here
Factorial
Call Stack
factorial(0)
Line: 5
factorial(1)
Line: 5
factorial(2)
Line: 5
factorial(3)
Line: 8
__main__
done
Factorial - Iterative vs Recursive
Recursive:
2. Recursive case:
a. At least 1 recursive case is required! Can be more than 1!
b. Use recursive call for decomposition.
c. Use result from recursive calls for composition.
No Base Case Example
If the recursive case does not exist, it becomes a simple function.
https://replit.com/@DrSkary/InfiteRecursion
Google’s Easter Egg - The cult of Recursion
Every number (but the first two) is the sum of the two
numbers before it.
https://repl.it/@DrSkary/recurseFibo
Why isn’t recursion as commonly used?
Used for specific problems.
https://repl.it/@DrSkary/recurseFiboTraced
Don’t believe me? Let’s run this code here for n = 35.
https://repl.it/@DrSkary/compareFibos
To Recur or not to Recur?
Don’t prefer as the first solution for most problems. Prefer the
closed-loop variant when possible.
When the function is working with immutable data i.e data does
not change. Usually in a for-loop, you loop through to change data.
Next Class is Midterm Review!
Review Exam Structure
Do some sample questions
Ask anything you need more clarification on.
Base case:
1) empty string or string size 1
Recursive case:
Remove first & last characters recursive call with a shorter string
Palindrome
Base Case 1
def is_palindrome(test):
if not text or len(text) == 1:
return True
if (text[0] != text[-1]): Base Case 2
return False
return is_palindrome(text[1:-1])
Recursive Case 2
Questions