Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit c4a3620

Browse files
committed
Challenge 19
Signed-off-by: ShardulDS <ssajnekar@gmail.com>
1 parent 7b9ea34 commit c4a3620

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

contributors/ShardulDS/TOH.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''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:
2+
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+
7+
def TowerOfHanoi(n, from_rod, to_rod, aux_rod, y):
8+
if n == 0:
9+
return y
10+
y = TowerOfHanoi(n-1, from_rod, aux_rod, to_rod, y)
11+
print("Move disk", n, "from rod", from_rod, "to rod", to_rod)
12+
y += 1
13+
y = TowerOfHanoi(n-1, aux_rod, to_rod, from_rod, y)
14+
return y
15+
16+
if __name__ == '__main__':
17+
temp = TowerOfHanoi(int(input('No. of disks: ')), 'A', 'C', 'B', 0)
18+
print(f'{temp} moves')

0 commit comments

Comments
 (0)