You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 30, 2023. It is now read-only.
'''Tower of Hanoi is a mathematical puzzle where we have three rods (A, B, and C) and N disks. Initially, all the disks are stacked in decreasing value of diameter i.e., the smallest disk is placed on the top and they are on rod A. The objective of the puzzle is to move the entire stack to another rod (here considered C), obeying the following simple rules:
1
+
"""Tower of Hanoi is a mathematical puzzle where we have three rods
2
+
(A, B, and C) and N disks. Initially, all the disks are stacked in decreasing
3
+
value of diameter i.e., the smallest disk is placed on the top and they are on
4
+
rod A. The objective of the puzzle is to move the entire stack to another rod
5
+
(here considered C), obeying the following simple rules:
6
+
- Only one disk can be moved at a time.
7
+
- Each move consists of taking the upper disk from one of the stacks and
8
+
placing it on top of another stack i.e. a disk can only be moved if it
9
+
is the uppermost disk on a stack.
10
+
- No disk may be placed on top of a smaller disk."""
2
11
3
-
Only one disk can be moved at a time.
4
-
Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
5
-
No disk may be placed on top of a smaller disk.'''
6
12
7
13
defTowerOfHanoi(n, from_rod, to_rod, aux_rod, y):
8
14
ifn==0:
9
15
returny
10
-
y=TowerOfHanoi(n-1, from_rod, aux_rod, to_rod, y)
16
+
y=TowerOfHanoi(n-1, from_rod, aux_rod, to_rod, y)
11
17
print("Move disk", n, "from rod", from_rod, "to rod", to_rod)
12
18
y+=1
13
-
y=TowerOfHanoi(n-1, aux_rod, to_rod, from_rod, y)
19
+
y=TowerOfHanoi(n-1, aux_rod, to_rod, from_rod, y)
14
20
returny
15
21
16
-
if__name__=='__main__':
17
-
temp=TowerOfHanoi(int(input('No. of disks: ')), 'A', 'C', 'B', 0)
18
-
print(f'{temp} moves')
22
+
23
+
if__name__=="__main__":
24
+
temp=TowerOfHanoi(int(input("No. of disks: ")), "A", "C", "B", 0)
0 commit comments