TwiddleLab / Courses / Code
Recursion: Functions That Call Themselves
Functions that call themselves: watch call trees grow, draw fractals and solve mazes.
Best after: Build with Python, Algorithms in Motion
Lessons
- A Function That Calls Itself (5 min): Recursion is a function that calls itself on a smaller version of the job.
- The Base Case (7 min): The base case is the line that answers without calling again. It is the floor.
- Trust the Smaller Problem (7 min): Recursive thinking: do one small piece yourself and trust a smaller call with the rest.
- Trees of Calls (7 min): A function that calls itself twice makes a call tree: every circle is one call.
- Remember Answers: Memoization (7 min): Memoization: keep a notebook of answers. Before working something out, look it up; after, write it down.
- Towers of Hanoi (7 min): A recursive plan: to move n disks, move n - 1 out of the way, move the biggest, then move the n - 1 back on top.
- Drawing Fractals (7 min): A fractal is a shape made of smaller copies of itself: a tree is a trunk with two smaller trees on top.
- Divide and Conquer (7 min): Divide and conquer: split the problem into smaller parts, solve each part with the same function, combine the answers.
- Generate Everything: Subsets and Orders (7 min): Brute force: list every possibility and check each one.
- Backtracking: Solving a Maze (7 min): Backtracking: try a choice and go deeper; at a dead end, undo the last choice and try the next one.
- Sudoku Solver (7 min): A constraint is a rule every answer must keep: no digit twice in a row, column or box.
Lessons are interactive and need JavaScript. Turn it on to play them.