Skip to content

Instantly share code, notes, and snippets.

@a-eid
Created June 29, 2017 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-eid/60f9f38d6d33fb80cae650c89cd77852 to your computer and use it in GitHub Desktop.
Save a-eid/60f9f38d6d33fb80cae650c89cd77852 to your computer and use it in GitHub Desktop.
""" Tower of Hanoi """
def printMove(fr , to):
print("move from {} to {} ".format(str(fr),str(to)) )
def towers(num , original , dist ,spare):
if num == 1:
printMove(original , dist)
else:
towers(num-1, original , spare , dist) # move the upper stack to spare to
towers(1, original , dist , spare) # move the bigger to to
towers(num-1 , spare , dist , original) # move the upper back to to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment