2018-10-04 13:37:37 +00:00
---
id: 5951ed8945deab770972ae56
2020-11-27 18:02:05 +00:00
title: Towers of Hanoi
2018-10-04 13:37:37 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302341
2021-01-13 02:31:00 +00:00
dashedName: towers-of-hanoi
2018-10-04 13:37:37 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2022-06-13 08:27:25 +00:00
Solve the Towers of Hanoi problem. The number of objects will be given as the first parameter, followed by the strings used to identify each stack of objects. Create a nested array containing the list of moves, `["source", "destination"]` .
For example, the parameters `(4, 'A', 'B', 'C')` , will result in nested array of moves `[['A', 'C'], ['B', 'A']]` , indicating that the 1st move was to move an object from stack `A` to `C` and the 2nd move was to move an object from stack `B` to `A` .
2020-11-27 18:02:05 +00:00
2022-06-13 08:27:25 +00:00
# --instructions--
2020-11-27 18:02:05 +00:00
2022-06-13 08:27:25 +00:00
Write a function that returns the moves to stack the objects in a nested array.
2020-11-27 18:02:05 +00:00
# --hints--
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
`towerOfHanoi` should be a function.
```js
assert(typeof towerOfHanoi === 'function');
2018-10-04 13:37:37 +00:00
```
2020-11-27 18:02:05 +00:00
`towerOfHanoi(3, ...)` should return 7 moves.
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(res3.length === 7);
```
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
`towerOfHanoi(3, 'A', 'B', 'C')` should return `[['A','B'], ['A','C'], ['B','C'], ['A','B'], ['C','A'], ['C','B'], ['A','B']]` .
2018-10-04 13:37:37 +00:00
```js
2020-11-27 18:02:05 +00:00
assert.deepEqual(towerOfHanoi(3, 'A', 'B', 'C'), res3Moves);
```
2020-09-15 16:57:40 +00:00
2020-11-27 18:02:05 +00:00
`towerOfHanoi(5, "X", "Y", "Z")` 10th move should be Y -> X.
```js
assert.deepEqual(res5[9], ['Y', 'X']);
2018-10-04 13:37:37 +00:00
```
2020-11-27 18:02:05 +00:00
`towerOfHanoi(7, 'A', 'B', 'C')` first ten moves should be `[['A','B'], ['A','C'], ['B','C'], ['A','B'], ['C','A'], ['C','B'], ['A','B'], ['A','C'], ['B','C'], ['B','A']]`
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
assert.deepEqual(towerOfHanoi(7, 'A', 'B', 'C').slice(0, 10), res7First10Moves);
```
# --seed--
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
## --after-user-code--
2018-10-04 13:37:37 +00:00
```js
2018-10-20 18:02:47 +00: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 13:37:37 +00:00
```
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
```js
function towerOfHanoi(n, a, b, c) {
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
return [[]];
}
```
2018-10-04 13:37:37 +00:00
2020-11-27 18:02:05 +00:00
# --solutions--
2018-10-04 13:37:37 +00:00
```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);
}
}
```