-
Notifications
You must be signed in to change notification settings - Fork 40.8k
feat(hanoi): create exercise #448
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
Open
nik-rev
wants to merge
21
commits into
TheOdinProject:main
Choose a base branch
from
nik-contrib:feat/exercise_20
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
2caf310
feat/ generate exercise
nik-rev d5d8bc8
feat(hanoi): create solution
nik-rev 9577544
feat(hanoi): create explanation readme
nik-rev 509c18d
feat(hanoi): create tests for exercise
nik-rev 507b137
refactor: wording
nik-rev 2d6cdc3
chore: use `_` instead of `*`
nik-rev c9f385f
refactor: wording
nik-rev 85570c6
fix: spelling
nik-rev 842e1b7
refactor: remove redundant `length` tests
nik-rev 6163c8d
feat: clarify requirements
nik-rev e010cdc
feat(hanoi): add line breaks
nik-rev 1fc7d09
feat(hanoi): add line breaks
nik-rev 3f98387
feat(hanoi): add line breaks
nik-rev 58a7f67
feat(hanoi): more concise representation of function in readme
nik-rev 12cd75c
feat(hanoi): change the way the function is tested to use strings ins…
nik-rev 98242fb
feat(hanoi): copy over tests from solution
nik-rev 7b343a1
feat: remove redundant test
nik-rev dbd8ee1
doc(hanoi): update instructions to use the new expected method
nik-rev 86a7cfa
fix(hanoi): spelling error
nik-rev e6d5034
feat: use exclamation mark instead of period
nik-rev 70e61a4
feat: provide a more explicit example of what form the string should …
nik-rev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,37 @@ | ||
# 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. | ||
|
||
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: | ||
|
||
```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 containing the steps that have to be taken in order to get from the initial state to the final state. | ||
|
||
The function **must** return a solution in the minimum number of moves. i.e. there will be no duplicates in the solution array returned. | ||
|
||
For example, here is the expected output of `hanoi(3)`: | ||
|
||
```javascript | ||
[ | ||
"Move disc 1 from tower 1 to tower 3", | ||
"Move disc 2 from tower 1 to tower 2", | ||
"Move disc 1 from tower 3 to tower 2", | ||
"Move disc 3 from tower 1 to tower 3", | ||
"Move disc 1 from tower 2 to tower 1", | ||
"Move disc 2 from tower 2 to tower 3", | ||
"Move disc 1 from tower 1 to tower 3", | ||
] | ||
``` |
This file contains hidden or 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,6 @@ | ||
const hanoi = function() { | ||
|
||
}; | ||
|
||
// Do not edit below this line | ||
module.exports = hanoi; |
This file contains hidden or 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,52 @@ | ||
const hanoi = require("./hanoi"); | ||
|
||
describe("hanoi", () => { | ||
test("hanoi(1) should solve the puzzle in 1 move", () => { | ||
const result = hanoi(1); | ||
expect(result).toEqual(["Move disc 1 from tower 1 to tower 3"]); | ||
}); | ||
|
||
test.skip("hanoi(2) should solve the puzzle in 3 moves", () => { | ||
const result = hanoi(2); | ||
expect(result).toEqual([ | ||
"Move disc 1 from tower 1 to tower 2", | ||
"Move disc 2 from tower 1 to tower 3", | ||
"Move disc 1 from tower 2 to tower 3", | ||
]); | ||
}); | ||
|
||
test.skip("hanoi(3) should solve the puzzle in 7 moves", () => { | ||
const result = hanoi(3); | ||
expect(result).toEqual([ | ||
"Move disc 1 from tower 1 to tower 3", | ||
"Move disc 2 from tower 1 to tower 2", | ||
"Move disc 1 from tower 3 to tower 2", | ||
"Move disc 3 from tower 1 to tower 3", | ||
"Move disc 1 from tower 2 to tower 1", | ||
"Move disc 2 from tower 2 to tower 3", | ||
"Move disc 1 from tower 1 to tower 3", | ||
]); | ||
}); | ||
|
||
test.skip("hanoi(4) should solve the puzzle in 15 moves", () => { | ||
const result = hanoi(4); | ||
expect(result).toEqual([ | ||
"Move disc 1 from tower 1 to tower 2", | ||
"Move disc 2 from tower 1 to tower 3", | ||
"Move disc 1 from tower 2 to tower 3", | ||
"Move disc 3 from tower 1 to tower 2", | ||
"Move disc 1 from tower 3 to tower 1", | ||
"Move disc 2 from tower 3 to tower 2", | ||
"Move disc 1 from tower 1 to tower 2", | ||
"Move disc 4 from tower 1 to tower 3", | ||
"Move disc 1 from tower 2 to tower 3", | ||
"Move disc 2 from tower 2 to tower 1", | ||
"Move disc 1 from tower 3 to tower 1", | ||
"Move disc 3 from tower 2 to tower 3", | ||
"Move disc 1 from tower 1 to tower 2", | ||
"Move disc 2 from tower 1 to tower 3", | ||
"Move disc 1 from tower 2 to tower 3", | ||
]); | ||
}); | ||
}); | ||
|
This file contains hidden or 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,24 @@ | ||
const hanoi = function ( | ||
n, | ||
fromTower = 0, | ||
storageTower = 1, | ||
toTower = 2, | ||
steps = [], | ||
) { | ||
if (n <= 0) { | ||
return steps; | ||
} | ||
|
||
hanoi(n - 1, fromTower, toTower, storageTower, steps); | ||
|
||
steps.push( | ||
`Move disc ${n} from tower ${fromTower + 1} to tower ${toTower + 1}`, | ||
); | ||
|
||
hanoi(n - 1, storageTower, fromTower, toTower, steps); | ||
|
||
return steps; | ||
}; | ||
|
||
// Do not edit below this line | ||
module.exports = hanoi; |
nik-rev marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,51 @@ | ||
const hanoi = require("./hanoi-solution"); | ||
|
||
describe("hanoi", () => { | ||
test("hanoi(1) should solve the puzzle in 1 move", () => { | ||
const result = hanoi(1); | ||
expect(result).toEqual(["Move disc 1 from tower 1 to tower 3"]); | ||
}); | ||
|
||
test("hanoi(2) should solve the puzzle in 3 moves", () => { | ||
const result = hanoi(2); | ||
expect(result).toEqual([ | ||
"Move disc 1 from tower 1 to tower 2", | ||
"Move disc 2 from tower 1 to tower 3", | ||
"Move disc 1 from tower 2 to tower 3", | ||
]); | ||
}); | ||
|
||
test("hanoi(3) should solve the puzzle in 7 moves", () => { | ||
const result = hanoi(3); | ||
expect(result).toEqual([ | ||
"Move disc 1 from tower 1 to tower 3", | ||
"Move disc 2 from tower 1 to tower 2", | ||
"Move disc 1 from tower 3 to tower 2", | ||
"Move disc 3 from tower 1 to tower 3", | ||
"Move disc 1 from tower 2 to tower 1", | ||
"Move disc 2 from tower 2 to tower 3", | ||
"Move disc 1 from tower 1 to tower 3", | ||
]); | ||
}); | ||
|
||
test("hanoi(4) should solve the puzzle in 15 moves", () => { | ||
const result = hanoi(4); | ||
expect(result).toEqual([ | ||
"Move disc 1 from tower 1 to tower 2", | ||
"Move disc 2 from tower 1 to tower 3", | ||
"Move disc 1 from tower 2 to tower 3", | ||
"Move disc 3 from tower 1 to tower 2", | ||
"Move disc 1 from tower 3 to tower 1", | ||
"Move disc 2 from tower 3 to tower 2", | ||
"Move disc 1 from tower 1 to tower 2", | ||
"Move disc 4 from tower 1 to tower 3", | ||
"Move disc 1 from tower 2 to tower 3", | ||
"Move disc 2 from tower 2 to tower 1", | ||
"Move disc 1 from tower 3 to tower 1", | ||
"Move disc 3 from tower 2 to tower 3", | ||
"Move disc 1 from tower 1 to tower 2", | ||
"Move disc 2 from tower 1 to tower 3", | ||
"Move disc 1 from tower 2 to tower 3", | ||
]); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.