Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hanoi): create exercise #448

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2caf310
feat/ generate exercise
NikitaRevenco Mar 24, 2024
d5d8bc8
feat(hanoi): create solution
NikitaRevenco Apr 26, 2024
9577544
feat(hanoi): create explanation readme
NikitaRevenco Apr 26, 2024
509c18d
feat(hanoi): create tests for exercise
NikitaRevenco May 3, 2024
507b137
refactor: wording
NikitaRevenco Jul 3, 2024
2d6cdc3
chore: use `_` instead of `*`
NikitaRevenco Jul 3, 2024
c9f385f
refactor: wording
NikitaRevenco Jul 3, 2024
85570c6
fix: spelling
NikitaRevenco Jul 3, 2024
842e1b7
refactor: remove redundant `length` tests
NikitaRevenco Jul 3, 2024
6163c8d
feat: clarify requirements
NikitaRevenco Jul 3, 2024
e010cdc
feat(hanoi): add line breaks
NikitaRevenco Aug 18, 2024
1fc7d09
feat(hanoi): add line breaks
NikitaRevenco Aug 18, 2024
3f98387
feat(hanoi): add line breaks
NikitaRevenco Aug 18, 2024
58a7f67
feat(hanoi): more concise representation of function in readme
NikitaRevenco Aug 18, 2024
12cd75c
feat(hanoi): change the way the function is tested to use strings ins…
NikitaRevenco Aug 18, 2024
98242fb
feat(hanoi): copy over tests from solution
NikitaRevenco Aug 20, 2024
7b343a1
feat: remove redundant test
NikitaRevenco Aug 25, 2024
dbd8ee1
doc(hanoi): update instructions to use the new expected method
NikitaRevenco Aug 25, 2024
86a7cfa
fix(hanoi): spelling error
NikitaRevenco Aug 25, 2024
e6d5034
feat: use exclamation mark instead of period
NikitaRevenco Aug 25, 2024
70e61a4
feat: provide a more explicit example of what form the string should …
NikitaRevenco Aug 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions 20_hanoi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Exercise 20 - hanoi

Good job, you have come a very long way. You should be proud of yourself for making it this far. At this point, you hopefully feel comfortable
with recursion, and so, this **final recursive exercise** should give you a proper challenge and take your abilities to the *next* level.
NikitaRevenco marked this conversation as resolved.
Show resolved Hide resolved

Tower of Hanoi is a classic challenge in which we have 3 towers. Tower 2 and tower 3 are empty.
Tower 1 has `n` disks. The goal is to move all disks from tower 1 to tower 3 so that they are in the exact same order. For instance:
NikitaRevenco marked this conversation as resolved.
Show resolved Hide resolved

```javascript
[[3, 2, 1], [], []]

// ...

[[], [], [3, 2, 1]]
```

The rules are as follows:
- Each disk has a length. For abstraction purposes, we will imagine each disk as an integer with that disk's length.
- We can only move 1 disk at a time between any of the towers
- The towers are modelled as stacks. We can move disks from the top of one stack to the top of another stack with the `Array.prototype.pop` and `Array.prototype.push` methods.
- **Disks can only be placed on top of disks that are smaller**. For instance, we cannot have the tower `[3, 4]` but `[4, 3]` is fine.

Your task is to create a function, `hanoi(n)`, that when given the number of disks in the starting tower (`n`), will return an array. The first element of this array will be the towers' initial state. The last element will be the towers' final state. Every intermediary element will represent a step to get from the initial position to the final position.
The function **must** return a solution in the minimum number of moves. i.e. there will be no duplicates in the array returned.
NikitaRevenco marked this conversation as resolved.
Show resolved Hide resolved

For example, lets say we had given this function `3`, it will then output the Tower of Hanoi solution as a series of steps:
NikitaRevenco marked this conversation as resolved.
Show resolved Hide resolved

```
[
[[3, 2, 1], [], []],
[[3, 2], [], [1]],
[[3], [2], [1]],
[[3], [2, 1], []],
[[], [2, 1], [3]],
[[1], [2], [3]],
[[1], [], [3, 2]],
[[], [], [3, 2, 1]],
]
```
6 changes: 6 additions & 0 deletions 20_hanoi/hanoi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const hanoi = function() {

};

// Do not edit below this line
module.exports = hanoi;
15 changes: 15 additions & 0 deletions 20_hanoi/hanoi.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const hanoi = require('./hanoi');

describe('hanoi', () => {
test('First test description', () => {
// Replace this comment with any other necessary code, and update the expect line as necessary

expect(hanoi()).toBe('');
});

test.skip('Second test description', () => {
// Replace this comment with any other necessary code, and update the expect line as necessary

expect(hanoi()).toBe('');
});
});
29 changes: 29 additions & 0 deletions 20_hanoi/solution/hanoi-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const hanoi = function (
n,
fromTowerIndex = 0,
storageTowerIndex = 1,
toTowerIndex = 2,
towers = [
Array(n)
.fill() // Array function creates an array with "empty" slots which map does not iterate over
.map((_, i) => n - i),
[],
[],
],
steps = [towers.map((tower) => [...tower])],
) {
NikitaRevenco marked this conversation as resolved.
Show resolved Hide resolved
if (n === 0) return steps;

hanoi(n - 1, fromTowerIndex, toTowerIndex, storageTowerIndex, towers, steps);

towers[toTowerIndex].push(towers[fromTowerIndex].pop());

steps.push(towers.map((tower) => [...tower]));

hanoi(n - 1, storageTowerIndex, fromTowerIndex, toTowerIndex, towers, steps);

return steps;
};

// Do not edit below this line
module.exports = hanoi;
61 changes: 61 additions & 0 deletions 20_hanoi/solution/hanoi-solution.spec.js
NikitaRevenco marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const hanoi = require("./hanoi-solution");

describe("hanoi", () => {
test("hanoi(1) should solve the puzzle in 1 move", () => {
const result = hanoi(1);
const expected = [
[[1], [], []],
[[], [], [1]],
];
expect(result).toEqual(expected);
});

test("hanoi(2) should solve the puzzle in 3 moves", () => {
const result = hanoi(2);
const expected = [
[[2, 1], [], []],
[[2], [1], []],
[[], [1], [2]],
[[], [], [2, 1]],
];
expect(result).toEqual(expected);
});

test("hanoi(3) should solve the puzzle in 7 moves", () => {
const result = hanoi(3);
const expected = [
[[3, 2, 1], [], []],
[[3, 2], [], [1]],
[[3], [2], [1]],
[[3], [2, 1], []],
[[], [2, 1], [3]],
[[1], [2], [3]],
[[1], [], [3, 2]],
[[], [], [3, 2, 1]],
];
expect(result).toEqual(expected);
});

test("hanoi(4) should solve the puzzle in 15 moves", () => {
const result = hanoi(4);
const expected = [
[[4, 3, 2, 1], [], []],
[[4, 3, 2], [1], []],
[[4, 3], [1], [2]],
[[4, 3], [], [2, 1]],
[[4], [3], [2, 1]],
[[4, 1], [3], [2]],
[[4, 1], [3, 2], []],
[[4], [3, 2, 1], []],
[[], [3, 2, 1], [4]],
[[], [3, 2], [4, 1]],
[[2], [3], [4, 1]],
[[2, 1], [3], [4]],
[[2, 1], [], [4, 3]],
[[2], [1], [4, 3]],
[[], [1], [4, 3, 2]],
[[], [], [4, 3, 2, 1]],
];
expect(result).toEqual(expected);
});
});