Skip to content
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
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
11 changes: 11 additions & 0 deletions 16_permutations/README.md
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([]); // [[]]
```
6 changes: 6 additions & 0 deletions 16_permutations/permutations.js
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;
64 changes: 64 additions & 0 deletions 16_permutations/permutations.spec.js
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([])).toEqual([[]]);
});

test.skip("1 possible permutation for a set containing 1 number", () => {
expect(permutations([1])).toEqual([[1]]);
});

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(),
);
});
});
28 changes: 28 additions & 0 deletions 16_permutations/solution/permutations-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const permutations = function (
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;
60 changes: 60 additions & 0 deletions 16_permutations/solution/permutations-solution.spec.js
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([])).toEqual([[]]);
});
test("1 possible permutation for a set containing 1 number", () => {
expect(permutations([1])).toEqual([[1]]);
});
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(),
);
});
});