Towers of Hanoi: Difference between revisions
m
→Python recursive: (separating composition of the solution from its display)
(→{{header|Python}}: A variant which separates computation from display) |
m (→Python recursive: (separating composition of the solution from its display)) |
||
Line 3,377:
<lang python># hanoi :: Int -> String -> String -> String -> [(String, String)]
def hanoi(n
def go(n, a, b, c):
p = n - 1
Line 3,383:
go(p, a, c, b) + [(a, b)] + go(p, c, b, a)
) if 0 < n else []
return lambda a: lambda b: lambda c: go(n, a, b, c)
Line 3,395:
print(
'\n'.join(
lambda xy: justifyRight(5)(' ')(xy[0]) + ' -> ' + xy[1],▼
map(
hanoi(4, 'left', 'right', 'mid')▼
lambda xy: justifyRight(5)
</lang>▼
)
)
▲)</lang>
{{Out}}
<pre> left -> mid
Line 3,417 ⟶ 3,420:
left -> right
mid -> right</pre>
==={{libheader|VPython}}===
|