Created
August 27, 2017 20:03
-
-
Save Per48edjes/8c53b909a64e69275dfec144f4fd28ce to your computer and use it in GitHub Desktop.
Recursive solution to the Tower of Hanoi problem with N discs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good walkthrough by Reducible here.