Problem Of Tower Of Hanoi In Python
Python Code For Tower Of Hanoi
Hello, Python Enthusiasts!
In the last section of this Python Tutorial Series, we talked about the Palindrome problem in Python. In this article, we will go a step ahead. We are going to deal with the problem of the tower of Hanoi. It’s a very famous problem. In this problem, we are going to use a recursive approach. If you are an absolute beginner then it’s okay. We are going to understand this problem from a very beginner level. If you are following this Python Tutorial Series then it will be easy for you to understand.
Attention all developers seeking to make social connections and establish themselves while earning passive income — look no further! I highly recommend ‘From Code to Connections’, a book that will guide you through the process. Don’t miss out, grab your copy now on Amazon worldwide or Amazon India! You can also go for Gumroad
Tower Of Hanoi
Let’s first understand, what is the Tower Of Hanoi Problem? There are three poles numbered 1, 2, and 3. In the first pole, n disks have been inserted. These disks are placed one above the other in descending order of diameter. The problem is to transfer all the disks from pole 1 to pole 3 using pole 2 as a spare pole, moving one disk at a time, and always keeping in mind that a disk with a larger diameter is not to be placed over a disk with a smaller diameter. For case n=3:
Now, if you take n=1. Then it’s very simple. You just have to move the disk from pole 1 to pole 3
If you take n = 2. Then, you have to move the smaller disk to pole 2. After that, move the bigger disk to pole 3. Finally, move the smaller disk from the spare pole 2 to pole 3.
For n = 3:
Move a disk from pole 1 to pole 3
Move a disk from pole 1 to pole 2
Move a disk from pole 3 to pole 2
Now, the largest disk can be moved from pole 1 to pole 3.
Move a disk from pole 2 to pole 1
Move a disk from pole 2 to pole 3
Move a disk from pole 1 to pole 3
Thus the task is now complete for n =3.
In general, if there are n disks to be transferred from pole 1 to pole 3, using pole 2 as the spare pole, the task can be accomplished as follows:
Transfer (n-1) disks from pole 1 to pole 2 using pole 3 as the spare pole.
Transfer one disk from pole 1 to pole 3
Transfer (n-1) disks from pole 2 to pole 3 using pole 1 as the spare pole
Now, we are good to write the code:
def main():
n = int(input("Enter the number of discs: "))
source = int(input('Enter source pole: '))
spare = int(input('Enter the spare pole: '))
target = int(input('Enter target pole: '))
hanoi(n, source, spare, target)
def hanoi(n, source, spare, target):
if n == 1:
print('Move a disk from ',source, ' to ',target)
elif n == 0:
return
else:
hanoi(n-1, source, target, spare)
print('Move a disk from ',source, ' to ',target)
hanoi(n-1, spare, source, target)
main()
The output of the code is:
Enter the number of discs: 1
Enter source pole: 1
Enter the spare pole: 2
Enter target pole: 3
Move a disk from 1 to 3Enter the number of discs: 3
Enter source pole: 1
Enter the spare pole: 2
Enter target pole: 3
Move a disk from 1 to 3
Move a disk from 1 to 2
Move a disk from 3 to 2
Move a disk from 1 to 3
Move a disk from 2 to 1
Move a disk from 2 to 3
Move a disk from 1 to 3
Our code gives the correct output. This is the solution of the tower of Hanoi using Python. You can try it with random values of n, source, spare, and target. This problem is very common in interviews. Now, you have a proper idea of how to deal with such a problem.
That’s it for this article. If I missed something then let me know in the comment section.
If this article sounds informative to you then make sure to follow and clap. Share it with your geek community.
Thanks for reading.
Find the Length of a String Without Using len Function in Python
Recursive Approach To Solve A Problem In Python
An Introduction to Dictionary in Python
An Introduction To Tuples In Python For Beginners
Built-in Functions on Python Strings
Break, Continue, and Pass Statements with Examples
Python while Loop with Examples
Python If-Elif-Else Conditional Statements with Examples
How to Use If and If-Else Statements in Python
Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build React Native projects and I am currently working on Python Django. Feel free to contact me at (freelance.rohit7@gmail.com).
Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.
If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇