Hi I am trying to create an iterative solution for Tower of Hanoi. However I cannot figure out how to incorporate the movement of the disk. For example in following function call for 2 disks, I want to return output as :
Move disk 1 from A to B, Move disk 2 from A to C, Move disk 1 from B to C
Here is what I have so far
import time
#Iterative solution Tower of Hanoi
start = time.time()
def iterative_hanoi(n):
legalmoves = []
number_of_moves = 2**n-1
source_peg = 'A'
auxiliary_peg = 'B'
destination_peg = 'C'
i = 0
if n % 2==0:
destination_peg = 'B'
auxiliary_peg = 'C'
while i in range(0,number_of_moves):
i +=1
if i%3 == 1:
legalmoves.append(source_peg + destination_peg)
if i%3 == 2:
legalmoves.append(source_peg + auxiliary_peg)
if i%3 == 0:
legalmoves.append(auxiliary_peg + destination_peg)
return legalmoves
print(iterative_hanoi(2))
end = time.time()
print(end - start)