Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 16 additions & 11 deletions computer_science/recursion/5_pascal/solution/pascal-solution.js
Original file line number Diff line number Diff line change
@@ -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 = [];
Comment thread
damon314159 marked this conversation as resolved.

for (let i = 0; i < previousRowWithZeros.length - 1; i += 1) {
const leftParent = previousRowWithZeros[i];
const rightParent = previousRowWithZeros[i + 1];
newRow.push(leftParent + rightParent);
}
Comment thread
damon314159 marked this conversation as resolved.

return newRow;
};

// Do not edit below this line
module.exports = pascal;
Loading