Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb2f1d9c

Browse files
willx75poyea
authored andcommitted
implementation of tower_of_hanoi algorithm (TheAlgorithms#756)
1 parent52d2fbf commitb2f1d9c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎maths/Hanoi.py

Lines changed: 24 additions & 0 deletions
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+
importlogging
5+
6+
log=logging.getLogger()
7+
logging.basicConfig(level=logging.DEBUG)
8+
9+
10+
defTower_Of_Hanoi(n,source,dest,by,mouvement):
11+
ifn==0:
12+
returnn
13+
elifn==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+
returnmouvement
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+
returnmouvement

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp