8 Feb 2015

Tower of Hanoi

#Python

'''
Recursive program to implement Tower of Hanoi for n rings and 3 stacks
'''

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)




Categories: , ,

0 comments:

Post a Comment

Copyright © UPgradeCODING | Powered by Blogger