Skip to content

Instantly share code, notes, and snippets.

@OzTamir
Created February 16, 2014 18:09
Show Gist options
  • Save OzTamir/9038288 to your computer and use it in GitHub Desktop.
Save OzTamir/9038288 to your computer and use it in GitHub Desktop.
Towers of Hanoi in Python (using recursion)
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