Tower of Hanoi Python
In this post, you will learn the Tower of Hanoi program using the Python programming language.
The Tower of Hanoi is a mathematical puzzle containing 3 pillars/towers with n disks each of a different size/diameter.
Given a stack of n disks arranged from biggest on the bottom to smallest on top put on a rod, along with two empty rods, the tower of Hanoi puzzle asks for the minimum number of moves required to move the stack from one rod to another, where moves are allowed only if they place smaller disks on the top of bigger disks.
Rules of Tower of Hanoi Problem
The rules for this puzzle are-
- We can move only one disk at a time.
- We can remove only the top disk.
- We can only place a disk above a disk of a bigger size.
Python Program for Tower of Hanoi Problem Using Recursion
Here, the function tower_of_hanoi() calls recursively and outputs a sequence of steps to move n disks from the start rod to the end rod.
# Creating a recursive function
def tower_of_hanoi(disks, source, auxiliary, destination):
if(disks == 1):
print('Move disk 1 from rod {} to rod {}.'.format(source, destination))
return
# calling recursion function
tower_of_hanoi(disks - 1, source, destination, auxiliary)
print('Move disk {} from rod {} to rod {}.'.format(disks, source, destination))
tower_of_hanoi(disks - 1, auxiliary, source, destination)
disks = int(input('Enter the number of disks: '))
# Calling the function
# Here, we are passing source as A, auxiliary as B, and destination as C
tower_of_hanoi(disks, 'A', 'B', 'C')
Output of the above code
Enter the number of disks:
3
Move disk 1 from rod A to rod C.
Move disk 2 from rod A to rod B.
Move disk 1 from rod C to rod B.
Move disk 3 from rod A to rod C.
Move disk 1 from rod B to rod A.
Move disk 2 from rod B to rod C.
Move disk 1 from rod A to rod C.
Enter the number of disks:
4
Move disk 1 from rod A to rod B.
Move disk 2 from rod A to rod C.
Move disk 1 from rod B to rod C.
Move disk 3 from rod A to rod B.
Move disk 1 from rod C to rod A.
Move disk 2 from rod C to rod B.
Move disk 1 from rod A to rod B.
Move disk 4 from rod A to rod C.
Move disk 1 from rod B to rod C.
Move disk 2 from rod B to rod A.
Move disk 1 from rod C to rod A.
Move disk 3 from rod B to rod C.
Move disk 1 from rod A to rod B.
Move disk 2 from rod A to rod C.
Move disk 1 from rod B to rod C.
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary