ECS 36A Lecture 8: Recursions
ECS 36A verified notes
8/21View all
Document Summary
Is when a function calls on itself in its own definition. A recursion processes. def factorial(n): if n == 1: return 1 else: return n * factorial(n - 1) 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) Can use tmp peg def hanoi_(n-1) (src, dst, tmp): pass.