Skip to content

Commit bda4bf1

Browse files
authored
Merge pull request #447 from nik-contrib/feat/exercise_19
feat(pascal): create exercise
2 parents 496a3c3 + b08da1e commit bda4bf1

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

19_pascal/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Exercise 19 - pascal
2+
3+
The pascal's triangle is modelled as follows:
4+
- The first row is `1`.
5+
- 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`
6+
- To obtain the next row, we take each number and add it with its rightmost neighbor.
7+
8+
First row: `[1]`
9+
Second row: `[0+1, 1+0]` or simply `[1, 1]`
10+
Third row: `[0+1, 1+1, 1+0]` or simply `[1, 2, 1]`
11+
Fourth row: `[0+1, 1+2, 2+1, 1+0]` or simply `[1, 3, 3, 1]`
12+
...
13+
14+
The pattern continues forever.
15+
16+
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.
17+
18+
For example, `pascal(3)` should return `[1, 2, 1]`.

19_pascal/pascal.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const pascal = function() {
2+
3+
};
4+
5+
// Do not edit below this line
6+
module.exports = pascal;

19_pascal/pascal.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const pascal = require('./pascal');
2+
3+
describe('pascal', () => {
4+
test('Gets the first row of pascal', () => {
5+
expect(pascal(1)).toEqual([1]);
6+
});
7+
8+
test.skip('Gets the second row of pascal', () => {
9+
expect(pascal(2)).toEqual([1, 1]);
10+
});
11+
12+
test.skip('Gets the third row of pascal', () => {
13+
expect(pascal(3)).toEqual([1, 2, 1]);
14+
});
15+
16+
test.skip('Gets the fourth row of pascal', () => {
17+
expect(pascal(4)).toEqual([1, 3, 3, 1]);
18+
});
19+
20+
test.skip('Gets the fifth row of pascal', () => {
21+
expect(pascal(5)).toEqual([1, 4, 6, 4, 1]);
22+
});
23+
24+
test.skip('Gets the sixth row of pascal', () => {
25+
expect(pascal(6)).toEqual([1, 5, 10, 10, 5, 1]);
26+
});
27+
28+
test.skip('Gets the seventh row of pascal', () => {
29+
expect(pascal(7)).toEqual([1, 6, 15, 20, 15, 6, 1]);
30+
});
31+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const pascal = function (counter) {
2+
const currentLine = [1];
3+
if (counter === 1) {
4+
return currentLine;
5+
}
6+
7+
const previousLine = pascal(counter - 1);
8+
previousLine.forEach((number, i) => {
9+
const rightNeighbor = previousLine[i + 1] ?? 0;
10+
currentLine.push(number + rightNeighbor);
11+
})
12+
13+
return currentLine;
14+
}
15+
16+
// Do not edit below this line
17+
module.exports = pascal;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const pascal = require('./pascal-solution');
2+
3+
describe('pascal', () => {
4+
test('Gets the first row of pascal', () => {
5+
expect(pascal(1)).toEqual([1]);
6+
});
7+
8+
test('Gets the second row of pascal', () => {
9+
expect(pascal(2)).toEqual([1, 1]);
10+
});
11+
12+
test('Gets the third row of pascal', () => {
13+
expect(pascal(3)).toEqual([1, 2, 1]);
14+
});
15+
16+
test('Gets the fourth row of pascal', () => {
17+
expect(pascal(4)).toEqual([1, 3, 3, 1]);
18+
});
19+
20+
test('Gets the fifth row of pascal', () => {
21+
expect(pascal(5)).toEqual([1, 4, 6, 4, 1]);
22+
});
23+
24+
test('Gets the sixth row of pascal', () => {
25+
expect(pascal(6)).toEqual([1, 5, 10, 10, 5, 1]);
26+
});
27+
28+
test('Gets the seventh row of pascal', () => {
29+
expect(pascal(7)).toEqual([1, 6, 15, 20, 15, 6, 1]);
30+
});
31+
});

0 commit comments

Comments
 (0)