[ create a new paste ] login | about

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

◆6WD5LB4sFVj5 - Python, pasted on Jan 11:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# coding: utf8
"""
Tower of Hanoi Puzzle

author: ◆6WD5LB4sFVj5
date: 2011/01/12
"""

def hanoi(src, dest, work, discs):
  if discs == 0:  raise StopIteration()
  for x in hanoi(src, work, dest, discs-1):  yield x
  yield (discs, src, dest)
  for x in hanoi(src, work, dest, discs-1):  yield x


for (disc,src,dest) in hanoi('A', 'B', 'C', 4):
  print 'move %d from %s to %s' % (disc, src, dest)


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
move 1 from A to C
move 2 from A to B
move 1 from A to C
move 3 from A to C
move 1 from A to C
move 2 from A to B
move 1 from A to C
move 4 from A to B
move 1 from A to C
move 2 from A to B
move 1 from A to C
move 3 from A to C
move 1 from A to C
move 2 from A to B
move 1 from A to C


Create a new paste based on this one


Comments: