A python algorithm written for Tower Of Hanoi.
class Towers:
hasItMoved = []
toDest = []
def __init__(self):
self.current = 1
self.n = int(input("Enter the number of disks: "))
self.hasItMoved = [None] * 12
self.toDest = [1] * 12
for i in range(self.n):
self.toDest[i] = 0
self.r = self.n
self.hanoiStart(self.n, "Start", "Aux1", "Aux3", "Aux2", "Dest", self.current)
# function to start
def hanoiStart(self, numOfDisks, start, source, dest, aux, last, current):
self.move(1, start, source, self.current)
self.current += 1
self.H1(self.n, "Start", "Aux1", "Aux3", "Aux2", "Dest", self.current)
self.move(1, dest, last, self.current)
self.current += 1
# helper function
def H1(self, numOfDisks, start, source, dest, aux, last, current):
if numOfDisks == 1:
self.move(numOfDisks, source, aux, self.current)
self.current += 1
self.move(numOfDisks, aux, dest, self.current)
self.current += 1
elif numOfDisks == 2:
self.move(numOfDisks-1, source, aux, self.current)
self.current += 1
self.move(numOfDisks-1, aux, dest, self.current)
self.current += 1
if self.current == 4:
self.move(numOfDisks, start, source, self.current)
self.current += 1
self.move(numOfDisks, source, aux, self.current)
self.current += 1
self.move(numOfDisks-1, dest, aux, self.current)
self.current += 1
self.move(numOfDisks-1, aux, source, self.current)
self.current += 1
self.move(numOfDisks, aux, dest, self.current)
self.current += 1
if self.r == 2:
self.move(2, dest, last, self.current)
self.current += 1
self.move(numOfDisks-1, source, aux, self.current)
self.current += 1
self.move(numOfDisks-1, aux, dest, self.current)
self.current += 1
elif numOfDisks > 2:
self.current = self.H1(numOfDisks-1, start, source, dest, aux, last, self.current)
if self.hasItMoved[numOfDisks] != 1:
self.move(numOfDisks, start, source, self.current)
self.current += 1
self.hasItMoved[numOfDisks] = 1
self.move(numOfDisks, source, aux, self.current)
self.current += 1
self.current = self.H1(numOfDisks-1, start, dest, source, aux, last, self.current)
self.move(numOfDisks, aux, dest, self.current)
self.current += 1
if self.toDest[numOfDisks+1] != 0:
self.move(numOfDisks, dest, last, self.current)
self.current += 1
self.toDest[numOfDisks] = 1
if numOfDisks == self.r:
self.r -= 1
self.current = self.H1(numOfDisks-1, start, source,
dest, aux, last, self.current)
return self.current
# function to print the move performed
def move(self, aDisk, source, dest, currentStep):
print("Move ", self.current, ": Move disk ", aDisk, " from ", source, " to ", dest)
Main File
import hanoi
if __name__=="__main__":
test = hanoi.Towers()
Please rewrite this so that it accounts for an additional auxilary.
Like Start, Aux1,Aux 2, Aux 3, Aux 4,
Destination.
And if you can, please provide Time and space comlexity.
Sol:-
I have written code for you, which is very understandable
Code:-
#include <stdio.h>
void towerOfHanoi(int n, char from_rod, char to_rod,
char aux_rod1, char aux_rod2)
{
if (n == 0)
return;
if (n == 1) {
printf("\n Move disk %d
from rod %c to rod %c",
n, from_rod, to_rod);
return;
}
towerOfHanoi(n - 2, from_rod, aux_rod1,
aux_rod2,
to_rod);
printf("\n Move disk %d from rod %c to rod %c
",
n - 1, from_rod, aux_rod2);
printf("\n Move disk %d from rod %c to rod %c
",
n, from_rod, to_rod);
printf("\n Move disk %d from rod %c to rod %c
",
n - 1, aux_rod2, to_rod);
towerOfHanoi(n - 2, aux_rod1, to_rod,
from_rod,
aux_rod2);
}
int main()
{
int n = 4; // Number of disks
// A, B, C and D are names of rods
towerOfHanoi(n, 'A', 'D', 'B', 'C');
return 0;
}
Output:-

We know that for any recursive function, the space complexity is O(n) so space complexity of tower of hanoi is O(n).
Time complexity for tower of hanoi problem is O(2n+1-1)or simply O(n) where n is no.of rods or disks.
Move disk 1 from rod A to rod D Move disk 2 from rod A to rod B Move disk 1 from rod D to rod B Move disk 3 from rod A to rod C Move disk 4 from rod A to rod D Move disk 3 from rod C to rod D Move disk 1 from rod B to rod C Move disk 2 from rod B to rod D Move disk 1 from rod C to rod D Process exited after 0.03206 seconds with return value o Press any key to continue ...
A python algorithm written for Tower Of Hanoi. class Towers: hasItMoved = [] toDest = []...
If possible, this tower of hanoi code is written in java. Could anyone make an attempt to write it in python? package Hanoi; import java.util.Scanner; private int n,current,r; public class Towers { private Scanner in; private int[] hasItMoved,toDest; Towers(){ this.in=new Scanner(System.in); this.current=1; System.out.println("Enter the number of disks: "); this.n = in.nextInt(); this.hasItMoved=new int[12]; this.toDest=new int[12]; for(int j=0;j<this.toDest.length;j++) this.toDest[j]=1; for(int i=1;i<=n;i++) this.toDest[i]=0; r=n; ...
Solve the Towers of Hanoi game for the following graph G=(V,E) with V={Start, Aux 1, Aux2, Aux3, Dest} and E = {(Start,Auxl), (Auxl,Aux2), (Aux2,Aux3), (Aux3,Aux1), (Aux3,Dest)}. Design an algorithm and determine the time and space complexities of moving n disks from Start to Dest. Implement this algorithm whereby your program prints out each of the moves of every disk. Show the output for n=1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. (If the output is too long,...
Recursion Write a program to solve the Towers of Hanoi problem for a tower of size n, using both recursion and iteration. Time each method separately. Be very carefull to time only the actual work and avoid superfluous module calls and initialization, etc. Compare and contrast your two versions of the problem. Are they what you expected? Your analysis must contain a table of the times obtained for each run. For a tower of a particular size, your output should...
PYTHON: Conan is writing a module to contain different implementations of trees. After his first tree, the BinaryTreeclass, he wrote test code and is having problems understanding the error. Locate his problem and explain how you would fix it. class Node(object): def __init__(self, data=None): self.data = data def __str__(self): return "NODE: " + str(self.data) class Tree(object): def __init__(self): self.root_node = None self.size = 0 def __len__(self): return self.size def add(self, data): raise NotImplementedError("Add method not implemented.") def inorder_traversal(self): raise NotImplementedError("inorder_traversal...
Towers of Hanoi (15 points) Given: n disks, all of different sizes the size of the ith disk is į .3 pegs - A, B, C the number of pegs might change in a future version of the game Inally, all the disks are stacked on peg A Requirement: Never place a larger disk on top of a smaller disk (disk 5 is larger than disk 4, etc.) Objective: Move the disks from peg A to peg C Input: n,...
You will implement the simple Tower of Hanoi problem using Python. You can find the implementation in just about any book that talks about recursion. Here is the problem description: o There are n disks labled 1,2,3, ..., n and the three towers A, B and C. o No disk can be on top of a smaller disk at anytime. o All the disks are initially placed on tower A. o Only one disk can be moved at a time,...
Write the recursive MIPS code (with abundant explanatory comments) for the Tower of Hanoi problem of transferring a stack of N disks (smaller sized disks stacked over the larger sized ones) from a source peg to a destination peg via a third (temporary rest peg) under the constraints: 1. Only one disk is moved at a time from one peg to another 2. At no time, a larger disk will sit on a smaller one.
Write a Python program that implements the Towers of Hanoi, using the recursive algorithm discussed in class. Use command-line arguments to pass parameters to the program - spring% python towersOfHanoi.py USAGE: towersOfHanoi.py <# rings> <FROM peg> <TO peg> spring% python towersOfHanoi.py 4 1 3 Move disk from peg 1 to peg 2 Move disk from peg 1 to peg 3 Move disk from peg 2 to peg 3 Move disk from peg 1 to peg 2 Move disk from peg...
I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i am recieving says that in line 61 list index is out of range. kindly someone help me with it as soon as possible. Below is the program kindly check and correct it. Thanks! class Animal: def __init__(self, name, types, species, mass): self.name=name self.type=types...
I am currently facing a problem with my python program which i have pasted below where i have to design and implement python classes and record zoo database and takes user input as query. the error i am recieving says that in line 61 list index is out of range. kindly someone help me with it as soon as possible. Below is the program kindly check and correct it. Thanks! class Animal: def __init__(self, name, types, species, mass): self.name=name self.type=types...