[ create a new paste ] login | about

Link: http://codepad.org/kiaIznYR    [ raw code | fork ]

Python, pasted on Jan 11:
class Hanoi(object):
  def __init__(self, height):
    self.step = 0
    self.bars = [range(height, 0, -1), [], []]
    self.gen = self.move(0, 1, 2, len(self.bars[0]))

  def stat(self):
    return self.bars

  def move(self, src, dst, work, discs):
    if discs == 0: raise StopIteration()
    for m in self.move(src, work, dst, discs - 1): yield m
    yield(src, dst)
    for m in self.move(work, dst, src, discs - 1): yield m

  def next(self):
    self.step += 1
    src, dst = self.gen.next()
    self.bars[dst].append(self.bars[src].pop())

if __name__ == '__main__':
  import sys
  try:
    height = int(sys.argv[1])
  except (IndexError, TypeError, ValueError), e:
    height = 3
  h = Hanoi(height)
  try:
    while True:
      print h.step, h.stat()
      h.next()
  except StopIteration, e:
    pass


Create a new paste based on this one


Comments: