mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
added manhattan-distance and number-calculation
This commit is contained in:
parent
b95cb8bbb2
commit
86ccfa63be
0
src/algorithms/math/manhattan-distance/README.md
Normal file
0
src/algorithms/math/manhattan-distance/README.md
Normal file
@ -0,0 +1,7 @@
|
||||
import manhattanDistance from '../manhattanDistance';
|
||||
|
||||
describe('manhattanDistance', () => {
|
||||
it('should calculate the manhattan distance', () => {
|
||||
expect(manhattanDistance(1,1)).toEqual(1);
|
||||
})
|
||||
})
|
19
src/algorithms/math/manhattan-distance/manhattanDistance.js
Normal file
19
src/algorithms/math/manhattan-distance/manhattanDistance.js
Normal file
@ -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;
|
||||
}
|
56
src/algorithms/math/number-calculation/README.md
Normal file
56
src/algorithms/math/number-calculation/README.md
Normal file
@ -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.
|
@ -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'))
|
||||
});
|
||||
});
|
@ -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'))
|
||||
});
|
||||
});
|
14
src/algorithms/math/number-calculation/calcIsEven.js
Normal file
14
src/algorithms/math/number-calculation/calcIsEven.js
Normal file
@ -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
|
15
src/algorithms/math/number-calculation/calcIsOdd.js
Normal file
15
src/algorithms/math/number-calculation/calcIsOdd.js
Normal file
@ -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 }
|
Loading…
Reference in New Issue
Block a user