-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathHanoi.py
29 lines (22 loc) · 893 Bytes
/
Hanoi.py
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
"""Tower of Hanoi."""
# @author willx75
# Tower of Hanoi recursion game algorithm is a game, it consists of three rods
# and a number of disks of different sizes, which can slide onto any rod
import logging
log = logging.getLogger()
logging.basicConfig(level=logging.DEBUG)
def Tower_Of_Hanoi(n, source, dest, by, movement):
"""Tower of Hanoi - Move plates to different rods."""
if n == 0:
return n
elif n == 1:
movement += 1
# no print statement
# (you could make it an optional flag for printing logs)
logging.debug('Move the plate from', source, 'to', dest)
return movement
else:
movement = movement + Tower_Of_Hanoi(n - 1, source, by, dest, 0)
logging.debug('Move the plate from', source, 'to', dest)
movement = movement + 1 + Tower_Of_Hanoi(n - 1, by, dest, source, 0)
return movement