Advertisement
LordAro

Towers of Hanoi

Jan 13th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. #!/usr/bin/env python2
  2.  
  3. class Pole(list):
  4.     def __init__(self, label, alist):
  5.         self.label = label
  6.         list.__init__(self, alist)
  7.  
  8.  
  9. def ToH(source, dest, spare, n, movenum):
  10.     if n == 1:
  11.         movenum[0] += 1
  12.         print "{}: Move disc {} from {} to {}"\
  13.             .format(movenum[0], source[-1],
  14.                     source.label, dest.label)
  15.         dest.append(source.pop())
  16.     else:
  17.         ToH(source, spare, dest, n - 1, movenum)
  18.         ToH(source, dest, spare, 1, movenum)
  19.         ToH(spare, dest, source, n - 1, movenum)
  20.  
  21.  
  22. num = 13
  23.  
  24. initList = list(xrange(1, num + 1))
  25. initList.reverse()
  26. A = Pole("A", initList)
  27. B = Pole("B", [])
  28. C = Pole("C", [])
  29. ToH(A, B, C, num, [0])
  30. print A
  31. print B
  32. print C
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement