Simple recursive towers of hanoi implementation

This commit is contained in:
2022-12-16 13:20:13 +01:00
parent fc04eec19c
commit 82587f5ce6
Executable
+13
View File
@@ -0,0 +1,13 @@
#!/usr/bin/env python3
def solve_hanoi(disks, from_pin, to_pin, aux_pin):
if disks == 1:
print("Move disk from pin", from_pin, "to", to_pin)
return
solve_hanoi(disks - 1, from_pin, aux_pin, to_pin) # Move all disks except the last to the auxiliary pin
print("Move disk from pin", from_pin, "to", to_pin) # Move the last disk to the target pin
solve_hanoi(disks - 1, aux_pin, to_pin, from_pin) # Move all disks from the auxiliary pin to the target pin
solve_hanoi(10, 1, 3, 2)