forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtower_of_hanoi.py
28 lines (22 loc) · 700 Bytes
/
tower_of_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
defmove_tower(height, from_pole, to_pole, with_pole):
"""
>>> move_tower(3, 'A', 'B', 'C')
moving disk from A to B
moving disk from A to C
moving disk from B to C
moving disk from A to B
moving disk from C to A
moving disk from C to B
moving disk from A to B
"""
ifheight>=1:
move_tower(height-1, from_pole, with_pole, to_pole)
move_disk(from_pole, to_pole)
move_tower(height-1, with_pole, to_pole, from_pole)
defmove_disk(fp, tp):
print("moving disk from", fp, "to", tp)
defmain():
height=int(input("Height of hanoi: ").strip())
move_tower(height, "A", "B", "C")
if__name__=="__main__":
main()