1+ class Tower (object ):
2+ # create three towers (i from 0 to 2)
3+ def __init__ (self , i ):
4+ self .disks = []
5+
6+ # Add a disk into this tower
7+ def add (self , d ):
8+ if len (self .disks ) > 0 and self .disks [- 1 ] <= d :
9+ print "Error placing disk %s" % d
10+ else :
11+ self .disks .append (d );
12+
13+ # @param {Tower} t a tower
14+ # Move the top disk of this tower to the top of t.
15+ def move_top_to (self , t ):
16+ # Write your code here
17+ if len (self .disks ) > 0 :
18+ t .disks .append (self .disks .pop ())
19+
20+ # @param {int} n an integer
21+ # @param {Tower} destination a tower
22+ # @param {Tower} buffer a tower
23+ # Move n Disks from this tower to destination by buffer tower
24+ def move_disks (self , n , destination , buffer ):
25+ # Write your code here
26+ if n == 0 :
27+ return
28+ elif n == 1 :
29+ self .move_top_to (destination )
30+ else :
31+ self .move_disks (n - 1 , buffer , destination )
32+ self .move_top_to (destination )
33+ buffer .move_disks (n - 1 , destination , self )
34+
35+ def get_disks (self ):
36+ return self .disks
37+
38+ """
39+ Your Tower object will be instantiated and called as such:
40+ towers = [Tower(0), Tower(1), Tower(2)]
41+ for i in xrange(n - 1, -1, -1): towers[0].add(i)
42+ towers[0].move_disks(n, towers[2], towers[1])
43+ print towers[0], towers[1], towers[2]
44+ """
0 commit comments