Skip to content

Instantly share code, notes, and snippets.

@yokiy
Created July 5, 2014 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yokiy/ceee7892798ca326a0e7 to your computer and use it in GitHub Desktop.
Save yokiy/ceee7892798ca326a0e7 to your computer and use it in GitHub Desktop.
cc 3.4
# cc 3.4 Tower of Hanoi
from stack import Stack, Node
class tower:
def __init__(self):
self.start = Stack()
self.dest = Stack()
self.buff = Stack()
self.tower = [self.start, self.buff, self.dest]
self.disks = 0
def add(self, item):
self.start.push(item)
self.disks = self.start.size
def moveDisk(self, disknum, start, dest, buff):
if disknum > 0:
self.moveDisk(disknum - 1, start, buff, dest) #move top n-1 disks to buff tower
self.moveTop(start, dest)
self.moveDisk(disknum - 1, buff, dest, start) #move from buff to dest use start as buff
def moveTop(self, start, dest):
top = start.pop()
dest.push(top)
#test
tower = tower()
tower.add(4)
tower.add(3)
tower.add(2)
tower.add(1)
print 'the original'
tower.start.printStack()
print '\n---------------'
tower.buff.printStack()
print '---------------'
tower.dest.printStack()
tower.moveDisk(4, tower.start, tower.dest, tower.buff)
print 'after change'
tower.start.printStack()
print '-------------'
tower.buff.printStack()
print '-------------'
tower.dest.printStack()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment