Skip to content

Commit ce35854

Browse files
authored
Merge pull request #444 from nik-contrib/feat/exercise_16
feat(permutations): create exercise
2 parents bda4bf1 + 5e4f4e2 commit ce35854

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

16_permutations/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Exercise 16 - permutations
2+
3+
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
4+
5+
The integers will not repeat.
6+
7+
```javascript
8+
permutations([1, 2, 3]); // [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
9+
// An empty set has a single permutation, 0! = 1
10+
permutations([]); // [[]]
11+
```

16_permutations/permutations.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const permutations = function() {
2+
3+
};
4+
5+
// Do not edit below this line
6+
module.exports = permutations;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const permutations = require("./permutations");
2+
3+
describe("permutations", () => {
4+
test("1 possible permutation for a set containing 0 numbers", () => {
5+
expect(permutations([])).toEqual([[]]);
6+
});
7+
8+
test.skip("1 possible permutation for a set containing 1 number", () => {
9+
expect(permutations([1])).toEqual([[1]]);
10+
});
11+
12+
test.skip("2 possible permutations for a set containing 2 numbers", () => {
13+
expect(permutations([1, 2]).sort()).toEqual(
14+
[
15+
[1, 2],
16+
[2, 1],
17+
].sort(),
18+
);
19+
});
20+
21+
test.skip("6 possible permutations for a set containing 3 numbers", () => {
22+
expect(permutations([1, 2, 3]).sort()).toEqual(
23+
[
24+
[1, 2, 3],
25+
[1, 3, 2],
26+
[2, 1, 3],
27+
[2, 3, 1],
28+
[3, 1, 2],
29+
[3, 2, 1],
30+
].sort(),
31+
);
32+
});
33+
34+
test.skip("24 possible permutations for a set containing 4 numbers", () => {
35+
expect(permutations([1, 2, 3, 4]).sort()).toEqual(
36+
[
37+
[1, 2, 3, 4],
38+
[1, 2, 4, 3],
39+
[1, 3, 2, 4],
40+
[1, 3, 4, 2],
41+
[1, 4, 2, 3],
42+
[1, 4, 3, 2],
43+
[2, 1, 3, 4],
44+
[2, 1, 4, 3],
45+
[2, 3, 1, 4],
46+
[2, 3, 4, 1],
47+
[2, 4, 1, 3],
48+
[2, 4, 3, 1],
49+
[3, 1, 2, 4],
50+
[3, 1, 4, 2],
51+
[3, 2, 1, 4],
52+
[3, 2, 4, 1],
53+
[3, 4, 1, 2],
54+
[3, 4, 2, 1],
55+
[4, 1, 2, 3],
56+
[4, 1, 3, 2],
57+
[4, 2, 1, 3],
58+
[4, 2, 3, 1],
59+
[4, 3, 1, 2],
60+
[4, 3, 2, 1],
61+
].sort(),
62+
);
63+
});
64+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const permutations = function (array, index = 0, results = []) {
2+
if (index == array.length) {
3+
// We have formed a valid permutation.
4+
5+
// the [...array] syntax is a way to clone the contents of the array.
6+
// because we do not want to pass a reference to the array, as that would mean
7+
// that each item in `results` will be the same item
8+
results.push([...array]);
9+
return results;
10+
}
11+
12+
for (let i = index; i < array.length; i++) {
13+
// We use "destructuring assignment" here to swap the values of array[index] and array[i]
14+
//
15+
// More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
16+
[array[index], array[i]] = [array[i], array[index]];
17+
permutations(array, index + 1, results);
18+
[array[index], array[i]] = [array[i], array[index]];
19+
}
20+
21+
return results;
22+
};
23+
24+
// Do not edit below this line
25+
module.exports = permutations;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const permutations = require("./permutations-solution");
2+
3+
describe("permutations", () => {
4+
test("1 possible permutation for a set containing 0 numbers", () => {
5+
expect(permutations([])).toEqual([[]]);
6+
});
7+
test("1 possible permutation for a set containing 1 number", () => {
8+
expect(permutations([1])).toEqual([[1]]);
9+
});
10+
test("2 possible permutations for a set containing 2 numbers", () => {
11+
expect(permutations([1, 2]).sort()).toEqual(
12+
[
13+
[1, 2],
14+
[2, 1],
15+
].sort(),
16+
);
17+
});
18+
test("6 possible permutations for a set containing 3 numbers", () => {
19+
expect(permutations([1, 2, 3]).sort()).toEqual(
20+
[
21+
[1, 2, 3],
22+
[1, 3, 2],
23+
[2, 1, 3],
24+
[2, 3, 1],
25+
[3, 1, 2],
26+
[3, 2, 1],
27+
].sort(),
28+
);
29+
});
30+
test("24 possible permutations for a set containing 4 numbers", () => {
31+
expect(permutations([1, 2, 3, 4]).sort()).toEqual(
32+
[
33+
[1, 2, 3, 4],
34+
[1, 2, 4, 3],
35+
[1, 3, 2, 4],
36+
[1, 3, 4, 2],
37+
[1, 4, 2, 3],
38+
[1, 4, 3, 2],
39+
[2, 1, 3, 4],
40+
[2, 1, 4, 3],
41+
[2, 3, 1, 4],
42+
[2, 3, 4, 1],
43+
[2, 4, 1, 3],
44+
[2, 4, 3, 1],
45+
[3, 1, 2, 4],
46+
[3, 1, 4, 2],
47+
[3, 2, 1, 4],
48+
[3, 2, 4, 1],
49+
[3, 4, 1, 2],
50+
[3, 4, 2, 1],
51+
[4, 1, 2, 3],
52+
[4, 1, 3, 2],
53+
[4, 2, 1, 3],
54+
[4, 2, 3, 1],
55+
[4, 3, 1, 2],
56+
[4, 3, 2, 1],
57+
].sort(),
58+
);
59+
});
60+
});

0 commit comments

Comments
 (0)