-
Notifications
You must be signed in to change notification settings - Fork 40.5k
feat(permutations): create exercise #444
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
17
commits into
TheOdinProject:main
Choose a base branch
from
nik-contrib:feat/exercise_16
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 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
0778f56
feat/ add new exercise
nik-rev a64b174
feat/ update readme
nik-rev cbef253
feat/ change exercise to permutations
nik-rev 10c7bc9
feat: give more information for the exercise
nik-rev 01d2ec5
feat: create tests for solution
nik-rev 1eebb5d
feat: create solution to exercise
nik-rev 9e5041a
feat(permutations): remove unnecessary utility helper function that i…
nik-rev 1e8c8f2
feat(permutations): update to inline logic within default parameter
nik-rev 1831d39
feat(permutations): updato to make code more readable by using defaul…
nik-rev ef5b1cd
feat(permutations): add test for an array containing no elements
nik-rev f8d9ee9
feat(permutations): add example to README for an empty array
nik-rev c89cb99
feat(permutations): improved variable naming
nik-rev b5d77fa
feat(permutations): clarify in README that the input integers will no…
nik-rev fc19279
feat(permutations): copy over tests from solution
nik-rev ca2eee9
feat: omit `.sort()` for permutations of length 0 and 1
nik-rev 2f0b347
feat: remove `.sort()` from test files
nik-rev 5e4f4e2
feat: simpler solution for permutations (credit @JoshDevHub)
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,11 @@ | ||
# Exercise 16 - permutations | ||
|
||
Write a function that takes in an empty array or an input array of an consecutive positive integers, starting at 1, and returns an array of all possible permutations of the original array | ||
|
||
The integers will not repeat. | ||
|
||
```javascript | ||
permutations([1, 2, 3]); // [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] | ||
// An empty set has a single permutation, 0! = 1 | ||
permutations([]); // [[]] | ||
``` |
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 permutations = function() { | ||
|
||
}; | ||
|
||
// Do not edit below this line | ||
module.exports = permutations; |
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,64 @@ | ||
const permutations = require("./permutations"); | ||
|
||
describe("permutations", () => { | ||
test("1 possible permutation for a set containing 0 numbers", () => { | ||
expect(permutations([]).sort()).toEqual([[]].sort()); | ||
}); | ||
|
||
test.skip("1 possible permutation for a set containing 1 number", () => { | ||
expect(permutations([1]).sort()).toEqual([[1]].sort()); | ||
}); | ||
|
||
test.skip("2 possible permutations for a set containing 2 numbers", () => { | ||
expect(permutations([1, 2]).sort()).toEqual( | ||
[ | ||
[1, 2], | ||
[2, 1], | ||
].sort(), | ||
); | ||
}); | ||
|
||
test.skip("6 possible permutations for a set containing 3 numbers", () => { | ||
expect(permutations([1, 2, 3]).sort()).toEqual( | ||
[ | ||
[1, 2, 3], | ||
[1, 3, 2], | ||
[2, 1, 3], | ||
[2, 3, 1], | ||
[3, 1, 2], | ||
[3, 2, 1], | ||
].sort(), | ||
); | ||
}); | ||
|
||
test.skip("24 possible permutations for a set containing 4 numbers", () => { | ||
expect(permutations([1, 2, 3, 4]).sort()).toEqual( | ||
[ | ||
[1, 2, 3, 4], | ||
[1, 2, 4, 3], | ||
[1, 3, 2, 4], | ||
[1, 3, 4, 2], | ||
[1, 4, 2, 3], | ||
[1, 4, 3, 2], | ||
[2, 1, 3, 4], | ||
[2, 1, 4, 3], | ||
[2, 3, 1, 4], | ||
[2, 3, 4, 1], | ||
[2, 4, 1, 3], | ||
[2, 4, 3, 1], | ||
[3, 1, 2, 4], | ||
[3, 1, 4, 2], | ||
[3, 2, 1, 4], | ||
[3, 2, 4, 1], | ||
[3, 4, 1, 2], | ||
[3, 4, 2, 1], | ||
[4, 1, 2, 3], | ||
[4, 1, 3, 2], | ||
[4, 2, 1, 3], | ||
[4, 2, 3, 1], | ||
[4, 3, 1, 2], | ||
[4, 3, 2, 1], | ||
].sort(), | ||
); | ||
}); | ||
}); |
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,28 @@ | ||
const permutations = function ( | ||
nik-rev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
original, | ||
partialPermutations = original.map((num) => [num]), | ||
) { | ||
if (original.length <= 1) return [original]; | ||
|
||
const newPartialPermutations = []; | ||
partialPermutations.forEach((partialPermutation) => { | ||
const missingNums = original.filter( | ||
(num) => !partialPermutation.includes(num), | ||
); | ||
missingNums.forEach((missingNum) => | ||
newPartialPermutations.push([...partialPermutation, missingNum]), | ||
); | ||
}); | ||
|
||
// We can pick any valid index because all of the elements will be the same length | ||
const ANY_INDEX = 0; | ||
|
||
if (newPartialPermutations[ANY_INDEX].length === original.length) { | ||
return newPartialPermutations; | ||
} | ||
|
||
return permutations(original, newPartialPermutations); | ||
}; | ||
|
||
// Do not edit below this line | ||
module.exports = permutations; |
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,60 @@ | ||
const permutations = require("./permutations-solution"); | ||
|
||
describe("permutations", () => { | ||
test("1 possible permutation for a set containing 0 numbers", () => { | ||
expect(permutations([]).sort()).toEqual([[]].sort()); | ||
}); | ||
test("1 possible permutation for a set containing 1 number", () => { | ||
expect(permutations([1]).sort()).toEqual([[1]].sort()); | ||
}); | ||
nik-rev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
test("2 possible permutations for a set containing 2 numbers", () => { | ||
expect(permutations([1, 2]).sort()).toEqual( | ||
[ | ||
[1, 2], | ||
[2, 1], | ||
].sort(), | ||
); | ||
}); | ||
test("6 possible permutations for a set containing 3 numbers", () => { | ||
expect(permutations([1, 2, 3]).sort()).toEqual( | ||
[ | ||
[1, 2, 3], | ||
[1, 3, 2], | ||
[2, 1, 3], | ||
[2, 3, 1], | ||
[3, 1, 2], | ||
[3, 2, 1], | ||
].sort(), | ||
); | ||
}); | ||
test("24 possible permutations for a set containing 4 numbers", () => { | ||
expect(permutations([1, 2, 3, 4]).sort()).toEqual( | ||
[ | ||
[1, 2, 3, 4], | ||
[1, 2, 4, 3], | ||
[1, 3, 2, 4], | ||
[1, 3, 4, 2], | ||
[1, 4, 2, 3], | ||
[1, 4, 3, 2], | ||
[2, 1, 3, 4], | ||
[2, 1, 4, 3], | ||
[2, 3, 1, 4], | ||
[2, 3, 4, 1], | ||
[2, 4, 1, 3], | ||
[2, 4, 3, 1], | ||
[3, 1, 2, 4], | ||
[3, 1, 4, 2], | ||
[3, 2, 1, 4], | ||
[3, 2, 4, 1], | ||
[3, 4, 1, 2], | ||
[3, 4, 2, 1], | ||
[4, 1, 2, 3], | ||
[4, 1, 3, 2], | ||
[4, 2, 1, 3], | ||
[4, 2, 3, 1], | ||
[4, 3, 1, 2], | ||
[4, 3, 2, 1], | ||
].sort(), | ||
); | ||
}); | ||
}); |
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.