Towers of Hanoi: Difference between revisions

Content added Content deleted
Line 3,422: Line 3,422:
Or, separating the composition of the data from its display:
Or, separating the composition of the data from its display:


<lang python>'''Towers of Hanoi'''
<lang python># hanoi :: Int -> String -> String -> String -> [(String, String)]


# hanoi :: Int -> String -> String -> String -> [(String, String)]
def hanoi(n):
def hanoi(n):
'''A list of (from, to) label pairs,
where a, b and c are labels for each of
the three Hanoi tower positions.'''
def go(n, a, b, c):
def go(n, a, b, c):
p = n - 1
p = n - 1
Line 3,432: Line 3,438:




# TEST AND DISPLAY -----------------------------------
# TEST ----------------------------------------------------
if __name__ == '__main__':

print(
# justifyRight :: Int -> Char -> String -> String
'\n'.join(map(
def justifyRight(n):
return lambda cFiller: lambda s: (
lambda xy: xy[0].rjust(5, ' ') + ' -> ' + xy[1],
((n * cFiller) + s)[-n:]
)


print(
'\n'.join(
map(
lambda xy: justifyRight(5)
(' ')(xy[0]) + ' -> ' + xy[1],
hanoi(4)('left')('right')('mid')
hanoi(4)('left')('right')('mid')
)
))
)
)</lang>
)</lang>
{{Out}}
{{Out}}
<pre> left -> mid
<pre> left -> mid