{"id":18707,"date":"2021-06-26T20:00:37","date_gmt":"2021-06-26T20:00:37","guid":{"rendered":"https:\/\/www.askpython.com\/?p=18707"},"modified":"2021-06-26T20:06:28","modified_gmt":"2021-06-26T20:06:28","slug":"tower-of-hanoi-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/examples\/tower-of-hanoi-in-python","title":{"rendered":"Tower of Hanoi in Python: Complete Step-by-Step"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Tower of Hanoi is a mathematical problem (puzzle) that consists of 3 poles and &#8216;n&#8217; number of discs, each disc having different diameters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Objective of the Tower of Hanoi Problem<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The objective or goal of this problem is to transfer all the &#8216;n&#8217; discs from source pole to the destination pole in such a way that we get the same arrangement of discs as before. But this goal must be achieved by sticking to the rules.  <\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Rules and Constraints<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The constraints that must be satisfied while solving the problem are &#8211;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Only one disc can be moved at a time.<\/li><li>Only the top-most disc can be removed<\/li><li>The larger disc cannot be placed on top of the smaller disc.<\/li><\/ol>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Visual Representation<\/strong> of the Tower of Hanoi problem<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The following picture shows the step-wise solution for a tower of Hanoi with 3 poles (source, intermediate, destination) and 3 discs. The goal is to move all the 3 discs from pole A to pole C.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"625\" height=\"228\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/STEP-1.png\" alt=\"STEP 1\" class=\"wp-image-18718\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/STEP-1.png 625w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/STEP-1-300x109.png 300w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"627\" height=\"227\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/STEP-3_4.png\" alt=\"STEP 3 4\" class=\"wp-image-18719\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/STEP-3_4.png 627w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/STEP-3_4-300x109.png 300w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"619\" height=\"222\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/STEP-5_6.png\" alt=\"STEP 5 6\" class=\"wp-image-18721\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/STEP-5_6.png 619w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/STEP-5_6-300x108.png 300w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"631\" height=\"238\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/STEP-7_8.png\" alt=\"STEP 7 8\" class=\"wp-image-18722\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/STEP-7_8.png 631w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/STEP-7_8-300x113.png 300w\" sizes=\"auto, (max-width: 631px) 100vw, 631px\" \/><\/figure><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">As we can see from the above solution, the number of moves needed for 3 discs = 8. So, a generalized formula for a total number of moves we need is:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Total number of moves = n<sup>2<\/sup>\u00a0&#8211; 1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Where &#8216;n&#8217; is the total no. of discs.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Solving the Tower of Hanoi Problem in Python<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef TowerOfHanoi(n , s_pole, d_pole, i_pole):           \n    if n == 1:\n        print(&quot;Move disc 1 from pole&quot;,s_pole,&quot;to pole&quot;,d_pole)\n        return\n    TowerOfHanoi(n-1, s_pole, i_pole, d_pole)\n    print(&quot;Move disc&quot;,n,&quot;from pole&quot;,s_pole,&quot;to pole&quot;,d_pole)\n    TowerOfHanoi(n-1, i_pole, d_pole, s_pole)\n\nn = 3\nTowerOfHanoi(n, &#039;A&#039;, &#039;C&#039;, &#039;B&#039;)\n# A, C, B are the name of poles\n \n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">In the above code, we call our function TowerOfHanoi recursively for 3 discs. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>s_pole: source pole<\/li><li>i_pole: intermediate pole<\/li><li>d_pole: destination pole<\/li><\/ul>\n\n\n\n<p class=\"has-normal-font-size wp-block-paragraph\"><strong>The output of the above code is:<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"528\" height=\"142\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/output-1.png\" alt=\"Output 1\" class=\"wp-image-18725\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/output-1.png 528w, https:\/\/www.askpython.com\/wp-content\/uploads\/2021\/06\/output-1-300x81.png 300w\" sizes=\"auto, (max-width: 528px) 100vw, 528px\" \/><\/figure><\/div>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">So , this is how we solve the problem of Tower of Hanoi.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This code can be generalized for any number of discs. So if you want the solution for 4 discs, just change the value of n from 3 to 4 as n = 4, and the output will be displayed for 4 discs and so on.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tower of Hanoi is a mathematical problem (puzzle) that consists of 3 poles and &#8216;n&#8217; number of discs, each disc having different diameters. The Objective of the Tower of Hanoi Problem The objective or goal of this problem is to transfer all the &#8216;n&#8217; discs from source pole to the destination pole in such a [&hellip;]<\/p>\n","protected":false},"author":34,"featured_media":18726,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-18707","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-examples"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/18707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=18707"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/18707\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/18726"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=18707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=18707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=18707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}