Binary
&
Towers of Hanoi

TOH

A = [5,4,3,2,1]
B = []
C = []

def move(n, source, target, auxiliary):
    if n > 0:
        # move n-1 disks from source to auxiliary, so they are out of the way
        move(n-1, source, auxiliary, target)

        # move the nth disk from source to target
        target.append(source.pop())

        # Display our progress
        print(A, B, C, '##############', sep='\n')

        # move the n-1 disks that we left on auxiliary onto target
        move(n-1, auxiliary, target, source)

# initiate call from source A to target C with auxiliary B
move(5, A, C, B)

Solution by iteration

0000

0001

0010

0011

0100

0101

0110

0111

1000

1001

1010

1011

1100

1101

1110

1111

Made with Slides.com