304. Python Program to Print the Moves of the Tower of Hanoi
HardPhase 3 · Challenge
You know functions with parameters, return and if. One idea is new: a function that calls itself. The statement explains it step by step. Play the puzzle with 3 coins of different sizes on paper first.
Overview
The Tower of Hanoi is a famous puzzle. It shows a strong idea: a function can call itself to solve a smaller copy of the same problem. This is called recursion. The statement shows you how it works.
Problem Statement
Haruto got a wooden Tower of Hanoi as a gift and wants a list of the moves. There are 3 pegs called A, B and C. Peg A holds n discs. Disc 1 is the smallest, disc n is the biggest, and the biggest is at the bottom. Move all the discs to peg C. The rules:
- Move one disc at a time: the top disc of one peg goes onto another peg.
- Never put a bigger disc on a smaller disc.
- Use the fewest moves. There is only one way to do it in the fewest moves.
The plan. To move n discs from A to C:
- Move the top
n - 1discs out of the way, from A to B. - Move disc
n, the biggest, from A to C. - Move the
n - 1discs from B onto it, on C.
Steps 1 and 3 are the same puzzle again, only with one disc less and the pegs in a new order.
A function that calls itself. The chapters have not shown this yet, but it is a normal function call. A function may call itself. When it does, the first call waits. The inner call runs to its end and returns its value. Then the first call goes on from the line where it stopped. Here each inner call has one disc less, so the discs run out. With 0 discs there is nothing to move, so that call returns at once. This stop is called the base case. Without it, the calls would never end.
For 2 discs, the calls go like this:
- The call "move 2 discs from A to C" starts the call "move 1 disc from A to B" and waits. That smaller call prints
Move disc 1 from A to Band returns. - The first call goes on. It prints
Move disc 2 from A to C. - Then it starts the call "move 1 disc from B to C". That call prints
Move disc 1 from B to Cand returns.
Print each move on its own line in the format Move disc 1 from A to C. At the end, print the number of moves in the format Total moves: 7.
Examples
Example 1. Input 2 gives:
Move disc 1 from A to B
Move disc 2 from A to C
Move disc 1 from B to C
Total moves: 3
Why: disc 1 moves out of the way to B. Disc 2 can then go to C, and disc 1 goes on top of it.
Example 2. Input 3 gives:
Move disc 1 from A to C
Move disc 2 from A to B
Move disc 1 from C to B
Move disc 3 from A to C
Move disc 1 from B to A
Move disc 2 from B to C
Move disc 1 from A to C
Total moves: 7
Why: the first 3 moves take discs 1 and 2 from A to B, using C as the spare peg. Disc 3 moves to C. The last 3 moves take discs 1 and 2 from B to C, using A as the spare peg.
Input
One whole number n, the number of discs (1 to 7). Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
One line for each move, then the Total moves: line. The tests try 7 different inputs.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.