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

Commit 840e125

Browse files
committed
Challenge 19 Try 2
Signed-off-by: ShardulDS <[email protected]>
1 parent c4a3620 commit 840e125

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

contributors/ShardulDS/TOH.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
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:
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."""
211

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.'''
612

713
def TowerOfHanoi(n, from_rod, to_rod, aux_rod, y):
814
if n == 0:
915
return y
10-
y = TowerOfHanoi(n-1, from_rod, aux_rod, to_rod, y)
16+
y = TowerOfHanoi(n - 1, from_rod, aux_rod, to_rod, y)
1117
print("Move disk", n, "from rod", from_rod, "to rod", to_rod)
1218
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)
1420
return y
1521

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)
25+
print(f"{temp} moves")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[Software Development Topic](https://gist.github.com/ShardulDS/234943fa902a5a2f94a4397db2120a34)
2+
[Code snippet](https://gist.github.com/ShardulDS/d2bfa0b9fdd7a21a19cf2a63fa312a47)

gist-solutions.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)