How to Solve The Towers Of Hanoi in Python | Recursive programming



The tower of Hanoi is most popular and very interesting puzzle.Let me explain the puzzle first, there are three towers given let say X , Y , and Z. and No. of Discs are placed on that tower in descending order(Large Disc to Small) let say 3 Disc.Now in idle condition all disc are placed in Tower A with Descending order.Now your task is to place that Discs in to last tower Z as Descending order.

But The rules ,Which we have to take into account before moving a Discs Are:
  • (1) Only one disc may be moved at a time.
  • (2) Only top disc on any Peg may be moved to any other Peg.
  • (3) A larger disc can not be p;aced on a smaller one.

Python Program to Solve The Tower of Hanoi :


This is the Example of Recursive programming in python with Function calling.
n = int(raw_input("Enter no of Disc to solve Tower\n >"))

def hanoi_tower(peg1, peg2, peg3, n):
    if n <= 0:
     print "Dont be over smart"
     return
    if n == 1:
     print "Move disc from %s to %s" % (peg1, peg3)
    else:
     hanoi_tower(peg1, peg3, peg2,n-1)
     hanoi_tower(peg1, peg2, peg3,1)
     hanoi_tower(peg2, peg1, peg3,n-1)

print hanoi_tower('x', 'y', 'z', n)

After Execution OutPut should be :

Enter no of Disc to solve Tower
>4
Move disc from x to y
Move disc from x to z
Move disc from y to z
Move disc from x to y
Move disc from z to x
Move disc from z to y
Move disc from x to y
Move disc from x to z
Move disc from y to z
Move disc from y to x
Move disc from z to x
Move disc from y to z
Move disc from x to y
Move disc from x to z
Move disc from y to z
None

Manual Solution for  Towers Of Hanoi With 4 Discs will Graphically Look like this:

Sourse

Search:
Python program for solve Solve The Towers Of Hanoi
How to solve Solve The Towers Of Hanoi with python programming.


66 comments:
  1. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
    Regards,
    Python Training in Chennai|Python Training|Python Training in Chennai

    ReplyDelete
  2. What a great idea thanks for sharing cool info about website designing visit for more info Buzz Application

    ReplyDelete
  3. Thanks for sharing this unique and informative post. This blog which provided me the required information. Selenium Training Institute in Chennai | Selenium Training Institute in Velachery.

    ReplyDelete
  4. this is really too useful and have more ideas from yours. keep sharing many techniques.
    Web designing course in chennai

    ReplyDelete
  5. Really an amazing post..! By reading your blog post i gained more information.
    Bulk SMS Chennai
    Internet Marketing Company Chennai

    ReplyDelete
  6. What a fantastic blog you should written in well manner.Iam really happy to read this blog.Thanks for sharing.keep sharing more blogs.


    Python Online Training

    ReplyDelete
  7. pdating ourselves to the latest technology is the good idea to live in this modern IT world. Reading articles like your blog this will make an encouraging power within me. Thanks for sharing such an informative content.
    Java Training in Chennai | Data Science Training in Chennai

    ReplyDelete
  8. great blog, you have provided the right information that will be beneficial to somebody at all time. Thanks for sharing such valuable Ideas to our vision. Software Testing Training in Chennai | Selenium Training in Chennai | Java Training in Chennai

    ReplyDelete
  9. It’s too informative blog and I am getting conglomerations of info’s about Python certification. Thanks for sharing,
    Python Training in Marathahalli
    Java Training in Marathahalli

    ReplyDelete
  10. Informative and impressive. Keep Updating
    Jobbörse

    ReplyDelete
  11. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    Data Science Training in Bangalore

    Datascience Training in Chennai

    ReplyDelete
  12. Superb extraordinary blog. I did not think it is possible to do programmatically. Thanks for sharing and keep posting new ideas.
    Final Year Project Center in Chennai | IEEE Project Center in Chennai | Diploma Project Center in Chennai

    ReplyDelete
  13. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly. Java Training Center in Chennai | Best J2EE Training Center in Chennai | No.1 Java Training Institution in Velachery | Core Java Training in Chennai

    ReplyDelete
  14. Thank you for sharing this awesome blog. its very useful and interesting

    mobile website builder

    ReplyDelete
  15. Good collection of information regarding Python thanks for posting please do keep updating...
    Best Online Software Training Institute | Python Training

    ReplyDelete
  16. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    OCJP Exam Center in Chennai | OCJP Exam Center in Velachery

    ReplyDelete
  17. Thank you so much as you have been willing to sharing your information with us. And not only that we will spend a lot of time other blog but your blog is so informative and compare to that your blog is unique.see more: Python Online Training

    ReplyDelete
  18. Hii…It was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly. The Post is a Wonderful Opportunity for Beginners of Python Course. Take time To look at our Website.
    python training in hyderabad

    ReplyDelete
  19. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.

    Best software testing insitute in kanchipuram

    ReplyDelete
  20. Thank you for sharing this awesome blog. its very useful and thanks for sharing.. Best Hardware and Networking courses in kanchipuram|

    ReplyDelete
  21. the blog is very useful, interesting and informative. thank you for sharing the blog with us. keep on updating. Best web designing and development courses in kanchipuram|

    ReplyDelete
  22. the blog is very useful, interesting and informative. thank you for sharing the blog with us. keep on updating. Best Tally ERP 9 Training institute in kanchipuram|

    ReplyDelete
  23. I think this is the best article today. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future reference. Best RHCE Training Institute in kanchipuram|

    ReplyDelete
  24. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future reference. Best tally Erp 9 Training Institute in kanchipuram|

    ReplyDelete
  25. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site. Best Graphic Designing Training Institute in kanchipuram

    ReplyDelete
  26. The best thing is that your blog really informative thanks for your great information.
    Best Graphic Designing Training Institute in kanchipuram|

    ReplyDelete
  27. I think this is the best article today. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future reference.
    Best Web Designing and Development Training Institute in kanchipuram|

    ReplyDelete
  28. Really it was an awesome blog...... Very interesting to read, .It’s very helpful for me, Big thanks for the useful info and keep updating…
    Best Tally Training Academy in Kanchipuram

    ReplyDelete
  29. Really it was an awesome blog...... Very interesting to read, .It’s very helpful for me, Big thanks for the useful info and keep updating…
    Best hardware & network Training Academy in Kanchipuram

    ReplyDelete
  30. Thanks for sharing this niche useful informative post to our knowledge...
    Best java Training Academy in Kanchipuram

    ReplyDelete
  31. Awesome post. Really you are shared very informative concept... Thank you for sharing. Keep on updating...
    Best ccnp Training Academy in Kanchipuram

    ReplyDelete
  32. It’s interesting that many of the bloggers to helped clarify a few things for me as
    Well as giving. Most of ideas can be nice content.
    Best Graphic Designing Training Institute in in Kanchipuram

    ReplyDelete
  33. I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    Best Graphic Designing Training Institute in in Kanchipuram

    ReplyDelete
  34. Your Blog is really awesome with useful and helpful content for us.Thanks for sharing ..keep updating more information.
    Best java Training Institute in in Kanchipuram

    ReplyDelete
  35. Nice post... Really you are done a wonderful job. Thanks for sharing such wonderful information with us. Please keep on updating...
    Best Graphic designing Training Institute in in Kanchipuram

    ReplyDelete
  36. This information is very useful and innovative..I gained more knowledge from this.Thanks for the nice information its very useful to read your blog...
    Best web designing Training Institute in in Kanchipuram

    ReplyDelete
  37. Wonderful article.It is to define the concepts very well.Clearly explain the information.It has more valuable information for encourage me to achieve my career goal.
    Best dally erp 9 Training Institute in in Kanchipuram

    ReplyDelete
  38. Wow, really I am much interested to know our blog content is really good.Great information. Thank you for Sharing.
    Best C++ Training Institute in Kanchipuram

    ReplyDelete
  39. Awesome Post! I like writing style, the way you described the topics throughout the post. I hope many web reader will keep reading your post at the end, Thanks for sharing your view.
    Best web design Training Institute in Kanchipuram

    ReplyDelete
  40. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site
    Best dotnet Training Institute in Kanchipuram

    ReplyDelete
  41. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing
    Best graphic designing Training Institute in Kanchipuram

    ReplyDelete