From 99a96f9487dada3175fc0a532e9f8aa56652078a Mon Sep 17 00:00:00 2001 From: Damon <126731021+damon314159@users.noreply.github.com> Date: Mon, 7 Sep 2026 19:00:24 +0100 Subject: [PATCH 1/3] refactor: make recursive logic more explicit for permutations --- .../solution/permutations-solution.js | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/computer_science/recursion/4_permutations/solution/permutations-solution.js b/computer_science/recursion/4_permutations/solution/permutations-solution.js index fb3f2d5cf79..2327e1e9314 100644 --- a/computer_science/recursion/4_permutations/solution/permutations-solution.js +++ b/computer_science/recursion/4_permutations/solution/permutations-solution.js @@ -1,24 +1,31 @@ -const permutations = function (array, index = 0, results = []) { - if (index == array.length) { - // We have formed a valid permutation. +// helper to insert a value into a specified position in a given array without modifying the original +const insertIntoArray = function (array, position, value) { + return [...array.slice(0, position), value, ...array.slice(position)]; +}; - // 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; +const permutations = function (array) { + if (array.length === 0) { + return [[]]; // There is only one permutation of an empty array, which is the empty array } - 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]]; + const firstElement = array[0]; + const rest = array.slice(1); + + // Calculate (recursively) each permutation of all the elements except the first + const previousPermutations = permutations(rest); + const newPermutations = []; + + // For each previousPermutations, make new permutations + // by inserting firstElement into every position + // E.g. inserting 1 into [2,3] can produce [1,2,3] and [2,1,3] and [2,3,1] + for (const permutation of previousPermutations) { + for (let i = 0; i <= permutation.length; i += 1) { + const newPermutation = insertIntoArray(permutation, i, firstElement); + newPermutations.push(newPermutation); + } } - return results; + return newPermutations; }; // Do not edit below this line From f0adc6fe51b1fbac5123af8ffdb3d6154b2642a8 Mon Sep 17 00:00:00 2001 From: Damon <126731021+damon314159@users.noreply.github.com> Date: Mon, 7 Sep 2026 19:08:46 +0100 Subject: [PATCH 2/3] refactor: make row looping logic more explicit --- .../5_pascal/solution/pascal-solution.js | 31 ++++++++++++------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/computer_science/recursion/5_pascal/solution/pascal-solution.js b/computer_science/recursion/5_pascal/solution/pascal-solution.js index b70cee732bc..7ceea3d8af6 100644 --- a/computer_science/recursion/5_pascal/solution/pascal-solution.js +++ b/computer_science/recursion/5_pascal/solution/pascal-solution.js @@ -1,17 +1,26 @@ -const pascal = function (counter) { - const currentLine = [1]; - if (counter === 1) { - return currentLine; +// helper to add extra zeros to the start and end of an array +const padZeros = function (array) { + return [0, ...array, 0]; +}; + +const pascal = function (rowNumber) { + if (rowNumber === 1) { + return [1]; } - const previousLine = pascal(counter - 1); - previousLine.forEach((number, i) => { - const rightNeighbor = previousLine[i + 1] ?? 0; - currentLine.push(number + rightNeighbor); - }) + const previousRow = pascal(rowNumber - 1); + // Add the imaginary extra zeros to the start and end, as described in the README + const previousRowWithZeros = padZeros(previousRow); + const newRow = []; + + for (let i = 0; i < previousRowWithZeros.length - 1; i += 1) { + const leftParent = previousRowWithZeros[i]; + const rightParent = previousRowWithZeros[i + 1]; + newRow.push(leftParent + rightParent); + } - return currentLine; -} + return newRow; +}; // Do not edit below this line module.exports = pascal; From 4a4f3a66c7cb4cd1e6c5c30041fee24a8979a0ad Mon Sep 17 00:00:00 2001 From: Damon <126731021+damon314159@users.noreply.github.com> Date: Tue, 8 Sep 2026 17:31:19 +0100 Subject: [PATCH 3/3] refactor: address PR feedback for consistency and clarity --- .../4_permutations/solution/permutations-solution.js | 4 ++-- .../recursion/5_pascal/solution/pascal-solution.js | 8 ++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/computer_science/recursion/4_permutations/solution/permutations-solution.js b/computer_science/recursion/4_permutations/solution/permutations-solution.js index 2327e1e9314..73e219bfc3d 100644 --- a/computer_science/recursion/4_permutations/solution/permutations-solution.js +++ b/computer_science/recursion/4_permutations/solution/permutations-solution.js @@ -1,11 +1,11 @@ -// helper to insert a value into a specified position in a given array without modifying the original const insertIntoArray = function (array, position, value) { return [...array.slice(0, position), value, ...array.slice(position)]; }; const permutations = function (array) { if (array.length === 0) { - return [[]]; // There is only one permutation of an empty array, which is the empty array + // There is only one permutation of an empty array, which is the empty array + return [[]]; } const firstElement = array[0]; diff --git a/computer_science/recursion/5_pascal/solution/pascal-solution.js b/computer_science/recursion/5_pascal/solution/pascal-solution.js index 7ceea3d8af6..588e7d7d59d 100644 --- a/computer_science/recursion/5_pascal/solution/pascal-solution.js +++ b/computer_science/recursion/5_pascal/solution/pascal-solution.js @@ -1,8 +1,3 @@ -// helper to add extra zeros to the start and end of an array -const padZeros = function (array) { - return [0, ...array, 0]; -}; - const pascal = function (rowNumber) { if (rowNumber === 1) { return [1]; @@ -10,7 +5,8 @@ const pascal = function (rowNumber) { const previousRow = pascal(rowNumber - 1); // Add the imaginary extra zeros to the start and end, as described in the README - const previousRowWithZeros = padZeros(previousRow); + const previousRowWithZeros = [0, ...previousRow, 0]; + const newRow = []; for (let i = 0; i < previousRowWithZeros.length - 1; i += 1) {