Solving the Towers of Hanoi Problem

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

The Towers of Hanoi is a classic problem in computer science and mathematics. It consists of three rods and a number of disks of different sizes, which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/recursion_dynamic("`Recursion Dynamic`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/AdvancedTopicsGroup -.-> python/iterators("`Iterators`") subgraph Lab Skills algorithm/recursion_dynamic -.-> lab-268859{{"`Solving the Towers of Hanoi Problem`"}} python/conditional_statements -.-> lab-268859{{"`Solving the Towers of Hanoi Problem`"}} python/tuples -.-> lab-268859{{"`Solving the Towers of Hanoi Problem`"}} python/function_definition -.-> lab-268859{{"`Solving the Towers of Hanoi Problem`"}} python/default_arguments -.-> lab-268859{{"`Solving the Towers of Hanoi Problem`"}} python/classes_objects -.-> lab-268859{{"`Solving the Towers of Hanoi Problem`"}} python/constructor -.-> lab-268859{{"`Solving the Towers of Hanoi Problem`"}} python/polymorphism -.-> lab-268859{{"`Solving the Towers of Hanoi Problem`"}} python/encapsulation -.-> lab-268859{{"`Solving the Towers of Hanoi Problem`"}} python/raising_exceptions -.-> lab-268859{{"`Solving the Towers of Hanoi Problem`"}} python/iterators -.-> lab-268859{{"`Solving the Towers of Hanoi Problem`"}} end

Hanoi

Problem

Your task is to implement the Towers of Hanoi with 3 towers and N disks. The goal is to move all the disks from the first tower to the third tower, obeying the following simple rules:

  1. Only one disk can be moved at a time.
  2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod.
  3. No disk may be placed on top of a smaller disk.

Requirements

To solve this problem, you need to meet the following requirements:

  • You should have a stack class that can be used for this problem.
  • You should validate the inputs before processing them.
  • You should ensure that the program fits memory.

Example Usage

Here are some examples of how the program should behave:

  • If there are no towers, an exception should be raised.
  • If there are 0 disks, the program should return None.
  • If there is only 1 disk, the program should move it from the first tower to the third tower.
  • If there are 2 or more disks, the program should move them from the first tower to the third tower, obeying the rules mentioned above.

Summary

In summary, the Towers of Hanoi is a classic problem that requires moving disks from one tower to another while obeying certain rules. To solve this problem, you need to have a stack class, validate the inputs, and ensure that the program fits memory.

Other Algorithm Tutorials you may like