From 1316f975156f61bf19a8997f110a768dcd56387d Mon Sep 17 00:00:00 2001 From: IcarusTheFly Date: Sat, 8 Oct 2022 02:11:19 +0200 Subject: [PATCH 1/2] Add catalan numbers algorithm --- Dynamic-Programming/CatalanNumbers.js | 30 +++++++++ .../tests/CatalanNumbers.test.js | 67 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 Dynamic-Programming/CatalanNumbers.js create mode 100644 Dynamic-Programming/tests/CatalanNumbers.test.js diff --git a/Dynamic-Programming/CatalanNumbers.js b/Dynamic-Programming/CatalanNumbers.js new file mode 100644 index 0000000000..a21a0c965a --- /dev/null +++ b/Dynamic-Programming/CatalanNumbers.js @@ -0,0 +1,30 @@ +/* + * Author: IcarusTheFly (https://github.com/IcarusTheFly) + * Catalan Numbers explanation can be found in the following links: + * Wikipedia: https://en.wikipedia.org/wiki/Catalan_number + * Brilliant: https://brilliant.org/wiki/catalan-numbers + */ + +/** + * @function catalanNumbers + * @description Returns all catalan numbers from index 0 to n + * @param {number} n + * @returns {number[]} Array with the catalan numbers from 0 to n + */ + +export const catalanNumbers = (n) => { + if (n === 0) { + return [1] + } + const catList = [1, 1] + + for (let i = 2; i <= n; i++) { + let newNumber = 0 + for (let j = 0; j < i; j++) { + newNumber += catList[j] * catList[i - j - 1] + } + catList.push(newNumber) + } + + return catList +} diff --git a/Dynamic-Programming/tests/CatalanNumbers.test.js b/Dynamic-Programming/tests/CatalanNumbers.test.js new file mode 100644 index 0000000000..6d145b1031 --- /dev/null +++ b/Dynamic-Programming/tests/CatalanNumbers.test.js @@ -0,0 +1,67 @@ +import { catalanNumbers } from '../CatalanNumbers' + +describe('Testing catalanNumbers function', () => { + it('should return the expected array for the input: 0', () => { + const input = 0 + const expectedOutput = [1] + expect(catalanNumbers(input)).toStrictEqual(expectedOutput) + }) + it('should return the expected array for the input: 1', () => { + const input = 1 + const expectedOutput = [1, 1] + expect(catalanNumbers(input)).toStrictEqual(expectedOutput) + }) + it('should return the expected array for the input: 2', () => { + const input = 2 + const expectedOutput = [1, 1, 2] + expect(catalanNumbers(input)).toStrictEqual(expectedOutput) + }) + it('should return the expected array for the input: 3', () => { + const input = 3 + const expectedOutput = [1, 1, 2, 5] + expect(catalanNumbers(input)).toStrictEqual(expectedOutput) + }) + it('should return the expected array for the input: 4', () => { + const input = 4 + const expectedOutput = [1, 1, 2, 5, 14] + expect(catalanNumbers(input)).toStrictEqual(expectedOutput) + }) + it('should return the expected array for the input: 5', () => { + const input = 5 + const expectedOutput = [1, 1, 2, 5, 14, 42] + expect(catalanNumbers(input)).toStrictEqual(expectedOutput) + }) + it('should return the expected array for the input: 6', () => { + const input = 6 + const expectedOutput = [1, 1, 2, 5, 14, 42, 132] + expect(catalanNumbers(input)).toStrictEqual(expectedOutput) + }) + it('should return the expected array for the input: 7', () => { + const input = 7 + const expectedOutput = [1, 1, 2, 5, 14, 42, 132, 429] + expect(catalanNumbers(input)).toStrictEqual(expectedOutput) + }) + it('should return the expected array for the input: 8', () => { + const input = 8 + const expectedOutput = [1, 1, 2, 5, 14, 42, 132, 429, 1430] + expect(catalanNumbers(input)).toStrictEqual(expectedOutput) + }) + it('should return the expected array for the input: 9', () => { + const input = 9 + const expectedOutput = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862] + expect(catalanNumbers(input)).toStrictEqual(expectedOutput) + }) + it('should return the expected array for the input: 10', () => { + const input = 10 + const expectedOutput = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796] + expect(catalanNumbers(input)).toStrictEqual(expectedOutput) + }) + it('should return the expected array for the input: 20', () => { + const input = 20 + const expectedOutput = [ + 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, + 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420 + ] + expect(catalanNumbers(input)).toStrictEqual(expectedOutput) + }) +}) From 1157b2d1ca8d3036c2fca9f61e04386b73d0845c Mon Sep 17 00:00:00 2001 From: IcarusTheFly Date: Sat, 8 Oct 2022 16:20:49 +0200 Subject: [PATCH 2/2] Compactify tests --- .../tests/CatalanNumbers.test.js | 63 ++----------------- 1 file changed, 5 insertions(+), 58 deletions(-) diff --git a/Dynamic-Programming/tests/CatalanNumbers.test.js b/Dynamic-Programming/tests/CatalanNumbers.test.js index 6d145b1031..be5db5af76 100644 --- a/Dynamic-Programming/tests/CatalanNumbers.test.js +++ b/Dynamic-Programming/tests/CatalanNumbers.test.js @@ -1,67 +1,14 @@ import { catalanNumbers } from '../CatalanNumbers' describe('Testing catalanNumbers function', () => { - it('should return the expected array for the input: 0', () => { - const input = 0 - const expectedOutput = [1] - expect(catalanNumbers(input)).toStrictEqual(expectedOutput) - }) - it('should return the expected array for the input: 1', () => { - const input = 1 - const expectedOutput = [1, 1] - expect(catalanNumbers(input)).toStrictEqual(expectedOutput) - }) - it('should return the expected array for the input: 2', () => { - const input = 2 - const expectedOutput = [1, 1, 2] - expect(catalanNumbers(input)).toStrictEqual(expectedOutput) - }) - it('should return the expected array for the input: 3', () => { - const input = 3 - const expectedOutput = [1, 1, 2, 5] - expect(catalanNumbers(input)).toStrictEqual(expectedOutput) - }) - it('should return the expected array for the input: 4', () => { - const input = 4 - const expectedOutput = [1, 1, 2, 5, 14] - expect(catalanNumbers(input)).toStrictEqual(expectedOutput) - }) - it('should return the expected array for the input: 5', () => { - const input = 5 - const expectedOutput = [1, 1, 2, 5, 14, 42] - expect(catalanNumbers(input)).toStrictEqual(expectedOutput) - }) - it('should return the expected array for the input: 6', () => { - const input = 6 - const expectedOutput = [1, 1, 2, 5, 14, 42, 132] - expect(catalanNumbers(input)).toStrictEqual(expectedOutput) - }) - it('should return the expected array for the input: 7', () => { - const input = 7 - const expectedOutput = [1, 1, 2, 5, 14, 42, 132, 429] - expect(catalanNumbers(input)).toStrictEqual(expectedOutput) - }) - it('should return the expected array for the input: 8', () => { - const input = 8 - const expectedOutput = [1, 1, 2, 5, 14, 42, 132, 429, 1430] - expect(catalanNumbers(input)).toStrictEqual(expectedOutput) - }) - it('should return the expected array for the input: 9', () => { - const input = 9 - const expectedOutput = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862] - expect(catalanNumbers(input)).toStrictEqual(expectedOutput) - }) - it('should return the expected array for the input: 10', () => { - const input = 10 - const expectedOutput = [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796] - expect(catalanNumbers(input)).toStrictEqual(expectedOutput) - }) - it('should return the expected array for the input: 20', () => { - const input = 20 + test('should return the expected array for inputs from 0 to 20', () => { const expectedOutput = [ 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420 ] - expect(catalanNumbers(input)).toStrictEqual(expectedOutput) + + for (let i = 0; i <= 20; i++) { + expect(catalanNumbers(i)).toStrictEqual(expectedOutput.slice(0, i + 1)) + } }) })