Towers of Hanoi: Difference between revisions

→‎Python Graphic: Added caption texts to simple ascii diagrams
m (→‎Python Graphic: Switched display characters to unbroken (underscore) lines)
(→‎Python Graphic: Added caption texts to simple ascii diagrams)
Line 3,503:
 
 
# hanoiState :: ([Int],[Int],[Int], String) -> (Int, Int) -> ([Int],[Int],[Int])
# ([Int],[Int],[Int], String)
def hanoiState(tpl, ab):
'''A new Hanoi tower state'''
Line 3,514 ⟶ 3,515:
) else [xs[0]] + ys
 
return tuple(map(delta, [0, 1, 2])) + (
moveName(('left', 'mid', 'right'))(ab),
)
 
 
# showHanoi :: ([Int],[Int],[Int], String) -> String
def showHanoi(tpl):
'''StringCaptioned string representation of aan updated Hanoi tower state.'''
 
def fullHeight(n):
Line 3,527 ⟶ 3,530:
lt = curry(operator.lt)
rods = fmap(fmap(mul('__')))(
list([tpl)[0], tpl[1], tpl[2]]
)
h = max(map(len, rods))
Line 3,543 ⟶ 3,546:
)(rods))
)
return unlines(xs) + '\n' + ('_' * 3 * w) + '\n' + tpl[3] + (
'\n' * 2
)
 
 
# moveName :: (String, String, String) -> (Int, Int) -> String
def moveName(labels):
'''(from, to) index pair represented as an a -> b string.'''
def go(ab):
a, b = ab
return labels[a] + ' -> ' + labels[b]
return lambda ab: go(ab)
 
 
Line 3,557 ⟶ 3,569:
fmap(showHanoi)(
scanl(hanoiState)(
(enumFromTo(1)(n), [], [], '')
)(hanoi(n))
)
Line 3,626 ⟶ 3,638:
return lambda a: lambda xs: (
accumulate(chain([a], xs), f)
)
 
 
# showLog :: a -> IO String
def showLog(*s):
'''Arguments printed with
intercalated arrows.'''
print(
' -> '.join(map(str, s))
)
 
Line 3,661 ⟶ 3,682:
______
________________________
 
 
 
Line 3,666 ⟶ 3,688:
______ __
________________________
left -> right
 
 
______ ____ __
________________________
left -> mid
 
 
Line 3,675 ⟶ 3,699:
______ ____
________________________
right -> mid
 
 
Line 3,680 ⟶ 3,705:
____ ______
________________________
left -> right
 
 
__ ____ ______
________________________
mid -> left
 
 
Line 3,689 ⟶ 3,716:
__ ______
________________________
mid -> right
 
 
Line 3,694 ⟶ 3,722:
____
______
________________________</pre>
left -> right</pre>
 
==={{libheader|VPython}}===
9,659

edits