Skip to content

Commit

Permalink
Add day 16
Browse files Browse the repository at this point in the history
  • Loading branch information
MadhavBahl committed Jan 10, 2019
1 parent a0a245c commit ba972ee
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -32,6 +32,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
13. [Day 13 -- Factorial and Fibonacci](./day13/) -- [http://codetoexpress.tech/dc/day13/](http://codetoexpress.tech/dc/day13/)
14. [Day 14 -- Sum of digits and product of numbers](./day14) -- [http://codetoexpress.tech/dc/day14/](http://codetoexpress.tech/dc/day14/)
15. [Day 15 -- Pascal's Triangle](./day15) -- [http://codetoexpress.tech/dc/day15/](http://codetoexpress.tech/dc/day15/)
16. [Day 16 -- Tower of Hanoi](./day16) -- [http://codetoexpress.tech/dc/day16/](http://codetoexpress.tech/dc/day16/)

## Timeline

Expand Down
21 changes: 21 additions & 0 deletions day16/JavaScript/hanoi_MadhavBahlMD.js
@@ -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');
47 changes: 47 additions & 0 deletions day16/README.md
@@ -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');
```
Binary file added day16/cover.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added day16/ques.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ba972ee

Please sign in to comment.