Skip to content

Instantly share code, notes, and snippets.

@jnozsc
Last active February 28, 2018 03:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnozsc/99a7c7328bc1ad3856b286b7e305f75e to your computer and use it in GitHub Desktop.
Save jnozsc/99a7c7328bc1ad3856b286b7e305f75e to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import argparse
def hanoi(number):
def hanoi_recursion(to_move=[], start=1, intermediate=2, end=3):
if len(to_move) <= 0:
return
elif len(to_move) == 1:
print(u'把 %d 号盘子从第 %d 个塔移动到第 %d 个塔' % (to_move[0], start, end))
else:
new_hanoi = to_move[:len(to_move) - 1]
hanoi_recursion(new_hanoi, start, end, intermediate)
hanoi_recursion([to_move[-1]], start, intermediate, end)
hanoi_recursion(new_hanoi, intermediate, start, end)
return hanoi_recursion(list(range(1, number+1)))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-n', '--number',
type=int,
default=9,
help='the level of hanoi tower'
)
args = parser.parse_args()
hanoi(args.number)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment