Tell us what’s happening:
I have coded all the possible moves to be called in a specific sequence, and am completely stuck here. How do I take it from here?
Your code so far
def hanoi_solver(n):
one = [i for i in range(n, 0, -1)]
two = []
three = []
moves = f'{one} {two} {three}\n'
i = 50
while three != list(one) and i > 0:
if (one and not three) or (one and three[-1] > one[-1]):
three.append(one.pop())
elif one and not two or (one and two[-1] > one[-1]):
two.append(one.pop())
elif (three and not two) or (three and two[-1] > three[-1]):
two.append(three.pop())
elif (two and not one) or (two and one[-1] > two[-1]):
one.append(two.pop())
elif (two and not three) or (two and three[-1] > two[-1]):
three.append(two.pop())
elif (three and not one) or (three and one[-1] > three[-1]):
one.append(three.pop())
else:
return 'Incorrect solution'
moves += f'{one} {two} {three}\n'
i -= 1
return moves
print(hanoi_solver(3))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Challenge Information:
Implement the Tower of Hanoi Algorithm - Implement the Tower of Hanoi Algorithm