Question: A Tower of Hanoi is this game: Current code template: Book Problems: Chapter 1: 2, 6, 11 1. Write a python program to solve the

 A Tower of Hanoi is this game: Current code template: Book

A Tower of Hanoi is this game: Problems: Chapter 1: 2, 6, 11 1. Write a python program to Current code template: solve the Tower of Hanoi problem for n disks (function Hanoi in

Book Problems: Chapter 1: 2, 6, 11 1. Write a python program to solve the Tower of Hanoi problem for n disks (function Hanoi in the associated code file). Include the output of the provided test code ("solved or not solved"). Submit only your typed solution, not the entire file. 2. (i) Use your solution to book problem 2 to solve the problem for 4 disks. (ii) Write a python program to solve the problem for n disks (function Hanoi02 in the associated code file). Include the output of the provided test code ("solved" or "not solved"). Submit only your typed solution, not the entire file. || # ID: quongofa, Eekoh2fo class Tower: # {{{1 def _init_(self, num_disks, num_pegs): self.tower = [ [k for k in range(num_disks)] ] + [ [] for _ in range(num_pegs-1) ] def move (self, pega, pegb): if len(self. tower[peg4]) == 0: raise Exception("Peg " + str(pega) + " is empty! Tower: " + str(self.tower)) disk = self. tower (pega][] if len(self.tower[pegb]) > 0 and disk > self. tower (pegB][0]: raise Exception("Disk " + str(disk) + " is larger than " + str(self. tower [pegB][0])+"! Tower: " + str(self.tower)) del self.tower[pega][0] self tower [pegB] - [disk] + self. tower [pegB] print("Moved disk from peg", pega, "to peg", pegB) --}}}1 def _str_(self): return str(self. tower) #----- class Tower02: # {{{1 def _init__(self, num_disks): self.tower = [ [k for k in range(num_disks)], [], [] ] def move(self, pega, pegB): if pegA == 0 and pegB == 2: raise Exception ("Moves from peg 0 to peg 2 aren't allowed!") if len(self.tower[peg4]) == 0: raise Exception ("Peg " + str(pega) + " is empty! Tower: " + str(self.tower)) disk = self.tower [peg][0] if len(self.tower[pegB]) > O and disk > self tower [pegB][]: raise Exception("Disk " + str(disk) + " is larger than " + str(self.tower[peg][0]) + "! Tower: " + str(self. tower)) del self.tower [peg4][0] self.tower [pegB] = [disk] + self.tower [pegB] print("Moved disk from peg", pega, "to peg", pegB) def __str_(self): return str(self.tower) }}}1 def Hanoi(T, n, src, dest, other): # {{{ # An algorithm to solve the tower of Hanoi for n disks. You should return the # tower T. if n == 0: return T # your solution goes here return T -}}} def Hanoi02(T, n, src, dest, other): # {{{ # An algorithm to solve the tower of Hanoi for n disks, where direct moves # from peg 0 to peg 2 aren't allowed. You should return the tower T. if n == 0: eturn T # your solution goes here return T -}}} # ues this to test your solution print("Hanoi(5):") T = Tower(5, 3) I - Hanoi, 5, 8, 2, 1) if len(T. tower(-1]) == 5: print("solved!") else: print("not solved! T:") print(1) # ues this to test your solution print(" Hanoio2(5):") T = Tower02(5) T = Hanoi02(T, 5, 0, 2, 1) if len(T. tower(-1]) == 5: print("solved!") else: print("not solved! T:") print(T)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!