From de3e2f196c63a149d9ca95f70f648e748dc1b740 Mon Sep 17 00:00:00 2001 From: ddhira123 Date: 2021年10月23日 17:43:29 +0700 Subject: [PATCH 1/8] Added Hex to Binary conversion --- Conversions/HexToBinary.js | 37 ++++++++++++++++++++++++++++ Conversions/test/HexToBinary.test.js | 19 ++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 Conversions/HexToBinary.js create mode 100644 Conversions/test/HexToBinary.test.js diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js new file mode 100644 index 0000000000..b67b00c820 --- /dev/null +++ b/Conversions/HexToBinary.js @@ -0,0 +1,37 @@ +const binLookup = (c) => { + switch (c.toLowerCase()) { + case '0': return '0000' + case '1': return '0001' + case '2': return '0010' + case '3': return '0011' + case '4': return '0100' + case '5': return '0101' + case '6': return '0110' + case '7': return '0111' + case '8': return '1000' + case '9': return '1001' + case 'a': return '1010' + case 'b': return '1011' + case 'c': return '1100' + case 'd': return '1101' + case 'e': return '1110' + case 'f': return '1111' + default: return '' + } +} +const hexToBinary = (hexString) => { + /* + Function for convertung Hex to Binary + + 1. We convert every hexadecimal bit to 4 binary bits + 2. Conversion goes by searching in the lookup table + + */ + + let result = '' + hexString = hexString.split('') + hexString.forEach(c => { result += binLookup(c) }) + return result +} + +export default hexToBinary diff --git a/Conversions/test/HexToBinary.test.js b/Conversions/test/HexToBinary.test.js new file mode 100644 index 0000000000..40148d1cee --- /dev/null +++ b/Conversions/test/HexToBinary.test.js @@ -0,0 +1,19 @@ +import hexToBinary from '../HexToBinary' + +describe('hexToBinary', () => { + it('expects to return correct hexadecimal value', () => { + expect(hexToBinary('8')).toBe('1000') + }) + + it('expects to return correct binary value for more than one hex digit', () => { + expect(hexToBinary('EA')).toBe('11101010') + }) + + it('expects to test its robustness as it should be case-insensitive', () => { + expect(hexToBinary('4d')).toBe('01001101') + }) + + it('expects to return correct hexadecimal value, matching (num).toString(2)', () => { + expect(hexToBinary('F')).toBe(parseInt('F', 16).toString(2)) + }) +}) From 14320374fbd902d7cf885c4834995014a0ff5a1e Mon Sep 17 00:00:00 2001 From: Dhana D <39583785+ddhira123@users.noreply.github.com> Date: 2021年10月24日 00:51:15 +0700 Subject: [PATCH 2/8] Update Conversions/HexToBinary.js Co-authored-by: Rak Laptudirm --- Conversions/HexToBinary.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js index b67b00c820..8354af73c4 100644 --- a/Conversions/HexToBinary.js +++ b/Conversions/HexToBinary.js @@ -29,7 +29,7 @@ const hexToBinary = (hexString) => { */ let result = '' - hexString = hexString.split('') + const hexLexemes = hexString.split('') hexString.forEach(c => { result += binLookup(c) }) return result } From 6efa3db630720873ce677be4c14b952207c7618b Mon Sep 17 00:00:00 2001 From: Dhana D <39583785+ddhira123@users.noreply.github.com> Date: 2021年10月24日 00:51:25 +0700 Subject: [PATCH 3/8] Update Conversions/HexToBinary.js Co-authored-by: Rak Laptudirm --- Conversions/HexToBinary.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js index 8354af73c4..124eeef1f6 100644 --- a/Conversions/HexToBinary.js +++ b/Conversions/HexToBinary.js @@ -30,7 +30,7 @@ const hexToBinary = (hexString) => { let result = '' const hexLexemes = hexString.split('') - hexString.forEach(c => { result += binLookup(c) }) + hexLexemes.map(lexeme => binLookup(lexeme)) return result } From 312332294c1fbbbcc0e6a5f1eba37a2e0dc91877 Mon Sep 17 00:00:00 2001 From: Dhana D <39583785+ddhira123@users.noreply.github.com> Date: 2021年10月24日 00:51:34 +0700 Subject: [PATCH 4/8] Update Conversions/HexToBinary.js Co-authored-by: Rak Laptudirm --- Conversions/HexToBinary.js | 1 - 1 file changed, 1 deletion(-) diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js index 124eeef1f6..a2af02ac46 100644 --- a/Conversions/HexToBinary.js +++ b/Conversions/HexToBinary.js @@ -28,7 +28,6 @@ const hexToBinary = (hexString) => { */ - let result = '' const hexLexemes = hexString.split('') hexLexemes.map(lexeme => binLookup(lexeme)) return result From b70af8d47580bacf413bcd3533dafdd39516fdf4 Mon Sep 17 00:00:00 2001 From: Dhana D <39583785+ddhira123@users.noreply.github.com> Date: 2021年10月24日 00:51:39 +0700 Subject: [PATCH 5/8] Update Conversions/HexToBinary.js Co-authored-by: Rak Laptudirm --- Conversions/HexToBinary.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js index a2af02ac46..5a82149db4 100644 --- a/Conversions/HexToBinary.js +++ b/Conversions/HexToBinary.js @@ -30,7 +30,7 @@ const hexToBinary = (hexString) => { const hexLexemes = hexString.split('') hexLexemes.map(lexeme => binLookup(lexeme)) - return result + return hexLexemes.join('') } export default hexToBinary From 45d14b70abc2195e2447bba4f4e122f97564a2e0 Mon Sep 17 00:00:00 2001 From: ddhira123 Date: 2021年10月24日 01:21:08 +0700 Subject: [PATCH 6/8] Fix errors --- Conversions/HexToBinary.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js index c1565ec438..3e6bf8f2e0 100644 --- a/Conversions/HexToBinary.js +++ b/Conversions/HexToBinary.js @@ -28,8 +28,7 @@ const hexToBinary = (hexString) => { */ const hexLexemes = hexString.split('') - hexLexemes.map(lexeme => binLookup(lexeme)) - return hexLexemes.join('') + return hexLexemes.map(lexeme => binLookup(lexeme)).join('') } export default hexToBinary From 5dbc20f4150c5ad99491deae68b0c2b7cb11f8c3 Mon Sep 17 00:00:00 2001 From: Rak Laptudirm Date: 2021年10月24日 14:23:38 +0530 Subject: [PATCH 7/8] fix: typo --- Conversions/HexToBinary.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js index 3e6bf8f2e0..2334954f92 100644 --- a/Conversions/HexToBinary.js +++ b/Conversions/HexToBinary.js @@ -21,7 +21,7 @@ const binLookup = (c) => { } const hexToBinary = (hexString) => { /* - Function for convertung Hex to Binary + Function for converting Hex to Binary 1. We convert every hexadecimal bit to 4 binary bits 2. Conversion goes by searching in the lookup table From 8ede06c7edc6e942cbdb142b05351f558e3f27e1 Mon Sep 17 00:00:00 2001 From: ddhira123 Date: 2021年10月25日 00:08:06 +0700 Subject: [PATCH 8/8] Added Manhattan Distance Algorithm --- Maths/Coordinate.js | 10 ++++++-- Maths/test/Coordinate.test.js | 47 +++++++++++++++++++++++++---------- 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/Maths/Coordinate.js b/Maths/Coordinate.js index ae18f5a8b1..9bff1b3424 100644 --- a/Maths/Coordinate.js +++ b/Maths/Coordinate.js @@ -4,10 +4,16 @@ Example: coorDistance(2,2,14,11) will return 15 Wikipedia reference: https://en.wikipedia.org/wiki/Geographical_distance#Flat-surface_formulae */ -const distance2points = (longitude1, latitude1, longitude2, latitude2) => { +const euclideanDistance = (longitude1, latitude1, longitude2, latitude2) => { const width = longitude2 - longitude1 const height = latitude2 - latitude1 return (Math.sqrt(width * width + height * height)) } -export { distance2points } +const manhattanDistance = (longitude1, latitude1, longitude2, latitude2) => { + const width = Math.abs(longitude2 - longitude1) + const height = Math.abs(latitude2 - latitude1) + return width + height +} + +export { euclideanDistance, manhattanDistance } diff --git a/Maths/test/Coordinate.test.js b/Maths/test/Coordinate.test.js index 2d829ecdd9..d32b2c3c47 100644 --- a/Maths/test/Coordinate.test.js +++ b/Maths/test/Coordinate.test.js @@ -1,22 +1,43 @@ import * as coordinate from '../Coordinate' -describe('Testing distance2points calculations', () => { +describe('Testing euclideanDistance calculations', () => { it('Should give a numeric output (distance between 2 points) with 4 numeric arguments', () => { - const distance2points = coordinate.distance2points(2, 2, -10, -7) - expect(distance2points).toBe(15) + const euclideanDistance = coordinate.euclideanDistance(2, 2, -10, -7) + expect(euclideanDistance).toBe(15) }) it('Should not give any output given non-numeric argument', () => { - const distance2points = coordinate.distance2points('ABC', '123', '', '###') - expect(distance2points).toBeNaN() + const euclideanDistance = coordinate.euclideanDistance('ABC', '123', '', '###') + expect(euclideanDistance).toBeNaN() }) it('Should not give any output given any number of numeric arguments less than 4', () => { - const distance2points3arg = coordinate.distance2points(2, 2, -10) - const distance2points2arg = coordinate.distance2points(2, 2) - const distance2points1arg = coordinate.distance2points(2) - const distance2points0arg = coordinate.distance2points() - expect(distance2points3arg).toBeNaN() - expect(distance2points2arg).toBeNaN() - expect(distance2points1arg).toBeNaN() - expect(distance2points0arg).toBeNaN() + const euclideanDistance3arg = coordinate.euclideanDistance(2, 2, -10) + const euclideanDistance2arg = coordinate.euclideanDistance(2, 2) + const euclideanDistance1arg = coordinate.euclideanDistance(2) + const euclideanDistance0arg = coordinate.euclideanDistance() + expect(euclideanDistance3arg).toBeNaN() + expect(euclideanDistance2arg).toBeNaN() + expect(euclideanDistance1arg).toBeNaN() + expect(euclideanDistance0arg).toBeNaN() + }) +}) + +describe('Testing manhattanDistance calculations', () => { + it('Should give a numeric output (distance between 2 points) with 4 numeric arguments', () => { + const manhattanDistance = coordinate.manhattanDistance(2, 2, -10, -7) + expect(manhattanDistance).toBe(21) + }) + it('Should not give any output given non-numeric argument', () => { + const manhattanDistance = coordinate.manhattanDistance('ABC', '123', '', '###') + expect(manhattanDistance).toBeNaN() + }) + it('Should not give any output given any number of numeric arguments less than 4', () => { + const manhattanDistance3arg = coordinate.manhattanDistance(2, 2, -10) + const manhattanDistance2arg = coordinate.manhattanDistance(2, 2) + const manhattanDistance1arg = coordinate.manhattanDistance(2) + const manhattanDistance0arg = coordinate.manhattanDistance() + expect(manhattanDistance3arg).toBeNaN() + expect(manhattanDistance2arg).toBeNaN() + expect(manhattanDistance1arg).toBeNaN() + expect(manhattanDistance0arg).toBeNaN() }) })

AltStyle によって変換されたページ (->オリジナル) /