From 0778f56b20dedc94c7409ee1b97191d4c92817ec Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sat, 23 Mar 2024 14:59:10 +0000 Subject: [PATCH 01/17] feat/ add new exercise --- 16_sumSquares/README.md | 3 +++ 16_sumSquares/solution/sumSquares-solution.js | 6 ++++++ .../solution/sumSquares-solution.spec.js | 15 +++++++++++++++ 16_sumSquares/sumSquares.js | 6 ++++++ 16_sumSquares/sumSquares.spec.js | 15 +++++++++++++++ 5 files changed, 45 insertions(+) create mode 100644 16_sumSquares/README.md create mode 100644 16_sumSquares/solution/sumSquares-solution.js create mode 100644 16_sumSquares/solution/sumSquares-solution.spec.js create mode 100644 16_sumSquares/sumSquares.js create mode 100644 16_sumSquares/sumSquares.spec.js diff --git a/16_sumSquares/README.md b/16_sumSquares/README.md new file mode 100644 index 00000000000..357e675bddc --- /dev/null +++ b/16_sumSquares/README.md @@ -0,0 +1,3 @@ +# Exercise 13 - sumSquares + +Description of the exercise goes here. diff --git a/16_sumSquares/solution/sumSquares-solution.js b/16_sumSquares/solution/sumSquares-solution.js new file mode 100644 index 00000000000..5315a6a7255 --- /dev/null +++ b/16_sumSquares/solution/sumSquares-solution.js @@ -0,0 +1,6 @@ +const sumSquares = function() { + // Replace this comment with the solution code +}; + +// Do not edit below this line +module.exports = sumSquares; diff --git a/16_sumSquares/solution/sumSquares-solution.spec.js b/16_sumSquares/solution/sumSquares-solution.spec.js new file mode 100644 index 00000000000..5252ed049ad --- /dev/null +++ b/16_sumSquares/solution/sumSquares-solution.spec.js @@ -0,0 +1,15 @@ +const sumSquares = require('./sumSquares-solution'); + +describe('sumSquares', () => { + test('First test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(sumSquares()).toBe(''); + }); + + test('Second test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(sumSquares()).toBe(''); + }); +}); diff --git a/16_sumSquares/sumSquares.js b/16_sumSquares/sumSquares.js new file mode 100644 index 00000000000..6eb00e98cf4 --- /dev/null +++ b/16_sumSquares/sumSquares.js @@ -0,0 +1,6 @@ +const sumSquares = function() { + +}; + +// Do not edit below this line +module.exports = sumSquares; diff --git a/16_sumSquares/sumSquares.spec.js b/16_sumSquares/sumSquares.spec.js new file mode 100644 index 00000000000..9af9cc8104f --- /dev/null +++ b/16_sumSquares/sumSquares.spec.js @@ -0,0 +1,15 @@ +const sumSquares = require('./sumSquares'); + +describe('sumSquares', () => { + test('First test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(sumSquares()).toBe(''); + }); + + test.skip('Second test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(sumSquares()).toBe(''); + }); +}); From a64b174e308e92c80796706f9e57305cb703cd70 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sat, 23 Mar 2024 16:34:14 +0000 Subject: [PATCH 02/17] feat/ update readme --- 16_sumSquares/README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/16_sumSquares/README.md b/16_sumSquares/README.md index 357e675bddc..1da3e429e57 100644 --- a/16_sumSquares/README.md +++ b/16_sumSquares/README.md @@ -1,3 +1,10 @@ -# Exercise 13 - sumSquares +# Exercise 16 - sumSquares -Description of the exercise goes here. +Write a function that sums squares of numbers in an arbitrarily nested array + +```javascript +SumSquares([1,2,3]); // 1 + 4 + 9 = 14 +SumSquares([[1,2],3]); // 1 + 4 + 9 = 14 +SumSquares([[[[[[[[[1]]]]]]]]]); // 1 = 1 +SumSquares([10,[[10],10],[10]]); // 100 + 100 + 100 + 100 = 400 +``` From cbef253cd6b644b0957c9d2b06ad0f55c6a2e473 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 24 Mar 2024 15:03:40 +0000 Subject: [PATCH 03/17] feat/ change exercise to permutations --- 16_permutations/README.md | 7 +++++++ 16_permutations/permutations.js | 6 ++++++ .../permutations.spec.js | 8 ++++---- .../solution/permutations-solution.js | 4 ++-- .../solution/permutations-solution.spec.js | 8 ++++---- 16_sumSquares/README.md | 10 ---------- 16_sumSquares/sumSquares.js | 6 ------ 7 files changed, 23 insertions(+), 26 deletions(-) create mode 100644 16_permutations/README.md create mode 100644 16_permutations/permutations.js rename 16_sumSquares/sumSquares.spec.js => 16_permutations/permutations.spec.js (66%) rename 16_sumSquares/solution/sumSquares-solution.js => 16_permutations/solution/permutations-solution.js (56%) rename 16_sumSquares/solution/sumSquares-solution.spec.js => 16_permutations/solution/permutations-solution.spec.js (64%) delete mode 100644 16_sumSquares/README.md delete mode 100644 16_sumSquares/sumSquares.js diff --git a/16_permutations/README.md b/16_permutations/README.md new file mode 100644 index 00000000000..90209dd2f10 --- /dev/null +++ b/16_permutations/README.md @@ -0,0 +1,7 @@ +# Exercise 16 - permutations + +Write a function that takes in an input array and returns an array of all possible permutations of the array + +```javascript +permutations([1,2,3]); // [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] +``` diff --git a/16_permutations/permutations.js b/16_permutations/permutations.js new file mode 100644 index 00000000000..c7380ec3640 --- /dev/null +++ b/16_permutations/permutations.js @@ -0,0 +1,6 @@ +const permutations = function() { + +}; + +// Do not edit below this line +module.exports = permutations; diff --git a/16_sumSquares/sumSquares.spec.js b/16_permutations/permutations.spec.js similarity index 66% rename from 16_sumSquares/sumSquares.spec.js rename to 16_permutations/permutations.spec.js index 9af9cc8104f..57664c4c16b 100644 --- a/16_sumSquares/sumSquares.spec.js +++ b/16_permutations/permutations.spec.js @@ -1,15 +1,15 @@ -const sumSquares = require('./sumSquares'); +const permutations = require('./permutations'); -describe('sumSquares', () => { +describe('permutations', () => { test('First test description', () => { // Replace this comment with any other necessary code, and update the expect line as necessary - expect(sumSquares()).toBe(''); + expect(permutations()).toBe(''); }); test.skip('Second test description', () => { // Replace this comment with any other necessary code, and update the expect line as necessary - expect(sumSquares()).toBe(''); + expect(permutations()).toBe(''); }); }); diff --git a/16_sumSquares/solution/sumSquares-solution.js b/16_permutations/solution/permutations-solution.js similarity index 56% rename from 16_sumSquares/solution/sumSquares-solution.js rename to 16_permutations/solution/permutations-solution.js index 5315a6a7255..87cbeac657b 100644 --- a/16_sumSquares/solution/sumSquares-solution.js +++ b/16_permutations/solution/permutations-solution.js @@ -1,6 +1,6 @@ -const sumSquares = function() { +const permutations = function() { // Replace this comment with the solution code }; // Do not edit below this line -module.exports = sumSquares; +module.exports = permutations; diff --git a/16_sumSquares/solution/sumSquares-solution.spec.js b/16_permutations/solution/permutations-solution.spec.js similarity index 64% rename from 16_sumSquares/solution/sumSquares-solution.spec.js rename to 16_permutations/solution/permutations-solution.spec.js index 5252ed049ad..888305ea1cc 100644 --- a/16_sumSquares/solution/sumSquares-solution.spec.js +++ b/16_permutations/solution/permutations-solution.spec.js @@ -1,15 +1,15 @@ -const sumSquares = require('./sumSquares-solution'); +const permutations = require('./permutations-solution'); -describe('sumSquares', () => { +describe('permutations', () => { test('First test description', () => { // Replace this comment with any other necessary code, and update the expect line as necessary - expect(sumSquares()).toBe(''); + expect(permutations()).toBe(''); }); test('Second test description', () => { // Replace this comment with any other necessary code, and update the expect line as necessary - expect(sumSquares()).toBe(''); + expect(permutations()).toBe(''); }); }); diff --git a/16_sumSquares/README.md b/16_sumSquares/README.md deleted file mode 100644 index 1da3e429e57..00000000000 --- a/16_sumSquares/README.md +++ /dev/null @@ -1,10 +0,0 @@ -# Exercise 16 - sumSquares - -Write a function that sums squares of numbers in an arbitrarily nested array - -```javascript -SumSquares([1,2,3]); // 1 + 4 + 9 = 14 -SumSquares([[1,2],3]); // 1 + 4 + 9 = 14 -SumSquares([[[[[[[[[1]]]]]]]]]); // 1 = 1 -SumSquares([10,[[10],10],[10]]); // 100 + 100 + 100 + 100 = 400 -``` diff --git a/16_sumSquares/sumSquares.js b/16_sumSquares/sumSquares.js deleted file mode 100644 index 6eb00e98cf4..00000000000 --- a/16_sumSquares/sumSquares.js +++ /dev/null @@ -1,6 +0,0 @@ -const sumSquares = function() { - -}; - -// Do not edit below this line -module.exports = sumSquares; From 10c7bc971f60516f12f8767ed9a69f68a697bff2 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:06:47 +0100 Subject: [PATCH 04/17] feat: give more information for the exercise --- 16_permutations/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/16_permutations/README.md b/16_permutations/README.md index 90209dd2f10..fa7116a5c28 100644 --- a/16_permutations/README.md +++ b/16_permutations/README.md @@ -1,6 +1,6 @@ # Exercise 16 - permutations -Write a function that takes in an input array and returns an array of all possible permutations of the array +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]] From 01d2ec5699f89179aef4a67a37a39413446fd10d Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:07:01 +0100 Subject: [PATCH 05/17] feat: create tests for solution --- .../solution/permutations-solution.spec.js | 81 +++++++++++++++++-- 1 file changed, 74 insertions(+), 7 deletions(-) diff --git a/16_permutations/solution/permutations-solution.spec.js b/16_permutations/solution/permutations-solution.spec.js index 888305ea1cc..949bd1b3a84 100644 --- a/16_permutations/solution/permutations-solution.spec.js +++ b/16_permutations/solution/permutations-solution.spec.js @@ -1,15 +1,82 @@ const permutations = require('./permutations-solution'); describe('permutations', () => { - test('First test description', () => { - // Replace this comment with any other necessary code, and update the expect line as necessary + function outcome(input, expected) { + const actual = permutations(input); - expect(permutations()).toBe(''); + // 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('Second test description', () => { - // Replace this comment with any other necessary code, and update the expect line as necessary - expect(permutations()).toBe(''); + 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], + ], + ], + ); }); }); From 1eebb5d6d6cd2ab8978f7be6ae4e299d7ba72d1c Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Thu, 4 Apr 2024 18:07:11 +0100 Subject: [PATCH 06/17] feat: create solution to exercise --- .../solution/permutations-solution.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/16_permutations/solution/permutations-solution.js b/16_permutations/solution/permutations-solution.js index 87cbeac657b..6db6a0a6c2a 100644 --- a/16_permutations/solution/permutations-solution.js +++ b/16_permutations/solution/permutations-solution.js @@ -1,6 +1,19 @@ -const permutations = function() { - // Replace this comment with the solution code +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; From 9e5041a7bdd8f386747baad27e587e6f67e388ab Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 18 Aug 2024 15:59:31 +0100 Subject: [PATCH 07/17] feat(permutations): remove unnecessary utility helper function that is replaceable with toEqual --- .../solution/permutations-solution.spec.js | 99 +++++++------------ 1 file changed, 37 insertions(+), 62 deletions(-) diff --git a/16_permutations/solution/permutations-solution.spec.js b/16_permutations/solution/permutations-solution.spec.js index 949bd1b3a84..5f9507789b4 100644 --- a/16_permutations/solution/permutations-solution.spec.js +++ b/16_permutations/solution/permutations-solution.spec.js @@ -1,41 +1,19 @@ -const permutations = require('./permutations-solution'); +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]); +describe("permutations", () => { + test("Works for array of size one", () => { + expect(permutations([1]).sort()).toEqual([1].sort()); }); - test('Works for array of size two', () => { - [actual, expected] = outcome( - [1, 2], + test("Works for array of size two", () => { + expect(permutations([1, 2]).sort()).toEqual( [ [1, 2], [2, 1], - ], + ].sort(), ); }); - test('Works for array of size three', () => { - [actual, expected] = outcome( - [1, 2, 3], + test("Works for array of size three", () => { + expect(permutations([1, 2, 3]).sort()).toEqual( [ [1, 2, 3], [1, 3, 2], @@ -43,40 +21,37 @@ describe('permutations', () => { [2, 3, 1], [3, 1, 2], [3, 2, 1], - ], + ].sort(), ); }); - test('Works for array of size four', () => { - [actual, expected] = outcome( - [1, 2, 3, 4], + test("Works for array of size four", () => { + 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], - ], - ], + [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(), ); }); }); From 1e8c8f2e01c99123074b961f7e539f39b997414e Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 18 Aug 2024 16:02:41 +0100 Subject: [PATCH 08/17] feat(permutations): update to inline logic within default parameter Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> --- 16_permutations/solution/permutations-solution.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/16_permutations/solution/permutations-solution.js b/16_permutations/solution/permutations-solution.js index 6db6a0a6c2a..b62318923a8 100644 --- a/16_permutations/solution/permutations-solution.js +++ b/16_permutations/solution/permutations-solution.js @@ -1,9 +1,6 @@ -const permutations = function (original, currentPermutations) { - if (original.length === 1) return original; - - const perms = currentPermutations - ? currentPermutations - : original.map((el) => [el]); +const permutations = function (original, currentPermutations = original.map((num) => [num])) { + if (original.length === 0) return []; + if (original.length === 1) return [original]; const newPerms = []; perms.forEach((el) => { From 1831d3964708a9e1b7ae5c4b73c8badfc569e21f Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 18 Aug 2024 16:09:10 +0100 Subject: [PATCH 09/17] feat(permutations): updato to make code more readable by using default parameter --- 16_permutations/solution/permutations-solution.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/16_permutations/solution/permutations-solution.js b/16_permutations/solution/permutations-solution.js index b62318923a8..47be3d84e40 100644 --- a/16_permutations/solution/permutations-solution.js +++ b/16_permutations/solution/permutations-solution.js @@ -1,9 +1,11 @@ -const permutations = function (original, currentPermutations = original.map((num) => [num])) { - if (original.length === 0) return []; - if (original.length === 1) return [original]; +const permutations = function ( + original, + currentPermutations = original.map((num) => [num]), +) { + if (original.length < 2) return [original]; const newPerms = []; - perms.forEach((el) => { + currentPermutations.forEach((el) => { const missing = original.filter((item) => !el.includes(item)); missing.forEach((itemMissing) => newPerms.push([...el, itemMissing])); }); From ef5b1cdba53e723ad1248dc68505db174b0b6553 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 18 Aug 2024 16:09:24 +0100 Subject: [PATCH 10/17] feat(permutations): add test for an array containing no elements --- .../solution/permutations-solution.spec.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/16_permutations/solution/permutations-solution.spec.js b/16_permutations/solution/permutations-solution.spec.js index 5f9507789b4..25899e27bf6 100644 --- a/16_permutations/solution/permutations-solution.spec.js +++ b/16_permutations/solution/permutations-solution.spec.js @@ -1,10 +1,13 @@ const permutations = require("./permutations-solution"); describe("permutations", () => { - test("Works for array of size one", () => { - expect(permutations([1]).sort()).toEqual([1].sort()); + test("1 possible permutation for a set containing 0 numbers", () => { + expect(permutations([]).sort()).toEqual([[]].sort()); }); - test("Works for array of size two", () => { + test("1 possible permutation for a set containing 1 number", () => { + expect(permutations([1]).sort()).toEqual([[1]].sort()); + }); + test("2 possible permutations for a set containing 2 numbers", () => { expect(permutations([1, 2]).sort()).toEqual( [ [1, 2], @@ -12,7 +15,7 @@ describe("permutations", () => { ].sort(), ); }); - test("Works for array of size three", () => { + test("6 possible permutations for a set containing 3 numbers", () => { expect(permutations([1, 2, 3]).sort()).toEqual( [ [1, 2, 3], @@ -24,7 +27,7 @@ describe("permutations", () => { ].sort(), ); }); - test("Works for array of size four", () => { + test("24 possible permutations for a set containing 4 numbers", () => { expect(permutations([1, 2, 3, 4]).sort()).toEqual( [ [1, 2, 3, 4], From f8d9ee982e025ac7134e7881d1a08a114790ebe9 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 18 Aug 2024 16:12:06 +0100 Subject: [PATCH 11/17] feat(permutations): add example to README for an empty array --- 16_permutations/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/16_permutations/README.md b/16_permutations/README.md index fa7116a5c28..9542e4f471f 100644 --- a/16_permutations/README.md +++ b/16_permutations/README.md @@ -1,7 +1,8 @@ # 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 - +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 ```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([]); // [[]] ``` From c89cb991d001ebbf1e0821178d30d9341a99b5ea Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 18 Aug 2024 16:18:28 +0100 Subject: [PATCH 12/17] feat(permutations): improved variable naming --- .../solution/permutations-solution.js | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/16_permutations/solution/permutations-solution.js b/16_permutations/solution/permutations-solution.js index 47be3d84e40..3291fee101e 100644 --- a/16_permutations/solution/permutations-solution.js +++ b/16_permutations/solution/permutations-solution.js @@ -1,17 +1,27 @@ const permutations = function ( original, - currentPermutations = original.map((num) => [num]), + partialPermutations = original.map((num) => [num]), ) { - if (original.length < 2) return [original]; + if (original.length <= 1) return [original]; - const newPerms = []; - currentPermutations.forEach((el) => { - const missing = original.filter((item) => !el.includes(item)); - missing.forEach((itemMissing) => newPerms.push([...el, itemMissing])); + const newPartialPermutations = []; + partialPermutations.forEach((partialPermutation) => { + const missingNums = original.filter( + (num) => !partialPermutation.includes(num), + ); + missingNums.forEach((missingNum) => + newPartialPermutations.push([...partialPermutation, missingNum]), + ); }); - if (newPerms.every((el) => el.length === original.length)) return newPerms; - return permutations(original, newPerms); + // 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 From b5d77fac88f22054c490c9f02c0a17f8ea227700 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 18 Aug 2024 16:19:38 +0100 Subject: [PATCH 13/17] feat(permutations): clarify in README that the input integers will not repeat --- 16_permutations/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/16_permutations/README.md b/16_permutations/README.md index 9542e4f471f..de5569ccbc2 100644 --- a/16_permutations/README.md +++ b/16_permutations/README.md @@ -1,8 +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]] +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([]); // [[]] ``` From fc1927959685ddb57f3bcf13da60ab19d77f709c Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Tue, 20 Aug 2024 23:38:10 +0100 Subject: [PATCH 14/17] feat(permutations): copy over tests from solution --- 16_permutations/permutations.spec.js | 67 ++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/16_permutations/permutations.spec.js b/16_permutations/permutations.spec.js index 57664c4c16b..bf720e978f3 100644 --- a/16_permutations/permutations.spec.js +++ b/16_permutations/permutations.spec.js @@ -1,15 +1,64 @@ -const permutations = require('./permutations'); +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 +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(), + ); + }); - expect(permutations()).toBe(''); + 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('Second test description', () => { - // Replace this comment with any other necessary code, and update the expect line as necessary - expect(permutations()).toBe(''); + 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(), + ); }); }); From ca2eee979eefe4e53e2ee966d6d1cd50af2b1dfd Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 25 Aug 2024 16:57:22 +0100 Subject: [PATCH 15/17] feat: omit `.sort()` for permutations of length 0 and 1 Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> --- 16_permutations/permutations.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/16_permutations/permutations.spec.js b/16_permutations/permutations.spec.js index bf720e978f3..69ee0a1ae9b 100644 --- a/16_permutations/permutations.spec.js +++ b/16_permutations/permutations.spec.js @@ -2,11 +2,11 @@ const permutations = require("./permutations"); describe("permutations", () => { test("1 possible permutation for a set containing 0 numbers", () => { - expect(permutations([]).sort()).toEqual([[]].sort()); + expect(permutations([])).toEqual([[]]); }); test.skip("1 possible permutation for a set containing 1 number", () => { - expect(permutations([1]).sort()).toEqual([[1]].sort()); + expect(permutations([1])).toEqual([[1]]); }); test.skip("2 possible permutations for a set containing 2 numbers", () => { From 2f0b347f9d9bf9649237ffb515f9473319de1e9f Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 25 Aug 2024 16:58:19 +0100 Subject: [PATCH 16/17] feat: remove `.sort()` from test files --- 16_permutations/solution/permutations-solution.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/16_permutations/solution/permutations-solution.spec.js b/16_permutations/solution/permutations-solution.spec.js index 25899e27bf6..2f3a393f495 100644 --- a/16_permutations/solution/permutations-solution.spec.js +++ b/16_permutations/solution/permutations-solution.spec.js @@ -2,10 +2,10 @@ const permutations = require("./permutations-solution"); describe("permutations", () => { test("1 possible permutation for a set containing 0 numbers", () => { - expect(permutations([]).sort()).toEqual([[]].sort()); + expect(permutations([])).toEqual([[]]); }); test("1 possible permutation for a set containing 1 number", () => { - expect(permutations([1]).sort()).toEqual([[1]].sort()); + expect(permutations([1])).toEqual([[1]]); }); test("2 possible permutations for a set containing 2 numbers", () => { expect(permutations([1, 2]).sort()).toEqual( From 5e4f4e2fc8dc4510fc56db8c03348b9537e71e3a Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Sun, 16 Feb 2025 18:13:25 +0000 Subject: [PATCH 17/17] feat: simpler solution for permutations (credit @JoshDevHub) --- .../solution/permutations-solution.js | 37 +++++++++---------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/16_permutations/solution/permutations-solution.js b/16_permutations/solution/permutations-solution.js index 3291fee101e..fb3f2d5cf79 100644 --- a/16_permutations/solution/permutations-solution.js +++ b/16_permutations/solution/permutations-solution.js @@ -1,27 +1,24 @@ -const permutations = function ( - original, - partialPermutations = original.map((num) => [num]), -) { - if (original.length <= 1) return [original]; +const permutations = function (array, index = 0, results = []) { + if (index == array.length) { + // We have formed a valid permutation. - 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; + // the [...array] syntax is a way to clone the contents of the array. + // because we do not want to pass a reference to the array, as that would mean + // that each item in `results` will be the same item + results.push([...array]); + return results; + } - if (newPartialPermutations[ANY_INDEX].length === original.length) { - return newPartialPermutations; + for (let i = index; i < array.length; i++) { + // We use "destructuring assignment" here to swap the values of array[index] and array[i] + // + // More info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment + [array[index], array[i]] = [array[i], array[index]]; + permutations(array, index + 1, results); + [array[index], array[i]] = [array[i], array[index]]; } - return permutations(original, newPartialPermutations); + return results; }; // Do not edit below this line