This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: ShardulDS <[email protected]>
- Loading branch information
Showing
3 changed files
with
18 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.