Question: A python algorithm written for Tower Of Hanoi. class Towers: hasItMoved = [] toDest = [] def __init__(self): self.current = 1 self.n = int(input(Enter the
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.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
