1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| class Stick: def __init__(self,name,list): self.name=name self.list=list def pop(self): return self.list.pop() def append(self, element): self.list.append(element) def __str__(self): return self.name+':'+str(self.list)
def move(n,a,b,c): if n==1: move_action(a,c) return move(n-1,a,c,b) move_action(a,c) move(n-1,b,a,c)
def move_action(source, target): size = len(source.list) print('move {0} from {1} to {2}.'.format(source.list[size-1],source.name,target.name)) target.append(source.pop())
a=Stick('A',[4,3,2,1]) b=Stick('B',[]) c=Stick('C',[]) print(a) print(b) print(c)
print('start moving:') n = len(a.list) move(n,a,b,c) print(a) print(b) print(c)
|