Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
7 changes: 7 additions & 0 deletions 16_permutations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Exercise 16 - permutations

Write a function that takes in an input array of an consecutive positive integers, starting at 1, and returns an array of all possible permutations of the original array

```javascript
permutations([1,2,3]); // [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
```
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;
15 changes: 15 additions & 0 deletions 16_permutations/permutations.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const permutations = require('./permutations');

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

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

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

expect(permutations()).toBe('');
});
});
19 changes: 19 additions & 0 deletions 16_permutations/solution/permutations-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const permutations = function (original, currentPermutations) {
if (original.length === 1) return original;

const perms = currentPermutations
? currentPermutations
: original.map((el) => [el]);

const newPerms = [];
perms.forEach((el) => {
const missing = original.filter((item) => !el.includes(item));
missing.forEach((itemMissing) => newPerms.push([...el, itemMissing]));
});

if (newPerms.every((el) => el.length === original.length)) return newPerms;
return permutations(original, newPerms);
};

// Do not edit below this line
module.exports = permutations;
82 changes: 82 additions & 0 deletions 16_permutations/solution/permutations-solution.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const permutations = require('./permutations-solution');

describe('permutations', () => {
function outcome(input, expected) {
const actual = permutations(input);

// Convert both arrays to strings to compare them later, excluding the order the arrays elements are in

const sterilise = (input) =>
input
.map((el) => el.toString())
.toSorted()
.toString();

return [sterilise(actual), sterilise(expected)];
}

let actual, expected;

afterEach(() => {
expect(actual).toBe(expected);
});

test('Works for array of size one', () => {
[actual, expected] = outcome([1], [1]);
});
test('Works for array of size two', () => {
[actual, expected] = outcome(
[1, 2],
[
[1, 2],
[2, 1],
],
);
});
test('Works for array of size three', () => {
[actual, expected] = outcome(
[1, 2, 3],
[
[1, 2, 3],
[1, 3, 2],
[2, 1, 3],
[2, 3, 1],
[3, 1, 2],
[3, 2, 1],
],
);
});
test('Works for array of size four', () => {
[actual, expected] = outcome(
[1, 2, 3, 4],
[
[
[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],
],
],
);
});
});