diff --git a/computer_science/recursion/4_permutations/solution/permutations-solution.js b/computer_science/recursion/4_permutations/solution/permutations-solution.js index fb3f2d5cf79..73e219bfc3d 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. +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) { + // There is only one permutation of an empty array, which is the empty array + return [[]]; } - 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 diff --git a/computer_science/recursion/5_pascal/solution/pascal-solution.js b/computer_science/recursion/5_pascal/solution/pascal-solution.js index b70cee732bc..588e7d7d59d 100644 --- a/computer_science/recursion/5_pascal/solution/pascal-solution.js +++ b/computer_science/recursion/5_pascal/solution/pascal-solution.js @@ -1,17 +1,22 @@ -const pascal = function (counter) { - const currentLine = [1]; - if (counter === 1) { - return currentLine; +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 = [0, ...previousRow, 0]; - return currentLine; -} + 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 newRow; +}; // Do not edit below this line module.exports = pascal;