kernelthread.com

Python

The Towers of Hanoi as a Python program.

# # The Towers Of Hanoi # Python # Copyright (C) 1998 Amit Singh. All Rights Reserved. # http://hanoi.kernelthread.com # # Tested under GNU Python 1.5.1 import sys error = 'hanoi.error' def moveit(frm, to): print 'move %s --> %s' % (frm, to) def dohanoi(n, to, frm, using): if n == 0: return [] dohanoi(n-1, using, frm, to); moveit(frm, to); dohanoi(n-1, to, using, frm); def main(): if len(sys.argv) > 1: for arg in sys.argv[1:]: n = eval(arg) dohanoi(n, 3, 1, 2) else: try: while 1: n = input() dohanoi(n, 3, 1, 2) except EOFError: pass main()

Download

hanoi.py