From 9f8b5ddd56e013d93a030cb069a7a300d12447d9 Mon Sep 17 00:00:00 2001 From: keithbored3310 <108907420+keithbored3310@users.noreply.github.com> Date: Sun, 7 Aug 2022 23:15:17 +0800 Subject: [PATCH 1/3] Create a Manhattan Distance Calculation Formula This is the first attempt of the Manhattan Distance Formula, feel free to update it. --- src/algorithms/math/Manhattan Distance | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/algorithms/math/Manhattan Distance diff --git a/src/algorithms/math/Manhattan Distance b/src/algorithms/math/Manhattan Distance new file mode 100644 index 00000000..6ce947a5 --- /dev/null +++ b/src/algorithms/math/Manhattan Distance @@ -0,0 +1,18 @@ +//This function is calculate the manhattan distance +@param x +@param y +@return {SumOhManhattanDistance} + +export default function manhattanDistance(x, y){ + +let SumOfManhattanDistance = 0; + +for (let i = 0; i < x.length; i++){ + for (lei j= 0; j < x[i].length ; j++){ + //Compute the result of the Manhattan Distance + SumOfManhattanDistance += (Math.abs(x[i] -x[j])+Math.abs(y[i] - y[j])); + } +} +//Send the result back to the main function +return SumOfManhattanDistance; +} From a163f805d737df97f9abf80a3407d3f224b0d225 Mon Sep 17 00:00:00 2001 From: keithbored3310 <108907420+keithbored3310@users.noreply.github.com> Date: Tue, 30 Aug 2022 22:11:35 +0800 Subject: [PATCH 2/3] Delete Manhattan Distance --- src/algorithms/math/Manhattan Distance | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 src/algorithms/math/Manhattan Distance diff --git a/src/algorithms/math/Manhattan Distance b/src/algorithms/math/Manhattan Distance deleted file mode 100644 index 6ce947a5..00000000 --- a/src/algorithms/math/Manhattan Distance +++ /dev/null @@ -1,18 +0,0 @@ -//This function is calculate the manhattan distance -@param x -@param y -@return {SumOhManhattanDistance} - -export default function manhattanDistance(x, y){ - -let SumOfManhattanDistance = 0; - -for (let i = 0; i < x.length; i++){ - for (lei j= 0; j < x[i].length ; j++){ - //Compute the result of the Manhattan Distance - SumOfManhattanDistance += (Math.abs(x[i] -x[j])+Math.abs(y[i] - y[j])); - } -} -//Send the result back to the main function -return SumOfManhattanDistance; -} From 86ccfa63be3d26c20034a4ac6b3a12c1889f5ff5 Mon Sep 17 00:00:00 2001 From: keithbored3310 Date: Tue, 30 Aug 2022 22:28:27 +0800 Subject: [PATCH 3/3] added manhattan-distance and number-calculation --- .../math/manhattan-distance/README.md | 0 .../__test__/manhattanDistance.test.js | 7 +++ .../manhattan-distance/manhattanDistance.js | 19 +++++++ .../math/number-calculation/README.md | 56 +++++++++++++++++++ .../__test__/calcIsEven.test.js | 23 ++++++++ .../__test__/calcIsOdd.test.js | 23 ++++++++ .../math/number-calculation/calcIsEven.js | 14 +++++ .../math/number-calculation/calcIsOdd.js | 15 +++++ 8 files changed, 157 insertions(+) create mode 100644 src/algorithms/math/manhattan-distance/README.md create mode 100644 src/algorithms/math/manhattan-distance/__test__/manhattanDistance.test.js create mode 100644 src/algorithms/math/manhattan-distance/manhattanDistance.js create mode 100644 src/algorithms/math/number-calculation/README.md create mode 100644 src/algorithms/math/number-calculation/__test__/calcIsEven.test.js create mode 100644 src/algorithms/math/number-calculation/__test__/calcIsOdd.test.js create mode 100644 src/algorithms/math/number-calculation/calcIsEven.js create mode 100644 src/algorithms/math/number-calculation/calcIsOdd.js diff --git a/src/algorithms/math/manhattan-distance/README.md b/src/algorithms/math/manhattan-distance/README.md new file mode 100644 index 00000000..e69de29b diff --git a/src/algorithms/math/manhattan-distance/__test__/manhattanDistance.test.js b/src/algorithms/math/manhattan-distance/__test__/manhattanDistance.test.js new file mode 100644 index 00000000..b76f36a2 --- /dev/null +++ b/src/algorithms/math/manhattan-distance/__test__/manhattanDistance.test.js @@ -0,0 +1,7 @@ +import manhattanDistance from '../manhattanDistance'; + +describe('manhattanDistance', () => { + it('should calculate the manhattan distance', () => { + expect(manhattanDistance(1,1)).toEqual(1); + }) +}) \ No newline at end of file diff --git a/src/algorithms/math/manhattan-distance/manhattanDistance.js b/src/algorithms/math/manhattan-distance/manhattanDistance.js new file mode 100644 index 00000000..aca0f0ec --- /dev/null +++ b/src/algorithms/math/manhattan-distance/manhattanDistance.js @@ -0,0 +1,19 @@ +//This function is calculate the manhattan distance +/** +* @param {string} x +* @param {string} y +* @return {string} +*/ +export default function manhattanDistance(x, y){ + +let SumOfManhattanDistance = 0; + +for (let i = 0; i < x.length; i++){ + for (let j = 0; j < x[i].length ; j++){ + //Compute the result of the Manhattan Distance + SumOfManhattanDistance += (Math.abs(x[i] -x[j])+Math.abs(y[i] - y[j])); + } +} +//Send the result back to the main function +return SumOfManhattanDistance; +} \ No newline at end of file diff --git a/src/algorithms/math/number-calculation/README.md b/src/algorithms/math/number-calculation/README.md new file mode 100644 index 00000000..93801397 --- /dev/null +++ b/src/algorithms/math/number-calculation/README.md @@ -0,0 +1,56 @@ + Calculate is even number + * @description - Checking if number is even using divisibility by 2 + * + * If number is divisible by 2 i.e remainder = 0, then it is even + * therefore, the function will return true + * + * If number is not divisible by 2 i.e remainder != 0, then it is not even i.e odd + * therefore, the function will return false + +Calculate is even bitwise number + * @description - Checking if number is even using bitwise operator + * Bitwise AND (&) compares the bits of the 32 + * bit binary representations of the number and + * returns a number after comparing each bit: + * + * 0 & 0 -> 0 + * 0 & 1 -> 0 + * 1 & 0 -> 0 + * 1 & 1 -> 1 + * + * For odd numbers, the last binary bit will be 1 + * and for even numbers, the last binary bit will + * be 0. + * + * As the number is compared with one, all the + * other bits except the last will become 0. The + * last bit will be 0 for even numbers and 1 for + * odd numbers, which is checked with the use + * of the equality operator. + +Calculate is odd number + * @description -> Checking if number is odd using not divisibility by 2 + * If number is not divisible by 2 i.e remainder = 1, then it is odd + * therefore, the function will return true + * + * If number is divisible by 2 i.e remainder != 1, then it is even + * therefore, the function will return false + +Calculate is odd bitwise + * @description -> Checking if number is even using bitwise operator + * Bitwise AND (&) compares the bits of the 32 + * bit binary representations of the number and + * returns a number after comparing each bit: + * + * 0 & 0 -> 0 + * 0 & 1 -> 0 + * 1 & 0 -> 0 + * 1 & 1 -> 1 + * + * For every odd numbers, the last binary bit will be 1 + * and for even numbers, the last binary bit will be 0. + * + * As the number is compared with one, all the + * other bits except the last will become 0. The + * last bit will be 0 for even numbers and 1 for + * odd numbers. \ No newline at end of file diff --git a/src/algorithms/math/number-calculation/__test__/calcIsEven.test.js b/src/algorithms/math/number-calculation/__test__/calcIsEven.test.js new file mode 100644 index 00000000..065f5980 --- /dev/null +++ b/src/algorithms/math/number-calculation/__test__/calcIsEven.test.js @@ -0,0 +1,23 @@ +import { calcIsEven } from "../calcIsEven"; + +describe('calcIsEven', () => { + it('should calculate even number correctly', () => { + expect(calcIsEven(2)).toEqual(true); + expect(calcIsEven(4)).toEqual(true); + }); + it('should throw an error in case if detect odd number', () =>{ + expect(() => calcIsEven(3).toThrowError('It is odd number')) + }); +}); + +import { calcIsEvenBitwise } from "../calcIsEven"; + +describe('calcIsEvenBitwise', () => { + it('should calculate even bitwise number correctly', () => { + expect(calcIsEvenBitwise(2)).toEqual(true); + expect(calcIsEvenBitwise(4)).toEqual(true); + }); + it('should throw an error in case if detect odd number', () =>{ + expect(() => calcIsEvenBitwise(3).toThrowError('It is odd bitwise number')) + }); +}); \ No newline at end of file diff --git a/src/algorithms/math/number-calculation/__test__/calcIsOdd.test.js b/src/algorithms/math/number-calculation/__test__/calcIsOdd.test.js new file mode 100644 index 00000000..c798d7d4 --- /dev/null +++ b/src/algorithms/math/number-calculation/__test__/calcIsOdd.test.js @@ -0,0 +1,23 @@ +import { calcIsOdd } from "../calcIsOdd"; + +describe('calcIsOdd', () => { + it('should calculate odd number correctly', () => { + expect(calcIsOdd(1)).toEqual(true); + expect(calcIsOdd(3)).toEqual(true); + }); + it('should throw an error in case if detect even number', () =>{ + expect(() => calcIsOdd(8).toThrowError('It is even number')) + }); +}); + +import { calcIsOddBitwise } from "../calcIsOdd"; + +describe('calcIsOddBitwise', () => { + it('should calculate odd bitwise number correctly', () => { + expect(calcIsOddBitwise(1)).toEqual(true); + expect(calcIsOddBitwise(3)).toEqual(true); + }); + it('should throw an error in case if detect even number', () =>{ + expect(() => calcIsOddBitwise(6).toThrowError('It is even bitwise number')) + }); +}); \ No newline at end of file diff --git a/src/algorithms/math/number-calculation/calcIsEven.js b/src/algorithms/math/number-calculation/calcIsEven.js new file mode 100644 index 00000000..114b2958 --- /dev/null +++ b/src/algorithms/math/number-calculation/calcIsEven.js @@ -0,0 +1,14 @@ +/** + * @function calcIsEven + * @param {number} number + * @return {boolean} + */ + export const calcIsEven = (number) => number % 2 === 0 + + + /** + * @function calcIsEvenBitwise + * @param {number} number + * @returns {boolean} + */ +export const calcIsEvenBitwise = (number) => (number & 1) === 0 \ No newline at end of file diff --git a/src/algorithms/math/number-calculation/calcIsOdd.js b/src/algorithms/math/number-calculation/calcIsOdd.js new file mode 100644 index 00000000..aebbd291 --- /dev/null +++ b/src/algorithms/math/number-calculation/calcIsOdd.js @@ -0,0 +1,15 @@ +/** + * @function calcIsOdd + * @param {number} number + * @returns {boolean} + */ + const calcIsOdd = (number) => Boolean(number % 2) // 1 -> true, 0 -> false + + /** + * @function calcIsOddBitwise + * @param {number} number + * @returns {boolean} + */ + const calcIsOddBitwise = (number) => Boolean(number & 1) // 1 -> true, 0 -> false + + export { calcIsOdd, calcIsOddBitwise } \ No newline at end of file