Skip to content

Instantly share code, notes, and snippets.

@yaotti
Created October 25, 2009 09:41
Show Gist options
  • Save yaotti/217981 to your computer and use it in GitHub Desktop.
Save yaotti/217981 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# coding: utf8
def hanoi(n, from_, to, via):
"tower of hanoi"
if n == 1:
print "move %d: %s => %s" % (n, from_, to)
else:
hanoi(n-1, from_, via, to)
print "move %d: %s => %s" % (n, from_, to)
hanoi(n-1, via, to, from_)
hanoi(3, "left", "center", "right")
# move 1: left => center
# move 2: left => right
# move 1: center => right
# move 3: left => center
# move 1: right => left
# move 2: right => center
# move 1: left => center
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment