Question

In: Computer Science

There is a famous puzzle called the Tower of Hanoi. Code the recursive implementation of it...

There is a famous puzzle called the Tower of Hanoi. Code the recursive implementation of it with Python.

Solutions

Expert Solution

# Recursive function definition below
# input params: number of disks used, what is the source (start_point),what is the target where the disk is intended t be (end_point) and a intermittent to peroform movement(auxilary)

def TowerOfHanoi(num_disks , start_point, end_point, auxilary):
   if num_disks == 1:
       print "Move disk 1 from position ",start_point," to position ",end_point
       return
       # else Move num_disks-1 disks from source to auxiliary using the target (end_point) as the auxilary
   TowerOfHanoi(num_disks-1, start_point, auxilary, end_point)
   print "Move disk ",num_disks," from position ",start_point," to position ",end_point
   #Move the num_disks-1 disks on the auxiliary to the end_point using the start_point as the auxiliary.
   TowerOfHanoi(num_disks-1, auxilary, end_point, start_point)
      
# calling function passing inputs
num_disks = int(input('Enter number of disks: '))
# call the function with num_disks,A as source, B as auxiliary and C as target
TowerOfHanoi(num_disks, 'A','C','B')

Output:


Related Solutions

There is a famous puzzle called the Towers of Hanoi that consists of three pegs and...
There is a famous puzzle called the Towers of Hanoi that consists of three pegs and n circular disks, all of different sizes. The disks start on the leftmost peg, with the largest disk on the bottom, the second largest on top of it, and so on, up to the smallest disk on top. The goal is to move the disks so that they are stacked in this same order on the rightmost peg. However, you are allowed to move...
10. The Tower of Hanoi is a puzzle consisting of a board with three dowels and...
10. The Tower of Hanoi is a puzzle consisting of a board with three dowels and a collection of n disks of n different radii. The disks have holes drilled through their centers so they can fit on the dowels on the board. Initially, all the disks are on the first dowel arranged in order of their sizes, with the largest one being at the bottom, and the smallest one on the top. The object is to move all the...
prove that the tower of Hanoi puzzle With n rings cannot be solved in fewer than...
prove that the tower of Hanoi puzzle With n rings cannot be solved in fewer than (2^n)-1 moves
I have a recursive Tower of Hanoi program and would like to get the index numbers...
I have a recursive Tower of Hanoi program and would like to get the index numbers to be in correct sequence: 1,2,3,4,5,6,7. How can I do this? Main.java import java.io.*; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args){ /**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole C without ever putting a...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars
My recursive Tower of Hanoi program does not render the correct index numbers. Also, it does...
My recursive Tower of Hanoi program does not render the correct index numbers. Also, it does not prompt user to enter the starting rod letter and the ending rod letter. Could you help me fix it? Main.java import java.io.*; import java.util.List; import java.util.Scanner; public class Main { int index = 1; public static void main(String[] args) { /**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use...
Write two Java programs ( Iterative and Recursive programs ) that solves Tower of Hanoi problem?use 4 disks and 3 bars. Students must send the code and outputs
Tower of Hanoi - Java Code Use three rods labeled A, B, and C Use three...
Tower of Hanoi - Java Code Use three rods labeled A, B, and C Use three discs labels 1 (smallest), 2 (medium size), and 3 (largest disc) The program prompts you to enter the starting rod (A, B, or C) The program prompts you to enter the ending rod (A, B, or C, but cannot be same rod as entered as starting rod) The starting rod has discs 1, 2, 3, where 1 is on top of 2 is on...
Tower of Hanoi problem complete the problems please use this code #pragma once #include <iostream> #include...
Tower of Hanoi problem complete the problems please use this code #pragma once #include <iostream> #include <stack> #include <string> #include <vector> using namespace std; class TowersOfHanoi { friend ostream& operator<<(ostream& sink, const TowersOfHanoi& towers); public: //constructor TowersOfHanoi(); //custom methods unsigned move(unsigned new_n_disks = 6, ostream& new_sink = cout); private: //custom methods void move_aux(ostream& sink, unsigned n_disks, unsigned srce, unsigned dest, unsigned aux); //temporary variable unsigned _n_moves; //data const unsigned _n_towers; stack<unsigned>* _tower; }; #include "TowersOfHanoi.h" // ostream& operator<<(ostream& sink, const...
Tower of Hanoi problem please use this code #pragma once #include #include #include #include using namespace...
Tower of Hanoi problem please use this code #pragma once #include #include #include #include using namespace std; class TowersOfHanoi { friend ostream& operator<<(ostream& sink, const TowersOfHanoi& towers); public: //constructor TowersOfHanoi(); //custom methods unsigned move(unsigned new_n_disks = 6, ostream& new_sink = cout); private: //custom methods void move_aux(ostream& sink, unsigned n_disks, unsigned srce, unsigned dest, unsigned aux); //temporary variable unsigned _n_moves; //data const unsigned _n_towers; stack* _tower; }; #include "TowersOfHanoi.h" // ostream& operator<<(ostream& sink, const TowersOfHanoi& hanoi) { for (unsigned index =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT