Skip to content

Commit 02fe302

Browse files
tower of hanio (#25)
1 parent e2a1efc commit 02fe302

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

maths/tower_of_hanio.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
https://en.wikipedia.org/wiki/Tower_of_Hanoi
3+
"""
4+
5+
6+
def move(number_of_tower: int, a_place: str, b_place: str, c_place: str) -> None:
7+
"""
8+
>>> move(0, "A", "B", "C")
9+
>>> move(1, "A", "B", "C")
10+
move from A to B
11+
>>> move(2, "A", "B", "C")
12+
move from A to C
13+
move from A to B
14+
move from C to B
15+
>>> move(3, "A", "B", "C")
16+
move from A to B
17+
move from A to C
18+
move from B to C
19+
move from A to B
20+
move from C to A
21+
move from C to B
22+
move from A to B
23+
>>> move(4, "A", "B", "C")
24+
move from A to C
25+
move from A to B
26+
move from C to B
27+
move from A to C
28+
move from B to A
29+
move from B to C
30+
move from A to C
31+
move from A to B
32+
move from C to B
33+
move from C to A
34+
move from B to A
35+
move from C to B
36+
move from A to C
37+
move from A to B
38+
move from C to B
39+
"""
40+
if number_of_tower != 0:
41+
move(number_of_tower - 1, a_place, c_place, b_place)
42+
print(f"move from {a_place} to {b_place}")
43+
move(number_of_tower - 1, c_place, b_place, a_place)
44+
45+
46+
if __name__ == "__main__":
47+
from doctest import testmod
48+
49+
testmod()

0 commit comments

Comments
 (0)