From 13c66f43444f165999ed3c4e31c87140a874094b Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 24 Mar 2024 16:19:42 +0000 Subject: [PATCH 01/13] feat/ generate exercise --- 19_pascal/README.md | 3 +++ 19_pascal/pascal.js | 6 ++++++ 19_pascal/pascal.spec.js | 15 +++++++++++++++ 19_pascal/solution/pascal-solution.js | 6 ++++++ 19_pascal/solution/pascal-solution.spec.js | 15 +++++++++++++++ 5 files changed, 45 insertions(+) create mode 100644 19_pascal/README.md create mode 100644 19_pascal/pascal.js create mode 100644 19_pascal/pascal.spec.js create mode 100644 19_pascal/solution/pascal-solution.js create mode 100644 19_pascal/solution/pascal-solution.spec.js diff --git a/19_pascal/README.md b/19_pascal/README.md new file mode 100644 index 00000000000..3753384515e --- /dev/null +++ b/19_pascal/README.md @@ -0,0 +1,3 @@ +# Exercise 17 - pascal + +Description of the exercise goes here. 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..f6cdd4f1ef9 --- /dev/null +++ b/19_pascal/pascal.spec.js @@ -0,0 +1,15 @@ +const pascal = require('./pascal'); + +describe('pascal', () => { + test('First test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(pascal()).toBe(''); + }); + + test.skip('Second test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(pascal()).toBe(''); + }); +}); diff --git a/19_pascal/solution/pascal-solution.js b/19_pascal/solution/pascal-solution.js new file mode 100644 index 00000000000..951989be64c --- /dev/null +++ b/19_pascal/solution/pascal-solution.js @@ -0,0 +1,6 @@ +const pascal = function() { + // Replace this comment with the solution code +}; + +// 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..419031927ff --- /dev/null +++ b/19_pascal/solution/pascal-solution.spec.js @@ -0,0 +1,15 @@ +const pascal = require('./pascal-solution'); + +describe('pascal', () => { + test('First test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(pascal()).toBe(''); + }); + + test('Second test description', () => { + // Replace this comment with any other necessary code, and update the expect line as necessary + + expect(pascal()).toBe(''); + }); +}); From fbc74a68c57f297167f2d69fee664c5dabde6b3b Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Thu, 25 Apr 2024 20:22:54 +0100 Subject: [PATCH 02/13] feat(pascal): create solution --- 19_pascal/solution/pascal-solution.js | 33 ++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/19_pascal/solution/pascal-solution.js b/19_pascal/solution/pascal-solution.js index 951989be64c..4bb70bd5435 100644 --- a/19_pascal/solution/pascal-solution.js +++ b/19_pascal/solution/pascal-solution.js @@ -1,6 +1,33 @@ -const pascal = function() { - // Replace this comment with the solution code +const pascal = function (counter, currentLine = [1]) { + if (counter === 1) return currentLine; + + const halfOfNextLine = currentLine.reduce( + (accumulator, currentElement, index, originalArray) => { + const nextElement = originalArray[index + 1] + if (currentElement <= nextElement) { + return [...accumulator, currentElement + nextElement]; + } + return accumulator; + }, + [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 we are save to remove one + if (currentLine.length % 2 === 0) { + joined.splice(joined.length / 2, 1); + } + + return pascal(counter - 1, joined); }; - + +pascal(5) + // Do not edit below this line module.exports = pascal; From 0e00b590ba500220bed25f4b094a9040d9470e4b Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Thu, 25 Apr 2024 20:23:09 +0100 Subject: [PATCH 03/13] feat(pascal): create README.md --- 19_pascal/README.md | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/19_pascal/README.md b/19_pascal/README.md index 3753384515e..935a2445fff 100644 --- a/19_pascal/README.md +++ b/19_pascal/README.md @@ -1,3 +1,18 @@ -# Exercise 17 - pascal +# Exercise 19 - pascal -Description of the exercise goes here. +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, 2, 2, 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. From 8fe75f2c6e024f24620f14d874bc3dfec2d4a9f1 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Thu, 25 Apr 2024 20:23:26 +0100 Subject: [PATCH 04/13] feat(pascal): create tests for exercise --- 19_pascal/solution/pascal-solution.spec.js | 32 ++++++++++++++++------ 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/19_pascal/solution/pascal-solution.spec.js b/19_pascal/solution/pascal-solution.spec.js index 419031927ff..41e072ddde1 100644 --- a/19_pascal/solution/pascal-solution.spec.js +++ b/19_pascal/solution/pascal-solution.spec.js @@ -1,15 +1,31 @@ const pascal = require('./pascal-solution'); describe('pascal', () => { - test('First test description', () => { - // Replace this comment with any other necessary code, and update the expect line as necessary - - expect(pascal()).toBe(''); + test('Gets the first row of pascal', () => { + expect(pascal(1)).toEqual([1]); }); - test('Second test description', () => { - // Replace this comment with any other necessary code, and update the expect line as necessary - - expect(pascal()).toBe(''); + 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]); }); }); From 8608b4035e0bb301378c69419a1ccac11968a81f Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Fri, 3 May 2024 04:50:19 +0100 Subject: [PATCH 05/13] feat(pascal): remove unnecessary call to function --- 19_pascal/solution/pascal-solution.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/19_pascal/solution/pascal-solution.js b/19_pascal/solution/pascal-solution.js index 4bb70bd5435..cbea5e35ecb 100644 --- a/19_pascal/solution/pascal-solution.js +++ b/19_pascal/solution/pascal-solution.js @@ -27,7 +27,5 @@ const pascal = function (counter, currentLine = [1]) { return pascal(counter - 1, joined); }; -pascal(5) - // Do not edit below this line module.exports = pascal; From 26f5679b38bc06d59cb394a47fb2679683033bbe Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sat, 29 Jun 2024 18:06:35 +0100 Subject: [PATCH 06/13] refactor: make code more readable Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> --- 19_pascal/solution/pascal-solution.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/19_pascal/solution/pascal-solution.js b/19_pascal/solution/pascal-solution.js index cbea5e35ecb..adbf0aaba57 100644 --- a/19_pascal/solution/pascal-solution.js +++ b/19_pascal/solution/pascal-solution.js @@ -1,5 +1,7 @@ const pascal = function (counter, currentLine = [1]) { - if (counter === 1) return currentLine; + if (counter === 1) { + return currentLine; + } const halfOfNextLine = currentLine.reduce( (accumulator, currentElement, index, originalArray) => { From c6291984208e831838e0ca8c5efa03bdb6c861c2 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sat, 29 Jun 2024 18:08:52 +0100 Subject: [PATCH 07/13] refactor: remove unnecessary line break Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> --- 19_pascal/solution/pascal-solution.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/19_pascal/solution/pascal-solution.js b/19_pascal/solution/pascal-solution.js index adbf0aaba57..7bd43e8100a 100644 --- a/19_pascal/solution/pascal-solution.js +++ b/19_pascal/solution/pascal-solution.js @@ -15,8 +15,7 @@ const pascal = function (counter, currentLine = [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 + // 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: From b5adfce302f2d9bfc717e3e8887797f93d7d3553 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 18 Aug 2024 16:27:52 +0100 Subject: [PATCH 08/13] feat(pascal): better names for variables --- 19_pascal/solution/pascal-solution.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/19_pascal/solution/pascal-solution.js b/19_pascal/solution/pascal-solution.js index 7bd43e8100a..d461f289084 100644 --- a/19_pascal/solution/pascal-solution.js +++ b/19_pascal/solution/pascal-solution.js @@ -4,17 +4,17 @@ const pascal = function (counter, currentLine = [1]) { } const halfOfNextLine = currentLine.reduce( - (accumulator, currentElement, index, originalArray) => { - const nextElement = originalArray[index + 1] - if (currentElement <= nextElement) { - return [...accumulator, currentElement + nextElement]; + (numbers, currentNumber, index) => { + const nextNumber = currentLine[index + 1]; + if (currentNumber <= nextNumber) { + return [...numbers, currentNumber + nextNumber]; } - return accumulator; + return numbers; }, [1], ); - // Notice that pascal triangle's lines are always palindromic in nature. + // 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()]; From ca43c0029d2c7a09ae0db0c41a62b53325b9d2e4 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 18 Aug 2024 16:29:00 +0100 Subject: [PATCH 09/13] feat(pascal): math mistake Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> --- 19_pascal/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/19_pascal/README.md b/19_pascal/README.md index 935a2445fff..c22aef7c2f3 100644 --- a/19_pascal/README.md +++ b/19_pascal/README.md @@ -9,7 +9,7 @@ also be said to be `0, 1, 0` 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, 2, 2, 1]` +Fourth row: `[0+1, 1+2, 2+1, 1+0]` or simply `[1, 3, 3, 1]` ... The pattern continues forever. From d64601a8cfbfeecc855c7a7185f44363e279b0e9 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Tue, 20 Aug 2024 23:38:36 +0100 Subject: [PATCH 10/13] fix(pascal): typo Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> --- 19_pascal/solution/pascal-solution.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/19_pascal/solution/pascal-solution.js b/19_pascal/solution/pascal-solution.js index d461f289084..5e43d4391ef 100644 --- a/19_pascal/solution/pascal-solution.js +++ b/19_pascal/solution/pascal-solution.js @@ -20,7 +20,7 @@ const pascal = function (counter, currentLine = [1]) { // 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 we are save to remove one + // - 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); } From ad37314636638a3ce5414a849c763163493091f0 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Tue, 20 Aug 2024 23:39:40 +0100 Subject: [PATCH 11/13] feat(pascal): copy over tests from solution --- 19_pascal/pascal.spec.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/19_pascal/pascal.spec.js b/19_pascal/pascal.spec.js index f6cdd4f1ef9..d376f56bdee 100644 --- a/19_pascal/pascal.spec.js +++ b/19_pascal/pascal.spec.js @@ -1,15 +1,31 @@ const pascal = require('./pascal'); describe('pascal', () => { - test('First test description', () => { - // Replace this comment with any other necessary code, and update the expect line as necessary + 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]); + }); - expect(pascal()).toBe(''); + test.skip('Gets the sixth row of pascal', () => { + expect(pascal(6)).toEqual([1, 5, 10, 10, 5, 1]); }); - - test.skip('Second test description', () => { - // Replace this comment with any other necessary code, and update the expect line as necessary - expect(pascal()).toBe(''); + test.skip('Gets the seventh row of pascal', () => { + expect(pascal(7)).toEqual([1, 6, 15, 20, 15, 6, 1]); }); }); From f9bbd25c4638a5bfb22c6215caa1cd2f48433217 Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 25 Aug 2024 16:59:03 +0100 Subject: [PATCH 12/13] feat: remove line break Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> --- 19_pascal/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/19_pascal/README.md b/19_pascal/README.md index c22aef7c2f3..8d161fcfd13 100644 --- a/19_pascal/README.md +++ b/19_pascal/README.md @@ -2,8 +2,7 @@ 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` +- 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]` From fc4bd94b47bf5daff94256a19ff855601cd5a26e Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+nikitarevenco@users.noreply.github.com> Date: Sun, 25 Aug 2024 16:59:18 +0100 Subject: [PATCH 13/13] feat: remove line break Co-authored-by: MaoShizhong <122839503+MaoShizhong@users.noreply.github.com> --- 19_pascal/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/19_pascal/README.md b/19_pascal/README.md index 8d161fcfd13..fa607c942ad 100644 --- a/19_pascal/README.md +++ b/19_pascal/README.md @@ -13,5 +13,6 @@ 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. +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]`.