Created
February 16, 2014 18:09
-
-
Save OzTamir/9038288 to your computer and use it in GitHub Desktop.
Towers of Hanoi in Python (using recursion)
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 Hanoi(ndisks, start_rod=1, end_rod=3): | |
if ndisks: | |
Hanoi(ndisks - 1, start_rod, 6 - start_rod - end_rod) | |
print('Move disk {DISK} from {START} to {END}'.format(DISK = ndisks, START = start_rod, END = end_rod)) | |
Hanoi(ndisks - 1, 6 - start_rod - end_rod, end_rod) | |
if __name__ == '__main__': | |
print('Towers Of Hanoi - Recursion!') | |
disks = input('Enter number of disks >>>') | |
Hanoi(int(disks)) | |
print('Thanks for playing!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment