Last active
February 28, 2018 03:40
-
-
Save jnozsc/99a7c7328bc1ad3856b286b7e305f75e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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