You are on page 1of 4

Python Program to Implement Tower of Hanoi

AIM

This is a Python program to implement Tower of Hanoi.

OBJECTIVE

The objective of the puzzle is to move the entire stack to another rod, obeying the following
rules Only one disk can be moved at a time.

PROCEDURE

1.Create function hanoi that takes the number of disks n and the names of the source,
auxiliary and target pegs as arguments.

2. The base case is when the number of disks is 1, in which case simply move the
one disk from source to target and return.
3. Move n – 1 disks from source peg to auxiliary peg using the target peg as the
auxiliary.
4. Move the one remaining disk on the source to the target.
5. Move the n – 1 disks on the auxiliary peg to the target peg using the source peg
as the auxiliary.

Algorithm
To write an algorithm for Tower of Hanoi, first we need to learn how to solve this problem with
lesser amount of disks, say → 1 or 2. We mark three towers with
name, source, destination and aux (only to help moving the disks). If we have only one disk,
then it can easily be moved from source to destination peg.

If we have 2 disks −

 First, we move the smaller (top) disk to aux peg.


 Then, we move the larger (bottom) disk to destination peg.
 And finally, we move the smaller disk from aux to destination peg.
 So now, we are in a position to design an algorithm for Tower of Hanoi with more
than two disks. We divide the stack of disks in two parts. The largest disk
(nth disk) is in one part and all other (n-1) disks are in the second part.

1
 Our ultimate aim is to move disk n from source to destination and then put all
other (n1) disks onto it. We can imagine to apply the same in a recursive way for
all given set of disks.

 The steps to follow are –

 Step 1 − Move n-1 disks from source to aux


 Step 2 − Move nth disk from source to dest
 Step 3 − Move n-1 disks from aux to dest
 A recursive algorithm for Tower of Hanoi can be driven as follows −
 START
 Procedure Hanoi(disk, source, dest, aux)

 IF disk == 1, THEN
 move disk from source to dest
 ELSE
 Hanoi(disk - 1, source, aux, dest) // Step 1
 move disk from source to dest // Step 2
 Hanoi(disk - 1, aux, dest, source) // Step 3
 END IF

 END Procedure
 STOP

PROGRAM
Here is the source code of a Python program to implement Tower of Hanoi. The program
output is shown below.
def hanoi(disks, source, auxiliary, target):
if disks == 1:
print('Move disk 1 from peg {} to peg {}.'.format(source, target))
return
 
hanoi(disks - 1, source, target, auxiliary)
print('Move disk {} from peg {} to peg {}.'.format(disks, source, target))
hanoi(disks - 1, auxiliary, source, target)
 

2
 
disks = int(input('Enter number of disks: '))
hanoi(disks, 'A', 'B', 'C'

Program Explanation
1. The user is prompted for the number of disks n.
2. The function hanoi is called on n with names of the source, auxiliary and target pegs as
A, B and C respectively.

Runtime Test Cases


Case 1:
Enter number of disks: 4
Move disk 1 from peg A to peg B.
Move disk 2 from peg A to peg C.
Move disk 1 from peg B to peg C.
Move disk 3 from peg A to peg B.
Move disk 1 from peg C to peg A.
Move disk 2 from peg C to peg B.
Move disk 1 from peg A to peg B.
Move disk 4 from peg A to peg C.
Move disk 1 from peg B to peg C.
Move disk 2 from peg B to peg A.
Move disk 1 from peg C to peg A.
Move disk 3 from peg B to peg C.
Move disk 1 from peg A to peg B.
Move disk 2 from peg A to peg C.
Move disk 1 from peg B to peg C.
 
Case 2:
Enter number of disks: 2
Move disk 1 from peg A to peg B.
Move disk 2 from peg A to peg C.
Move disk 1 from peg B to peg C.
 
Case 3:
Enter number of disks: 1
Move disk 1 from peg A to peg C.

OUTPUT

3
RESULT
Thus the python program for towers Hanoi is excecuted and output is obtained
successfully.

You might also like