2018-10-04 14:37:37 +01:00
---
title: Towers of Hanoi
id: 5951ed8945deab770972ae56
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302341
2018-10-04 14:37:37 +01:00
---
## Description
< section id = 'description' >
2019-03-10 22:12:52 +09:00
Solve the < a href = "https://en.wikipedia.org/wiki/Towers_of_Hanoi" title = "wp: Towers_of_Hanoi" target = "_blank" > Towers of Hanoi< / a > problem.< / p >
Your solution should accept the number of discs as the first parameters, and three string used to identify each of the three stacks of discs, for example < code > towerOfHanoi(4, 'A', 'B', 'C')< / code > . The function should return an array of arrays containing the list of moves, source -> destination.
For example, the array < code > [['A', 'C'], ['B', 'A']]< / code > indicates that the 1st move was to move a disc from stack A to C, and the 2nd move was to move a disc from stack B to A.
2018-10-04 14:37:37 +01:00
< / p >
< / section >
## Instructions
< section id = 'instructions' >
< / section >
## Tests
< section id = 'tests' >
```yml
tests:
2019-11-20 07:01:31 -08:00
- text: < code > towerOfHanoi</ code > should be a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof towerOfHanoi === 'function');
2018-10-20 21:02:47 +03:00
- text: < code > towerOfHanoi(3, ...)</ code > should return 7 moves.
2019-07-26 05:24:52 -07:00
testString: assert(res3.length === 7);
2019-05-22 22:52:33 +09:00
- text: < code > towerOfHanoi(3, 'A', 'B', 'C')</ code > should return < code > [['A','B'], ['A','C'], ['B','C'], ['A','B'], ['C','A'], ['C','B'], ['A','B']]</ code > .
2019-07-26 05:24:52 -07:00
testString: assert.deepEqual(towerOfHanoi(3, 'A', 'B', 'C'), res3Moves);
2018-10-20 21:02:47 +03:00
- text: < code > towerOfHanoi(5, "X", "Y", "Z")</ code > 10th move should be Y -> X.
2019-07-26 05:24:52 -07:00
testString: assert.deepEqual(res5[9], ['Y', 'X']);
2019-11-20 07:01:31 -08:00
- text: < code > towerOfHanoi(7, 'A', 'B', 'C')</ code > first ten moves should be < code > [['A','B'], ['A','C'], ['B','C'], ['A','B'], ['C','A'], ['C','B'], ['A','B'], ['A','C'], ['B','C'], ['B','A']]</ code >
2019-07-26 05:24:52 -07:00
testString: assert.deepEqual(towerOfHanoi(7, 'A', 'B', 'C').slice(0, 10), res7First10Moves);
2018-10-04 14:37:37 +01:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-03-10 22:12:52 +09:00
function towerOfHanoi(n, a, b, c) {
2018-10-04 14:37:37 +01:00
// Good luck!
return [[]];
}
```
< / div >
### After Test
< div id = 'js-teardown' >
```js
2018-10-20 21:02:47 +03:00
const res3 = towerOfHanoi(3, 'A', 'B', 'C');
const res3Moves = [['A', 'B'], ['A', 'C'], ['B', 'C'], ['A', 'B'], ['C', 'A'], ['C', 'B'], ['A', 'B']];
const res5 = towerOfHanoi(5, 'X', 'Y', 'Z');
const res7First10Moves = [['A', 'B'], ['A', 'C'], ['B', 'C'], ['A', 'B'], ['C', 'A'], ['C', 'B'], ['A', 'B'], ['A', 'C'], ['B', 'C'], ['B', 'A']];
2018-10-04 14:37:37 +01:00
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
function towerOfHanoi(n, a, b, c) {
const res = [];
towerOfHanoiHelper(n, a, c, b, res);
return res;
}
function towerOfHanoiHelper(n, a, b, c, res) {
if (n > 0) {
towerOfHanoiHelper(n - 1, a, c, b, res);
res.push([a, c]);
towerOfHanoiHelper(n - 1, b, a, c, res);
}
}
```
< / section >