Project: tower of Hanoi
Python · Unit 11: Recursion
- Full plan
- Python
- Project
- 3 exercises
The tower of Hanoi: there are three pegs, A, B and C, and several disks stacked on A, from the biggest at the bottom to the smallest on top.
You have to move them all to C, one at a time, never putting a big disk on top of a small one.
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Complete the code
To count the moves, n disks cost twice what n - 1 cost, plus the move of the big disk. Fill in the blank.
2. Write the code
Write
moves(n), which returns how many moves it takes for n disks. With 0 disks it's 0.3. Write the code
Now the list of moves.
move(n, fr, to, via)returns each move as a two-letter string, like "AC".firstalready has the moves that clear the way: add the big disk's move and the ones that come after.
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.