Monday, April 15, 2013

Tower of hanoi solution python


resl=0
count =0

def main():
    num_discs=3
    from_peg=1
    to_peg=3
    temp_peg=2
    resl=move_discs(num_discs, from_peg, to_peg, temp_peg)
    print ('All the pegs are moved!')
    print ('done in: ', resl ,' Moves !')

def move_discs(num, from_peg, to_peg, temp_peg):
    global count
    count +=1
    print ('inside 1: ', count)
    if num >0:
        print ('inside 2: ', count)
        move_discs(num -1, from_peg, to_peg, temp_peg)
        print('Move a disc from peg', from_peg, 'to_peg', temp_peg)
        print ('inside 3: ', count)
        move_discs(num -1, temp_peg,to_peg,from_peg)
        print ('inside 4: ', count)
   
    print ('a',count)
    return count
    #else:
        #print(count)
main()

No comments: