Saturday, January 4, 2020

Towers of Hanoi in Python

# https://www.youtube.com/watch?v=8lhxIOAfDss
# (Professor Thorsten Altenkirch)

def hanoi(n, a, b, c):
  if n > 0:
    hanoi(n-1, a, c, b)
    print("Move disc from {} to {}".format(a, c))
    hanoi(n-1, b, a, c)

hanoi(4, 'A', 'B', 'C')

No comments:

Post a Comment