Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0a245c
commit ba972ee
Showing
5 changed files
with
69 additions
and
0 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
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,21 @@ | ||
/** | ||
* @source Geeks4Geeks: https://www.geeksforgeeks.org/c-program-for-tower-of-hanoi/ | ||
* Implementaed in JavaScript by @MadhavBahlMD | ||
* @date 10/01/2019 | ||
*/ | ||
|
||
function towerOfHanoi (num, fromRod, toRod, auxRod) { | ||
if (num === 1) { | ||
console.log (`Move disk 1 from rod ${fromRod} to ${toRod}`); | ||
return 0; | ||
} | ||
|
||
towerOfHanoi (num-1, fromRod, auxRod, toRod); | ||
console.log (`Move disk ${num} from rod ${fromRod} to ${toRod}`); | ||
towerOfHanoi (num-1, auxRod, toRod, fromRod); | ||
} | ||
|
||
console.log ('/* ===== for 2 disks ===== */'); | ||
towerOfHanoi (2, 'A', 'C', 'B'); | ||
console.log ('\n/* ===== for 3 disks ===== */'); | ||
towerOfHanoi (3, 'A', 'C', 'B'); |
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,47 @@ | ||
![cover](./cover.png) | ||
|
||
# Day 16 - Recursion Series Part D | ||
|
||
Today's Problem - Tower of Hanoi | ||
|
||
## Tower of Hanoi | ||
|
||
The Tower of Hanoi puzzle was invented by the French mathematician Edouard Lucas in 1883. | ||
|
||
**Question** -- Given 3 rods and n disks, write a program to display the steps involved in moving the whole stack of rods from one rod to another given the following constraints: | ||
|
||
1. Only one disk can be moved at a time | ||
2. 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 | ||
3. No disk may be placed on top of a smaller disk | ||
|
||
[read more...](http://interactivepython.org/runestone/static/pythonds/Recursion/TowerofHanoi.html) | ||
|
||
![ques](./ques.png) | ||
|
||
## JavaScript Implementation | ||
|
||
### [Solution](./JavaScript/hanoi_MadhavBahlMD.js) | ||
|
||
```js | ||
/** | ||
* @source Geeks4Geeks: https://www.geeksforgeeks.org/c-program-for-tower-of-hanoi/ | ||
* Implementaed in JavaScript by @MadhavBahlMD | ||
* @date 10/01/2019 | ||
*/ | ||
|
||
function towerOfHanoi (num, fromRod, toRod, auxRod) { | ||
if (num === 1) { | ||
console.log (`Move disk 1 from rod ${fromRod} to ${toRod}`); | ||
return 0; | ||
} | ||
|
||
towerOfHanoi (num-1, fromRod, auxRod, toRod); | ||
console.log (`Move disk ${num} from rod ${fromRod} to ${toRod}`); | ||
towerOfHanoi (num-1, auxRod, toRod, fromRod); | ||
} | ||
|
||
console.log ('/* ===== for 2 disks ===== */'); | ||
towerOfHanoi (2, 'A', 'C', 'B'); | ||
console.log ('\n/* ===== for 3 disks ===== */'); | ||
towerOfHanoi (3, 'A', 'C', 'B'); | ||
``` |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.