diff --git a/19_pascal/README.md b/19_pascal/README.md new file mode 100644 index 00000000000..fa607c942ad --- /dev/null +++ b/19_pascal/README.md @@ -0,0 +1,18 @@ +# Exercise 19 - pascal + +The pascal's triangle is modelled as follows: +- The first row is `1`. +- Each row can be considered to have a hidden `0` to either sides of it. So the first row could also be said to be `0, 1, 0` +- To obtain the next row, we take each number and add it with its rightmost neighbor. + +First row: `[1]` +Second row: `[0+1, 1+0]` or simply `[1, 1]` +Third row: `[0+1, 1+1, 1+0]` or simply `[1, 2, 1]` +Fourth row: `[0+1, 1+2, 2+1, 1+0]` or simply `[1, 3, 3, 1]` +... + +The pattern continues forever. + +Your task is to create a *recursive* function, `pascal` - that will take an input `n` and output the `n`th pascal's row as an array of numbers. + +For example, `pascal(3)` should return `[1, 2, 1]`. diff --git a/19_pascal/pascal.js b/19_pascal/pascal.js new file mode 100644 index 00000000000..74d6a382f7a --- /dev/null +++ b/19_pascal/pascal.js @@ -0,0 +1,6 @@ +const pascal = function() { + +}; + +// Do not edit below this line +module.exports = pascal; diff --git a/19_pascal/pascal.spec.js b/19_pascal/pascal.spec.js new file mode 100644 index 00000000000..d376f56bdee --- /dev/null +++ b/19_pascal/pascal.spec.js @@ -0,0 +1,31 @@ +const pascal = require('./pascal'); + +describe('pascal', () => { + test('Gets the first row of pascal', () => { + expect(pascal(1)).toEqual([1]); + }); + + test.skip('Gets the second row of pascal', () => { + expect(pascal(2)).toEqual([1, 1]); + }); + + test.skip('Gets the third row of pascal', () => { + expect(pascal(3)).toEqual([1, 2, 1]); + }); + + test.skip('Gets the fourth row of pascal', () => { + expect(pascal(4)).toEqual([1, 3, 3, 1]); + }); + + test.skip('Gets the fifth row of pascal', () => { + expect(pascal(5)).toEqual([1, 4, 6, 4, 1]); + }); + + test.skip('Gets the sixth row of pascal', () => { + expect(pascal(6)).toEqual([1, 5, 10, 10, 5, 1]); + }); + + test.skip('Gets the seventh row of pascal', () => { + expect(pascal(7)).toEqual([1, 6, 15, 20, 15, 6, 1]); + }); +}); diff --git a/19_pascal/solution/pascal-solution.js b/19_pascal/solution/pascal-solution.js new file mode 100644 index 00000000000..b70cee732bc --- /dev/null +++ b/19_pascal/solution/pascal-solution.js @@ -0,0 +1,17 @@ +const pascal = function (counter) { + const currentLine = [1]; + if (counter === 1) { + return currentLine; + } + + const previousLine = pascal(counter - 1); + previousLine.forEach((number, i) => { + const rightNeighbor = previousLine[i + 1] ?? 0; + currentLine.push(number + rightNeighbor); + }) + + return currentLine; +} + +// Do not edit below this line +module.exports = pascal; diff --git a/19_pascal/solution/pascal-solution.spec.js b/19_pascal/solution/pascal-solution.spec.js new file mode 100644 index 00000000000..41e072ddde1 --- /dev/null +++ b/19_pascal/solution/pascal-solution.spec.js @@ -0,0 +1,31 @@ +const pascal = require('./pascal-solution'); + +describe('pascal', () => { + test('Gets the first row of pascal', () => { + expect(pascal(1)).toEqual([1]); + }); + + test('Gets the second row of pascal', () => { + expect(pascal(2)).toEqual([1, 1]); + }); + + test('Gets the third row of pascal', () => { + expect(pascal(3)).toEqual([1, 2, 1]); + }); + + test('Gets the fourth row of pascal', () => { + expect(pascal(4)).toEqual([1, 3, 3, 1]); + }); + + test('Gets the fifth row of pascal', () => { + expect(pascal(5)).toEqual([1, 4, 6, 4, 1]); + }); + + test('Gets the sixth row of pascal', () => { + expect(pascal(6)).toEqual([1, 5, 10, 10, 5, 1]); + }); + + test('Gets the seventh row of pascal', () => { + expect(pascal(7)).toEqual([1, 6, 15, 20, 15, 6, 1]); + }); +});