From d64803057fe74dd6756024d789ae03104cbec10d Mon Sep 17 00:00:00 2001 From: alex_p Date: Sat, 29 Oct 2022 13:50:09 +0300 Subject: [PATCH 1/5] algorithm: add UniquePaths2 algorithm This algorithm allows to count ways to reach right-bottom corner on the grid with obtacles --- Dynamic-Programming/UniquePaths2.js | 80 +++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 Dynamic-Programming/UniquePaths2.js diff --git a/Dynamic-Programming/UniquePaths2.js b/Dynamic-Programming/UniquePaths2.js new file mode 100644 index 0000000000..7fc5ef3f36 --- /dev/null +++ b/Dynamic-Programming/UniquePaths2.js @@ -0,0 +1,80 @@ +/* + * Unique Paths 2 + * + * There is a robot on an `m x n` grid. + * The robot is initially located at the top-left corner + * The robot tries to move to the bottom-right corner. + * The robot can only move either down or right at any point in time. + * + * Given grid with obstacles + * An obstacle and space are marked as 1 or 0 respectively in grid. + * A path that the robot takes cannot include any square that is an obstacle. + * Return the number of possible unique paths that the robot can take to reach the bottom-right corner. + * + * More info: https://leetcode.com/problems/unique-paths-ii/ + */ + +/** + * @description Return 'rows x columns' grid with cells filled by 'filler' + * @param {Number} rows Number of rows in the grid + * @param {Number} columns Number of columns in the grid + * @param {String | Number | Boolean} filler The value to fill cells + * @returns {Object []} + */ +const generateMatrix = (rows, columns, filler = 0) => { + const matrix = [] + for (let i = 0; i < rows; i += 1) { + const submatrix = [] + for (let k = 0; k < columns; k += 1) { + submatrix[k] = filler + } + matrix[i] = submatrix + } + return matrix +} + +/** + * @description Return number of unique paths + * @param {Object []} obstacles Obstacles grid + * @returns {Number} + */ +const uniquePaths2 = (obstacles) => { + if (!(obstacles instanceof Object)) { + throw new Error('Input data must be type of Array') + } + // Create grid for calculating number of unique ways + const rows = obstacles.length + const columns = obstacles[0].length + const grid = generateMatrix(rows, columns) + // Fill the outermost cell with 1 b/c it has + // the only way to reach neighbor + for (let i = 0; i < rows; i += 1) { + // If robot encounters an obstacle in these cells, + // he cannot continue movind in that direction + if (obstacles[i][0]) { + break + } + grid[i][0] = 1 + } + for (let j = 0; j < columns; j += 1) { + if (obstacles[0][j]) { + break + } + grid[0][j] = 1 + } + // Fill the rest of grid by dynamic programming + // using following reccurent formula: + // K[i][j] = K[i - 1][j] + K[i][j - 1] + for (let i = 1; i < rows; i += 1) { + for (let j = 1; j < columns; j += 1) { + if (obstacles[i][j]) { + grid[i][j] = 0 + } else { + grid[i][j] = grid[i - 1][j] + grid[i][j - 1] + } + } + } + return grid[rows - 1][columns - 1] +} + +export { uniquePaths2 } From 10052072b3b05ed6f36a55248ff4a848102a7472 Mon Sep 17 00:00:00 2001 From: alex_p Date: Sat, 29 Oct 2022 13:53:42 +0300 Subject: [PATCH 2/5] test: add test for UniquePaths2.js --- .../tests/UniquePaths2.test.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Dynamic-Programming/tests/UniquePaths2.test.js diff --git a/Dynamic-Programming/tests/UniquePaths2.test.js b/Dynamic-Programming/tests/UniquePaths2.test.js new file mode 100644 index 0000000000..c35b1facee --- /dev/null +++ b/Dynamic-Programming/tests/UniquePaths2.test.js @@ -0,0 +1,19 @@ +import { uniquePaths2 } from '../UniquePaths2' + +describe('Unique Paths2', () => { + // Should return number of ways, taken into account the obstacles + test('Case 1: there are obstacles in the way', () => { + expect(uniquePaths2([[0, 0, 0], [0, 1, 0], [0, 0, 0]])).toEqual(2) + expect(uniquePaths2([[0, 0, 0], [0, 1, 0], [0, 0, 0], [1, 0, 0]])).toEqual(3) + }) + // Should return number of all possible ways to reach right-bottom corner + test('Case 2: there are no obstacles in the way', () => { + expect(uniquePaths2([[0, 0, 0], [0, 0, 0], [0, 0, 0]])).toEqual(6) + expect(uniquePaths2([[0, 0, 0], [0, 0, 0]])).toEqual(3) + }) + // Should throw an exception b/c input data has wrong type + test('Case 3: there are wrong type of input data', () => { + expect(() => uniquePaths2('wrong input')).toThrow() + expect(() => uniquePaths2(100)).toThrow() + }) +}) From b888548807173de85e9c3507a62d9447fa751927 Mon Sep 17 00:00:00 2001 From: alex_p Date: Sat, 29 Oct 2022 16:02:35 +0300 Subject: [PATCH 3/5] algorithm: add HornerScheme algorithm This algorithm allows to convert number from decimal number system to another one (from 2 to 10) --- Maths/HornerScheme.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Maths/HornerScheme.js diff --git a/Maths/HornerScheme.js b/Maths/HornerScheme.js new file mode 100644 index 0000000000..bc02523a4b --- /dev/null +++ b/Maths/HornerScheme.js @@ -0,0 +1,38 @@ +/** + * @function hornerScheme + * @description Convert a number from decimal system to another (till decimal) + * @param {Number} number Number to be converted + * @param {Number} base Base of new number system + * @returns {String} Converted Number + * @see [HornerMethod](https://en.wikipedia.org/wiki/Horner%27s_method) + * @example + * const num1 = 125 // Needs to be converted to the binary number system + * gornerScheme(num, 2); // ===> 1111101 + * @example + * const num2 = 125 // Needs to be converted to the octal number system + * gornerScheme(num, 8); // ===> 175 + */ +const hornerScheme = (number, base) => { + if (typeof number !== 'number' || typeof base !== 'number') { + throw new Error('Input data must be numbers') + } + // Zero in any number system is zero + if (number === 0) { + return `${number}` + } + let absoluteValue = Math.abs(number) + let convertedNumber = '' + while (absoluteValue > 0) { + // Every iteration last digit is taken away + // and added to the previois one + const lastDigit = absoluteValue % base + convertedNumber = lastDigit + convertedNumber + absoluteValue = Math.trunc(absoluteValue / base) + } + // Result is whether negative or positive, + // depending on the original value + const result = number < 0 ? `-${convertedNumber}` : convertedNumber + return result +} + +export { hornerScheme } From d4e1ec9f12d9335b1701ce72aebed92115e1a9e5 Mon Sep 17 00:00:00 2001 From: alex_p Date: Sat, 29 Oct 2022 16:05:56 +0300 Subject: [PATCH 4/5] test: add test for Maths/HornerScheme.js --- Maths/test/HornerScheme.test.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Maths/test/HornerScheme.test.js diff --git a/Maths/test/HornerScheme.test.js b/Maths/test/HornerScheme.test.js new file mode 100644 index 0000000000..d798b90bc0 --- /dev/null +++ b/Maths/test/HornerScheme.test.js @@ -0,0 +1,25 @@ +import { hornerScheme } from '../HornerScheme' + +describe('Horner Scheme', () => { + test('Case 1: testing convertation to the binary system', () => { + expect(hornerScheme(210, 2)).toEqual('11010010') + expect(hornerScheme(-210, 2)).toEqual('-11010010') + }) + test('Case 2: testing convertation to the system with base 5', () => { + expect(hornerScheme(210, 5)).toEqual('1320') + expect(hornerScheme(-210, 5)).toEqual('-1320') + }) + test('Case 3: testing convertation to the octal system', () => { + expect(hornerScheme(210, 8)).toEqual('322') + expect(hornerScheme(-210, 8)).toEqual('-322') + }) + test('Case 4: testing 0', () => { + expect(hornerScheme(0, 8)).toEqual('0') + expect(hornerScheme(0, 8)).toEqual('0') + }) + test('Case 5: testing throwing an exception', () => { + expect(() => hornerScheme('string', 2)).toThrow() + expect(() => hornerScheme(10, 'base')).toThrow() + expect(() => hornerScheme(true, false)).toThrow() + }) +}) From be56ea70e40a9d4d9ba65472dd09308a995574e7 Mon Sep 17 00:00:00 2001 From: alex_p Date: Sat, 29 Oct 2022 16:17:52 +0300 Subject: [PATCH 5/5] fix: fixed spelling errors in Horner Scheme tests --- Maths/test/HornerScheme.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Maths/test/HornerScheme.test.js b/Maths/test/HornerScheme.test.js index d798b90bc0..5922825048 100644 --- a/Maths/test/HornerScheme.test.js +++ b/Maths/test/HornerScheme.test.js @@ -1,15 +1,15 @@ import { hornerScheme } from '../HornerScheme' describe('Horner Scheme', () => { - test('Case 1: testing convertation to the binary system', () => { + test('Case 1: testing conversion to the binary system', () => { expect(hornerScheme(210, 2)).toEqual('11010010') expect(hornerScheme(-210, 2)).toEqual('-11010010') }) - test('Case 2: testing convertation to the system with base 5', () => { + test('Case 2: testing conversion to the system with base 5', () => { expect(hornerScheme(210, 5)).toEqual('1320') expect(hornerScheme(-210, 5)).toEqual('-1320') }) - test('Case 3: testing convertation to the octal system', () => { + test('Case 3: testing conversion to the octal system', () => { expect(hornerScheme(210, 8)).toEqual('322') expect(hornerScheme(-210, 8)).toEqual('-322') }) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy