![Page 1: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/1.jpg)
Fruitful and Graphical Recursion
![Page 2: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/2.jpg)
• Base case: Solving problem directly. • Recursive case:
• REDUCE the problem to smaller subproblem(s) (smaller version(s) of itself)
• DELEGATE the smaller problems to the recursion fairy (formally known as induction hypothesis) and assume they're solved correctly
• COMBINE the solution(s) of the smaller subproblems to reach/return the solution
Recursive Algorithm
![Page 3: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/3.jpg)
• We say a recursion is fruitful if the recursive function returns a value (other than None)
Fruitful Recursion
![Page 4: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/4.jpg)
• Let’s write a fruitful recursive function that sums up integers from 1 down to n (without loops)
• Recursive case. (REDUCE/ DELEGATE/ COMBINE): Can think of sum(5) as 5 + sumUp(4)
sumUp(n)
In[1] sumUp(5)
Out[1] 15
In[2] sumUP(10)
Out[2] 55
![Page 5: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/5.jpg)
Unfolding the Recursion
![Page 6: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/6.jpg)
Fruitful Recursion: Base Case(s) Required!
What happens if we eliminate the base case
for sumUp?
![Page 7: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/7.jpg)
PalindromesEVE
CIVICMADAM
AVID DIVASTEP ON NO PETS
STRESSED DESSERTSABLE WAS I ERE I SAW ELBA
LIVED ON DECAF FACED NO DEVIL
![Page 8: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/8.jpg)
• REDUCE it smaller version of the same problem
• Check if s' = s[1:-1] is a palindrome
Recursive Approach
0
s
palindrome(s')-1
palindrome(s)
![Page 9: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/9.jpg)
• DELEGATE the smaller problems to the recursion fairy (formally known as induction hypothesis) and assume they're solved correctly
Recursive Approach
s
palindrome(s')0 -1
palindrome(s)
![Page 10: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/10.jpg)
• COMBINE the solution(s) of the smaller subproblems to reach/return the solution
• return True if palindrome(s') is True and s[0] is same as s[-1]
Recursive Approach
s
palindrome(s')0 -1
palindrome(s)
![Page 11: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/11.jpg)
Factorial
Factorial. Denoted n!
number of different the arrangements of n items.n! = n * (n − 1) * (n − 2) * . . . . * 2 * 1
3 items were arranged in 6 different ways. Or 3x2x1.
![Page 12: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/12.jpg)
•
•
• Recursive case. factorial(n) is n * factorial(n-1)
• Base case.factorial(0) = 1
n! = n * (n − 1) * (n − 2) * … * 2 * 1
n! = n * (n − 1)!
factorial(n)
![Page 13: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/13.jpg)
• Fruitful recursion: recursion that "computes and returns" values
• Remember to implement the base case!
• Remember to store the value returned by recursive calls!
• Debug using print statements
Summary
![Page 14: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/14.jpg)
Recursion with Turtle Graphics
![Page 15: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/15.jpg)
• Python has a built-in module named turtle. See the Python turtle module API for details.
Turtle
![Page 16: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/16.jpg)
Playing with Turtle:polyFlow
![Page 17: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/17.jpg)
The Sun totally ruined by plans! You can't see anything....
This is what I am drawing
120 degrees
![Page 18: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/18.jpg)
Graphical Recursion
![Page 19: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/19.jpg)
• Graphical recursion with a single recursive call
• Fruitful recursion with turtles
• Learn about function invariance in anticipation of multiple recursive calls
Overview
Sierpinski's Triangle
![Page 20: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/20.jpg)
Single Recursive Call: Recursive Spirals
![Page 21: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/21.jpg)
Recursive Spirals
sideLen
sideLen * shrinkFactor
sideLen * shrinkFactor * shrinkFactor
![Page 22: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/22.jpg)
Function Frame Model to
Understand spiral
![Page 23: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/23.jpg)
![Page 24: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/24.jpg)
spiral(625, 90, 0.8, 250)
625sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(500, ...)
625
![Page 25: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/25.jpg)
spiral(625, 90, 0.8, 250)
625sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(500, ...)
625
![Page 26: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/26.jpg)
spiral(625, 90, 0.8, 250)
625sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(500, ...)
625
spiral(500, 90, 0.8, 250)
500sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(400, ...)
![Page 27: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/27.jpg)
spiral(625, 90, 0.8, 250)
625sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(500, ...)
625
spiral(500, 90, 0.8, 250)
500sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(400, ...)
500
![Page 28: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/28.jpg)
spiral(625, 90, 0.8, 250)
625sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(500, ...)
625
spiral(500, 90, 0.8, 250)
500sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(400, ...)
500
![Page 29: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/29.jpg)
spiral(625, 90, 0.8, 250)
625sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(500, ...)
625
spiral(500, 90, 0.8, 250)
500sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(400, ...)
500
spiral(400, 90, 0.8, 250)
400sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(320, ...)
![Page 30: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/30.jpg)
spiral(625, 90, 0.8, 250)
625sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(500, ...)
625
spiral(500, 90, 0.8, 250)
500sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(400, ...)
500
spiral(400, 90, 0.8, 250)
400sideLen
if sideLen > 250: fd(sideLen) lt(90) spiral(320, ...)
![Page 31: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/31.jpg)
Invariant Spiralling
![Page 32: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/32.jpg)
• A function is invariant relative to an objects state if the state of the object is the same before and after a function is invoked
Invariance
![Page 33: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/33.jpg)
Fruitful Recursion with Turtles
See Lecture Jupyter Notebook
![Page 34: Fruitful and Graphical Recursion - GitHub Pages](https://reader034.vdocuments.site/reader034/viewer/2022050403/62701826d862507f1c34483c/html5/thumbnails/34.jpg)
These slides have been adapted from: • http://cs111.wellesley.edu/spring19 and
• https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-programming-in-python-fall-2016/
Acknowledgments