Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pascal): create exercise #447

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
18 changes: 18 additions & 0 deletions 19_pascal/README.md
Original file line number Diff line number Diff line change
@@ -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]`.
6 changes: 6 additions & 0 deletions 19_pascal/pascal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const pascal = function() {

};

// Do not edit below this line
module.exports = pascal;
31 changes: 31 additions & 0 deletions 19_pascal/pascal.spec.js
Original file line number Diff line number Diff line change
@@ -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]);
});
});
32 changes: 32 additions & 0 deletions 19_pascal/solution/pascal-solution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const pascal = function (counter, currentLine = [1]) {
if (counter === 1) {
return currentLine;
}

const halfOfNextLine = currentLine.reduce(
(numbers, currentNumber, index) => {
const nextNumber = currentLine[index + 1];
if (currentNumber <= nextNumber) {
return [...numbers, currentNumber + nextNumber];
}
return numbers;
},
[1],
);

// Notice that pascal triangle's lines are always palindromic in nature.
// We only need the first half to obtain the information to construct the second half
const joined = [...halfOfNextLine, ...halfOfNextLine.reverse()];

// If a given line of the pascal's triangle has an even amount of elements, two things are true:
// - the next line will have an odd amount of elements
// - the two elements in the middle will always be the same. So it's safe to remove one
if (currentLine.length % 2 === 0) {
joined.splice(joined.length / 2, 1);
}

return pascal(counter - 1, joined);
};

// Do not edit below this line
module.exports = pascal;
31 changes: 31 additions & 0 deletions 19_pascal/solution/pascal-solution.spec.js
Original file line number Diff line number Diff line change
@@ -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]);
});
});