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.


10 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