[[練習問題]] #!/usr/bin/python # -*- coding: utf-8 -*- cnt = 1 def solve(n, A, B, C): global cnt if n > 0: solve(n-1, A, C, B) if A[0]: disc = A[0].pop() print '%2d: move %d from %s to %s'%(cnt, disc, A[1], C[1]) cnt += 1 C[0].append(disc) solve(n-1, B, A, C) def hanoi(n, frm, work, dest): global cnt if n < 1: return hanoi(n-1, frm, dest, work) print('{0:2}: {1}を{2}から{3}に移動したよ。'.format(cnt, n, frm, dest)) cnt += 1 hanoi(n-1, work, frm, dest) def hanoi(n): A = (range(1, n+1)[::-1], 'source') B = ([], 'helper') C = ([], 'target') solve(n, A, B, C) hanoi(4) hanoi(4, 'A', 'B', 'C')