Skip to content

Commit b2f1d9c

Browse files
willx75poyea
authored andcommitted
implementation of tower_of_hanoi algorithm (TheAlgorithms#756)
1 parent 52d2fbf commit b2f1d9c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

maths/Hanoi.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# @author willx75
2+
# 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
3+
4+
import logging
5+
6+
log = logging.getLogger()
7+
logging.basicConfig(level=logging.DEBUG)
8+
9+
10+
def Tower_Of_Hanoi(n, source, dest, by, mouvement):
11+
if n == 0:
12+
return n
13+
elif n == 1:
14+
mouvement += 1
15+
# no print statement (you could make it an optional flag for printing logs)
16+
logging.debug('Move the plate from', source, 'to', dest)
17+
return mouvement
18+
else:
19+
20+
mouvement = mouvement + Tower_Of_Hanoi(n-1, source, by, dest, 0)
21+
logging.debug('Move the plate from', source, 'to', dest)
22+
23+
mouvement = mouvement + 1 + Tower_Of_Hanoi(n-1, by, dest, source, 0)
24+
return mouvement

0 commit comments

Comments
 (0)