The recursion notebook / Python 3

One line. One call.
A clearer picture.

Recursion is easier to follow when you can see what each call is doing. Step through three Python examples, watch values change, and follow the work down the stack and back up.

  1. 01
    Predict

    What will the highlighted line do?

  2. 02
    Step

    Execute it and watch the state change.

  3. 03
    Inspect

    Select any stack frame to see its values.

A smaller version of the same problem

Move the tower. Keep the rules.

Move every disk from A to C, using B as a spare. Move one disk at a time, and never put a larger disk on a smaller one.

Base casen == 1: print one move and return.

Smaller problemMove n - 1 disks.

Then returnResume the caller where it paused.

Loading Python…

Program state

Disk numbers show size. Peg lists run from bottom to top.

Next line

Preparing the example…

Python source

→ Highlighted line executes on the next step.

Call stack

Newest call at the top. Select a frame to inspect it.

Variables

Trace console

Most recent transitions

    Python output

    This is a line-by-line teaching simulation of the Python functions, not an editable Python interpreter. Back and the slider replay recorded states. The downloadable scripts run in Python 3 without extra packages.

    The instructor’s recursive example

    Three jobs, one function.

    For n greater than 1, move n − 1 disks onto the auxiliary rod, print the move of disk n to the destination, then move the smaller tower onto it. At n == 1, print the move and return.

    Each call has its own n, source, destination, and auxiliary. The Python function prints instructions; the rods and move log are visual aids added by this page.

    For positive n, the number of moves is 2ⁿ − 1. Time is O(2ⁿ), and recursive stack space is O(n). The supplied function requires n ≥ 1.

    Instructor’s iterative comparison (reference code)

    This is the supplied iterative code, included in the download. Its src, aux, and des variables are not used by the fixed rod-pair logic, so swapping them has no effect. It finishes on B for odd disk counts and C for even counts. The recursive example above always targets C. Playback traces the recursive version.

    Pause & predict

    Follow the rod roles.

    With 3 disks, what is the first move?

    Disk 1 from A to C. Follow the calls until n == 1 and inspect source and destination.

    Why is there no n == 0 call?

    The n == 1 case prints and returns before either recursive call.

    How many moves do 4 disks need?

    15: seven moves, one largest-disk move, then seven more.

    What does the function return?

    None. The base case returns explicitly, while larger calls return implicitly after their last child finishes. The useful output is printed.

    Keep exploring

    Take the Python with you.

    Download an example, save it in a folder, and run it in your terminal. The script includes its sample input and prints the result. Change the disk count or the maze and predict the output before running it again.

    The downloaded maze starts with the original “Instructor’s 6×6 example” layout. Your browser edits are exploratory and are not saved into that file. The Sudoku download uses the “Nine missing numbers” example.

    Python 3 / no extra packagespython tower_of_hanoi.py
    python maze.py
    python sudoku.py
    Download HanoiDownload mazeDownload Sudoku