ECS 36A Lecture 8: Recursions

127 views1 pages
Published on 6 Nov 2018
ECS 36A - Lecture 8 - Recursions
A recursion is when a function calls on itself in its own definition. This helps expedite certain
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1) within factorial(n), factorial (n-1) is called
The Tower of Hanoi:
is a classical example involving the redistribution of disks in a new order. Though visualization
helps with this concept, below is a program that reimagines it through code.
def hanoi(n, src, dst, tmp):
if n > 0:
hanoi(n - 1, src, tmp, dst)
move(src, dst)
hanoi(n - 1, tmp, dst, src)
- move src peg to dst peg
- can use tmp peg
def hanoi_(n-1) (src, dst, tmp):
Goals of Recursions
* Explore all states
* Explore all branches
* Create input that cause errors
Unlock document

This preview shows half of the first page of the document.
Unlock all 1 pages and 3 million more documents.

Already have an account? Log in

Get OneClass Notes+

Unlimited access to class notes and textbook notes.

YearlyBest Value
75% OFF
$8 USD/m
$30 USD/m
You will be charged $96 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.
Payments are encrypted using 256-bit SSL. Powered by Stripe.