Recent Posts
«   2018/01   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags more
Archives
Today
11
Total
142,275
관리 메뉴

## python towers of hanoi__6_6 본문

0x00 개발/Python

### python towers of hanoi__6_6

영준이 2015.10.27 06:50
``````
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
``````
공유하기 링크