Skip to content

Instantly share code, notes, and snippets.

@Per48edjes
Created August 27, 2017 20:03
Show Gist options
  • Save Per48edjes/8c53b909a64e69275dfec144f4fd28ce to your computer and use it in GitHub Desktop.
Save Per48edjes/8c53b909a64e69275dfec144f4fd28ce to your computer and use it in GitHub Desktop.
Recursive solution to the Tower of Hanoi problem with N discs
def printMove(fr, to):
print('move from ' + str(fr) + ' to ' + str(to))
def Towers(n, fr, to, spare):
if n == 1:
printMove(fr, to)
else:
Towers(n-1, fr, spare, to)
Towers(1, fr, to, spare)
Towers(n-1, spare, to, fr)
if __name__ == "__main__":
Towers(3,"A","B","C")
@Per48edjes
Copy link
Author

Good walkthrough by Reducible here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment