Recursion
Jake Shoudy
Nov 16, 2022
CSCI 110 - Lecture 32
Announcements
Career week!
All recordings/slides available here (also on class website)
PLEASE fill out quick feedback form for each talk you attended
Log your own PD hours via the links here
Quiz 10
Last quiz this Friday!
Meet in lab downstairs
Will cover Classes and sorting
HW10
Last assignment
Classes and sorting
Due Monday November 21st at 11:59pm
Final Exam Date!
Tuesday 11/29 during normal lab hours
Final Exam Topics
Binary <-> Base 10
Data types
Operators (arithmetic, comparison, logical)
Variables
if / elif / else
while loops
Strings (length, index, slice)
Functions
For Loops
Will include short answer & coding questions
Lists
2D Lists
Big O
Sets
Dictionaries
Nested Dictionaries
Scope
Pass by Reference vs Pass by Value
Classes
Sorting
Reminder: NO CLASS NEXT WEEK
Cancelling class on Monday and Tuesday
Wednesday - Friday are school holidays
There will be a Kahoot review session on Monday the 28th
Recap
.sort()
sorted()
Sorting students
students = [[‘Nirmal’, 39], [‘Michael’, 18], [‘Jeremy’, 19], [‘Constance’, 18], [‘Andre’, 19], [‘Deirdre’, 18], [‘Zade’, 19], [‘Conner’, 18]]
students.sort()
[[‘Andre’, 19], [‘Conner’, 18], [‘Constance’, 18], [‘Deirdre’, 18], [‘Jeremy’, 19], [‘Michael’, 18], [‘Nirmal’, 39], [‘Zade’, 19]]
Sorting students by age
students = [[‘Nirmal’, 39], [‘Michael’, 18], [‘Jeremy’, 19], [‘Constance’, 18], [‘Andre’, 19], [‘Deirdre’, 18], [‘Zade’, 19], [‘Conner’, 18]]
students.sort(key=lambda student : student[1])
[[‘Michael’, 18], [‘Constance’, 18], [‘Deirdre’, 18], [‘Conner’, 18], [‘Jeremy’, 19], [‘Andre’, 19], [‘Zade’, 19], [‘Nirmal’, 39]]
Using name as a secondary “tie breaker”
students = [[‘Nirmal’, 39], [‘Michael’, 18], [‘Jeremy’, 19], [‘Constance’, 18], [‘Andre’, 19], [‘Deirdre’, 18], [‘Zade’, 19], [‘Conner’, 18]]
students.sort(key=lambda student : (student[1], student[0]))
[[‘Conner’, 18], [‘Constance’, 18], [‘Deirdre’, 18], [‘Michael’, 18], [‘Andre’, 19], [‘Jeremy’, 19], [‘Zade’, 19], [‘Nirmal’, 39]]
Using name as a secondary “tie breaker” v2
students = [[‘Nirmal’, 39], [‘Michael’, 18], [‘Jeremy’, 19], [‘Constance’, 18], [‘Andre’, 19], [‘Deirdre’, 18], [‘Zade’, 19], [‘Conner’, 18]]
students.sort(key=lambda student : student[0])
[[‘Andre’, 19], [‘Conner’, 18], [‘Constance’, 18], [‘Deirdre’, 18], [‘Jeremy’, 19], [‘Michael’, 18], [‘Nirmal’, 39], [‘Zade’, 19]]
students.sort(key=lambda student : student[1])
[[‘Conner’, 18], [‘Constance’, 18], [‘Deirdre’, 18], [‘Michael’, 18], [‘Andre’, 19], [‘Jeremy’, 19], [‘Zade’, 19], [‘Nirmal’, 39]]
Sort by age descending
students = [[‘Nirmal’, 39], [‘Michael’, 18], [‘Jeremy’, 19], [‘Constance’, 18], [‘Andre’, 19], [‘Deirdre’, 18], [‘Zade’, 19], [‘Conner’, 18]]
students.sort(key=lambda student : (student[1], student[0]), reverse=True)
[[‘Nirmal’, 39], [‘Zade’, 19], [‘Jeremy’, 19], [‘Andre’, 19], [‘Michael’, 18], [‘Deirdre’, 18], [‘Constance’, 18], [‘Conner’, 18]]
Sorting Runtime
Different sorting algorithms also have different runtimes.
The default sort in every programming language has time complexity:
O(n log(n))
Recursion
Calling functions from other functions
def add_one(num):
return num + 1
def add_five(num):
for i in range(5):
num = add_one(num)
return num
print(add_five(10))
15
Calling functions from within that function
def add_one(num):
num = add_one(num) + 1
return num
print(add_one(10))
RecursionError: maximum recursion depth exceeded
Imagine you are waiting in a line…
Imagine you are waiting in a line…
How do you determine how long the line is?
You could just get out of line and count!
What if it is a really long line?
Imagine you are waiting in a line…
How do you determine how long the line is?
You could still count! But it might take awhile…
What if no one will save your spot in line? Getting out of line would make you have to go to the end :/
It is too far to see the entire line from where you are standing
Idea!
Ask your neighbor! Maybe they know?
me
Hey do you know how long this line is?
Idea!
me
Nope but I can ask!
Idea!
me
Hey do you know how long this line is?
Idea!
me
Nope but I can ask!
Idea!
me
Hey do you know how long this line is?
Idea!
me
Nope but I can ask!
Idea!
me
Hey do you know how long this line is?
Idea!
me
Yes i’m actually first in line!
Idea!
me
I’m second in line!
Idea!
me
I’m third in line!
Idea!
me
I’m fourth in line!
Idea!
me
I guess I’m fifth!
Recursion
Recursion
How long is the line?
def line_length(person):
if (you are first in line):
return 1
else:
# ask neighbor what position they are in and wait
# for their response. Add 1
return line_length(neighbor) + 1
Base Case
Recursive Case
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
Code using a loop
def fibonacci(num):
if num <= 1:
return num
num_1 = 0
num_2 = 1
for i in range(num - 1):
temp = num_2
num_2 = num_2 + num_1
num_1 = temp
return num_2
Thinking recursively
Can I define this problem in terms of itself?
Hint: what if I start from the top instead of from the bottom?
fibonacci(4) -> 3
fibonacci(5) -> 5
fibonacci(6) -> 8
fibonacci(7) -> 13
fibonacci(0) -> 0
fibonacci(1) -> 1
fibonacci(2) -> 1
fibonacci(3) -> 2
Thinking recursively
f(5) = f(4) + f(3)
f(3) + f(2)
f(2) + f(1)
f(2) + f(1)
f(1) + f(0)
f(1) + f(0)
f(1) + f(0)
Thinking recursively
f(5) = f(4) + f(3)
f(3) + f(2)
f(2) + f(1)
f(2) + f(1)
f(1) + f(0)
f(1) + f(0)
f(1) + f(0)
Thinking recursively
f(5) = f(4) + f(3)
f(3) + f(2)
f(2) + 1
f(2) + 1
1 + 0
1 + 0
1 + 0
Thinking recursively
f(5) = f(4) + f(3)
f(3) + 1
1 + 1
1 + 1
Thinking recursively
f(5) = f(4) + 2
2 + 1
Thinking recursively
f(5) = 3 + 2
Thinking recursively
f(5) = 5
Code Fibonacci Sequence
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
Base Case
Recursive Case
Motivation
Why learn recursion?
It’s beautiful!!
Population count
def update_population_count(population):
world_population = 0
for continent in population:
continent_population = 0
for country in population[continent]:
country_population = 0
for state in population[continent][country]:
state_population = 0
for city in population[continent][country][state]:
state_population += population[continent][country][state][city]
population[continent][country][state]['total'] = state_population
country_population += state_population
population[continent][country]['total'] = country_population
continent_population += country_population
population[continent]['total'] = continent_population
world_population += continent_population
population['total'] = world_population
Population count
def update_population_count(population):
if type(population) != dict:
return population
else:
total = 0
for key in population:
total += update_population_count(population[key])
population['total'] = total
return total
Recursion in Nature (Self Similarity)
Recursion in Nature (Self Similarity)
Recursion in Nature (Self Similarity)
Recursive art!