Untitled
unknown
plain_text
2 years ago
705 B
14
Indexable
def tower_of_hanoi(n, source, auxiliary, target, disks):
if n == 1:
disks[target].append(disks[source].pop())
print(f"Move disk 1 from {source} to {target}. A={tuple(disks['A'])}, B={tuple(disks['B'])}, C={tuple(disks['C'])}.")
return
tower_of_hanoi(n-1, source, target, auxiliary, disks)
disks[target].append(disks[source].pop())
print(f"Move disk {n} from {source} to {target}. A={tuple(disks['A'])}, B={tuple(disks['B'])}, C={tuple(disks['C'])}.")
tower_of_hanoi(n-1, auxiliary, source, target, disks)
# Disks configuration
disks = {
'A': [3, 2, 1],
'B': [],
'C': []
}
# To solve the problem for 3 disks:
tower_of_hanoi(3, 'A', 'B', 'C', disks)Editor is loading...