Towers of Hanoi: Difference between revisions

Content added Content deleted
Line 3,481: Line 3,481:
===Graphic===
===Graphic===


Refactoring the version above to recursively generate a graphic solution:
Refactoring the version above to recursively generate a simple visualisation:
{{Works with|Python|3.7}}
{{Works with|Python|3.7}}
<lang python>'''Towers of Hanoi'''
<lang python>'''Towers of Hanoi'''
Line 3,519: Line 3,519:
# showHanoi :: ([Int],[Int],[Int]) -> String
# showHanoi :: ([Int],[Int],[Int]) -> String
def showHanoi(tpl):
def showHanoi(tpl):
'''String representation of a stage of a Hanoi tower state.'''
'''String representation of a Hanoi tower state.'''


def fullHeight(n):
def fullHeight(n):
Line 3,550: Line 3,550:
# TEST ----------------------------------------------------
# TEST ----------------------------------------------------
def main():
def main():
'''Stages in a Hanoi tower sequence for N discs.
'''Visualisation of a Hanoi tower sequence for N discs.
'''
'''
n = 3
n = 3
print('Hanoi sequence for ' + str(3) + ' disks:\n')
print(unlines(
print(unlines(
fmap(showHanoi)(
fmap(showHanoi)(
Line 3,654: Line 3,655:
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</lang>
<pre>Hanoi sequence for 3 disks:
<pre> **

**
****
****
******
******