jhylands

Towers oh hanoi

Jan 23rd, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. def ToH(sourcen,destn,sparen,n,source=None,destination=None, spare=None,printer=None):
  2.         if source == None:
  3.                 source = range(1,n+1)[::-1]
  4.                 destination = []
  5.                 spare = []
  6.                 printer = []
  7.         if n==1:
  8.                 printer.append( "Move disk" + str(source[-1]) + " from " + str(sourcen) + " to " + str(destn))
  9.                 #print len(printer)
  10.  
  11.                 destination.append(source.pop())
  12.         else:
  13.                 printer += ToH(sourcen,sparen,destn,(n-1),source,spare,destination,printer)
  14.                 printer += ToH(sourcen,destn,sparen,1,source,destination,spare,printer)
  15.                 printer += ToH(sparen,destn,sourcen,(n-1),spare, destination, source,printer)
  16.         if len(printer)>100:
  17.                 print "!!"
  18.                 print printer[99]
  19.                 print ""
  20.                 raise("Problem")
  21.         return printer
  22.  
  23. print "starting..."
  24. ToH("A","B","C",13)
Advertisement
Add Comment
Please, Sign In to add comment