Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
705 B
5
Indexable
Never
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)