
Silly, right? This is actually an example of a powerful divide-and-conquer technique in computer programming known as "recursion." Recursion treats a computation task as a set of smaller, similar computations, with the next smaller computation being calculated, and then the next, and the next, until you get to a "base case" with a known answer. See, you solve it for general n, hence, the smaller subproblems solves it by the same formula and we ensure the base case when there are no disks to move.Q: What's the easiest way to get the sum of all whole numbers between 1 and 100?Ī: First get the sum of all whole numbers between 1 and 99, then add 100. Finally, move the subproblem on top of biggest disk. This was done with the three things we did.
#Chegg code hanoi towers how to
You tell the machine how to solve the problem using a smaller instance of the problem.
#Chegg code hanoi towers code
The code includes a simple print function to see the trace of the movings. Solve_tower_of_hanoi(t, len(t.towers), 0, 2, 1) Solve_tower_of_hanoi(towers, n - 1, aux_tower, dest_tower, start_tower) # Move subproblem of n - 1 disk from aux_tower to dest_tower. Solve_tower_of_hanoi(towers, n - 1, start_tower, aux_tower, dest_tower) # Move subproblem of n - 1 disks from start_tower to aux_tower. Self.towers = ĭef solve_tower_of_hanoi(towers, n, start_tower, dest_tower, aux_tower):

This means that the problem starts with n disks. Move the smaller problem of 2 disks from second tower (rod) to last tower (rod).įirst understand that there can be any number of disks in an instance of Tower of Hanoi.Move the big disk from first tower (rod) to last tower (rod).Move the smaller problem of 2 disks from first tower (rod) to second tower (rod).Then we can move disk 3 to the final destination.Īnd after that, we should move the smaller problem of the 2 disks on top fo disk 3. What do we need to make happen if we should move disk 3 from first tower (rod) to the last tower (rod)?Įxactly. How can we break that down to a smaller problem? Step 3: Implement Tower of Hanoi with a recursive function It will make your code easy and straight forward. The example of Tower of Hanoi will show you the benefit. The above might not be a good example of how recursion helps you. Well, what did we gain from making the function recursive? Let’s first try to do in the iterative way. It can be a bit difficult to connect the definition of recursions to getting the sum of the integers 1 + 2+ 3 + … + (n – 1) + n. While that is a beautiful and perfect definition – there is still need to exemplify what that means.Ī simple example is to sum up the numbers from 1 to n. Recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem. Step 2: Recall recursion and unleash the power of it Now do yourself a favor and try to think how you would solve that. The third rule says, that we cannot move disk 2 on top of disk 1. Say, in the above we have moved the disk 1 from the first to the second tower (rod).Īfter that move, we can move disk 2 or disk 1. The first two rules combined means that you can only take one top disk and move it. You cannot place a bigger disk on top of a smaller disk.You can only take the top disk and place on top of another tower (rod).You can only move one disk at the time.The goal is to move all the disks from on tower (rod) to another one with the following 3 rules. The disk all have different sizes as pictured above. A basic setup of Tower of Hanoi with 3 disks and 3 towers (often called rods) Before we set the rules, let’s see how our universe looks like. Tower of Hanoi is a mathematical game, which has three rules. Step 1: Understand the Tower of Hanoi challenge Master Sort & Search Algorithms – Learn it Easy with Python.Python for Data Science: Master NumPy & Pandas on Real Data.Master Data Structures for Optimal Solutions in Python.Start OpenCV with Python: Real-time Processing with Webcam.

Master Modern Security and Cryptography by Coding in Python.


