Towers of Hanoi: Difference between revisions
Content added Content deleted
m (→{{header|Python}}: Tidying) |
|||
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''' |
|||
⚫ | |||
⚫ | |||
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 |
# TEST ---------------------------------------------------- |
||
if __name__ == '__main__': |
|||
⚫ | |||
# justifyRight :: Int -> Char -> String -> String |
|||
⚫ | |||
def justifyRight(n): |
|||
lambda xy: xy[0].rjust(5, ' ') + ' -> ' + xy[1], |
|||
((n * cFiller) + s)[-n:] |
|||
) |
|||
⚫ | |||
⚫ | |||
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 |