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

Commit

Permalink
Challenge 19 Try 2
Browse files Browse the repository at this point in the history
Signed-off-by: ShardulDS <[email protected]>
  • Loading branch information
ShardulDS committed Sep 19, 2022
1 parent c4a3620 commit 840e125
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
25 changes: 16 additions & 9 deletions contributors/ShardulDS/TOH.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
'''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:
"""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:
- Only one disk can be moved at a time.
- 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.
- No disk may be placed on top of a smaller disk."""

Only one disk can be moved at a time.
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.
No disk may be placed on top of a smaller disk.'''

def TowerOfHanoi(n, from_rod, to_rod, aux_rod, y):
if n == 0:
return y
y = TowerOfHanoi(n-1, from_rod, aux_rod, to_rod, y)
y = TowerOfHanoi(n - 1, from_rod, aux_rod, to_rod, y)
print("Move disk", n, "from rod", from_rod, "to rod", to_rod)
y += 1
y = TowerOfHanoi(n-1, aux_rod, to_rod, from_rod, y)
y = TowerOfHanoi(n - 1, aux_rod, to_rod, from_rod, y)
return y

if __name__ == '__main__':
temp = TowerOfHanoi(int(input('No. of disks: ')), 'A', 'C', 'B', 0)
print(f'{temp} moves')

if __name__ == "__main__":
temp = TowerOfHanoi(int(input("No. of disks: ")), "A", "C", "B", 0)
print(f"{temp} moves")
2 changes: 2 additions & 0 deletions contributors/ShardulDS/gist-solutions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Software Development Topic](https://gist.github.com/ShardulDS/234943fa902a5a2f94a4397db2120a34)
[Code snippet](https://gist.github.com/ShardulDS/d2bfa0b9fdd7a21a19cf2a63fa312a47)
2 changes: 0 additions & 2 deletions gist-solutions.md

This file was deleted.

0 comments on commit 840e125

Please sign in to comment.