Even though there's plenty of questions about this problem here, none of them have helped me clear this up. I understand what recursion is and I can easily solve Towers of Hanoi by myself in 2^n-1 moves, but I'm having trouble writing an algorithm for it in Python. The base case works but I can't seem to find a way to translate "move n-1 disks to the auxiliary peg and then the largest disk to the target peg" into array operations, and I don't understand why the last element isn't getting removed from the array when I pop it in the recursive call.
This is the programme:
peg_a = [1,0]
peg_b = []
peg_c = []
def hanoi(start,aux,target):
print(start,aux,target)
if len(start) == 1:
target.append(start.pop())
print(start,aux,target)
else:
hanoi(start[1:],target,aux)
target.append(start.pop())
print(start,aux,target)
hanoi(peg_a, peg_b, peg_c)
And this is what gets printed:
[1, 0] [] []
[0] [] []
[] [] [0]
[1] [0] [0]
Any help?