Created
December 11, 2011 14:21
-
-
Save bellbind/1460839 to your computer and use it in GitHub Desktop.
[python]tower of hanoi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hanoi_imp(n, curr, dest, rest): | |
if n == 0: return | |
hanoi_imp(n - 1, curr, rest, dest) | |
print("move %n from %s to %s" % (n, curr, dest)) | |
hanoi_imp(n - 1, rest, dest, curr) | |
return | |
def hanoi_fun(n, curr, dest, rest): | |
return [] if n == 0 else ( | |
hanoi_fun(n - 1, curr, rest, dest) + | |
[(n, curr, dest)] + | |
hanoi_fun(n - 1, rest, dest, curr)) | |
def hanoi_gen(n, curr, dest, rest): | |
if n == 0: return | |
for _ in hanoi_gen(n - 1, curr, rest, dest): yield _ | |
yield (n, curr, dest) | |
for _ in hanoi_gen(n - 1, rest, dest, curr): yield _ | |
return | |
def hanoi_cps(n, curr, dest, rest, acc=[], cont=lambda acc: acc): | |
return cont(acc) if n == 0 else hanoi_cps( | |
n - 1, curr, rest, dest, acc, lambda acc: hanoi_cps( | |
n - 1, rest, dest, curr, acc + [(n, curr, dest)], cont)) | |
if __name__ == "__main__": | |
hanoi_imp(3, "A", "B", "C") | |
print(hanoi_fun(3, "A", "B", "C")) | |
print(list(hanoi_gen(3, "A", "B", "C"))) | |
print(hanoi_cps(3, "A", "B", "C")) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment