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