From 6e1e6e85f82c4fd04426e73ac4b10236b1a30923 Mon Sep 17 00:00:00 2001 From: ChrDek Date: Wed, 4 Oct 2023 14:58:55 +0300 Subject: [PATCH 01/20] Added HammingCode.js Alternative with parity bit check. --- Bit-Manipulation/HammingCode.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Bit-Manipulation/HammingCode.js diff --git a/Bit-Manipulation/HammingCode.js b/Bit-Manipulation/HammingCode.js new file mode 100644 index 0000000000..37552dab92 --- /dev/null +++ b/Bit-Manipulation/HammingCode.js @@ -0,0 +1,29 @@ +/** + * github author: chrdek + * license: GPL-3.0 or later + * + * @param {number} b + * + * The following code generates the hamming code for + * 32-bit integer values (incl. parity bit check) + * + **/ + +function HammingCode(b) { + var bytes = new Array(65536); + const bitAlloc = (bin) => { + var cnt = 0; + while (bin > 0) { + cnt += bin & 1; + bin >>=1; + } + return cnt; + } //end of bitallocation + +for (var k=0; k < 65536; k++) bytes[k] = bitAlloc(k); + +//perform leftmost shifting for integer value +return bytes[bin & 0xFFFF] + bytes[b >> 16]; +} + +export { HammingCode } \ No newline at end of file From 27aa6c0b11764f282768228fff2c815c8c71c76e Mon Sep 17 00:00:00 2001 From: ChrDek Date: Wed, 4 Oct 2023 15:00:16 +0300 Subject: [PATCH 02/20] Tests for HammingCode.js Test suite for 32-bit values in different formats. --- Bit-Manipulation/test/HammingCode.test.js | 41 +++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Bit-Manipulation/test/HammingCode.test.js diff --git a/Bit-Manipulation/test/HammingCode.test.js b/Bit-Manipulation/test/HammingCode.test.js new file mode 100644 index 0000000000..6acd45806a --- /dev/null +++ b/Bit-Manipulation/test/HammingCode.test.js @@ -0,0 +1,41 @@ +import { HammingCode } from '../HammingCode' + +test('checks HammingCode of 1110101 hex converted is 5', () => { + const code = HammingCode(parseInt("1110101",16)) + expect(code).toBe(5) +}) + +test('checks HammingCode of 10043091 hex converted is 7', () => { + const code = HammingCode(parseInt("10043091",16)) + expect(code).toBe(7) +}) + +test('checks HammingCode of 889193 hex converted is 9', () => { + const code = HammingCode(parseInt("889193",16)) + expect(code).toBe(9) +}) + +test('checks HammingCode of 1110101 hex converted is 5', () => { + const code = HammingCode(parseInt("1101110",2)) + expect(code).toBe(5) +}) + +test('checks HammingCode of 1101110 is 5', () => { + const code = HammingCode(1101110) + expect(code).toBe(5) +}) + +test('checks HammingCode of 0x10043091 is 7', () => { + const code = HammingCode(0x10043091) + expect(code).toBe(7) +}) + +test('checks HammingCode of 0x8891930311 is NaN (above range)', () => { + const code = HammingCode(0x8891930311) + expect(code).toBe(NaN) +}) + +test('checks HammingCode of the Number 100 is 3', () => { + const code = HammingCode((Number(100)) + expect(code).toBe(3) +}) \ No newline at end of file From 060ee868bcbae926d394351aaf3b225ee0c5e4cf Mon Sep 17 00:00:00 2001 From: ChrDek Date: Wed, 4 Oct 2023 15:01:35 +0300 Subject: [PATCH 03/20] Delete Bit-Manipulation/HammingCode.js --- Bit-Manipulation/HammingCode.js | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 Bit-Manipulation/HammingCode.js diff --git a/Bit-Manipulation/HammingCode.js b/Bit-Manipulation/HammingCode.js deleted file mode 100644 index 37552dab92..0000000000 --- a/Bit-Manipulation/HammingCode.js +++ /dev/null @@ -1,29 +0,0 @@ -/** - * github author: chrdek - * license: GPL-3.0 or later - * - * @param {number} b - * - * The following code generates the hamming code for - * 32-bit integer values (incl. parity bit check) - * - **/ - -function HammingCode(b) { - var bytes = new Array(65536); - const bitAlloc = (bin) => { - var cnt = 0; - while (bin > 0) { - cnt += bin & 1; - bin >>=1; - } - return cnt; - } //end of bitallocation - -for (var k=0; k < 65536; k++) bytes[k] = bitAlloc(k); - -//perform leftmost shifting for integer value -return bytes[bin & 0xFFFF] + bytes[b >> 16]; -} - -export { HammingCode } \ No newline at end of file From 0c796096e7f673f706bd5ac3280df65ea0e172eb Mon Sep 17 00:00:00 2001 From: ChrDek Date: Wed, 4 Oct 2023 15:03:01 +0300 Subject: [PATCH 04/20] feat: HammingCode for 32bit values Added hamming code with parity value checks --- Bit-Manipulation/HammingCode.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Bit-Manipulation/HammingCode.js diff --git a/Bit-Manipulation/HammingCode.js b/Bit-Manipulation/HammingCode.js new file mode 100644 index 0000000000..b1b43ac38f --- /dev/null +++ b/Bit-Manipulation/HammingCode.js @@ -0,0 +1,29 @@ +/** + * github author: chrdek + * license: GPL-3.0 or later + * + * @param {number} b + * + * The following code generates the hamming code for + * 32-bit integer values (incl. parity bit check) + * + **/ + +function HammingCode(b) { + var bytes = new Array(65536); + const bitAlloc = (bin) => { + var cnt = 0; + while (bin > 0) { + cnt += bin & 1; + bin >>=1; + } + return cnt; + } //end of bitallocation + +for (var k=0; k < 65536; k++) bytes[k] = bitAlloc(k); + +//perform leftmost shifting for integer value +return bytes[b & 0xFFFF] + bytes[b >> 16]; +} + +export { HammingCode } \ No newline at end of file From 039a57245b7b109921d7503737cb7f067b58e377 Mon Sep 17 00:00:00 2001 From: ChrDek Date: Wed, 4 Oct 2023 15:15:47 +0300 Subject: [PATCH 05/20] Update HammingCode.test.js (fixes for tests) --- Bit-Manipulation/test/HammingCode.test.js | 48 +++++++++++------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/Bit-Manipulation/test/HammingCode.test.js b/Bit-Manipulation/test/HammingCode.test.js index 6acd45806a..0250f05b40 100644 --- a/Bit-Manipulation/test/HammingCode.test.js +++ b/Bit-Manipulation/test/HammingCode.test.js @@ -1,41 +1,41 @@ import { HammingCode } from '../HammingCode' test('checks HammingCode of 1110101 hex converted is 5', () => { - const code = HammingCode(parseInt("1110101",16)) - expect(code).toBe(5) -}) + const code = HammingCode(parseInt("1110101",16)); + expect(code).toBe(5); +}); test('checks HammingCode of 10043091 hex converted is 7', () => { - const code = HammingCode(parseInt("10043091",16)) - expect(code).toBe(7) -}) + const code = HammingCode(parseInt("10043091",16)); + expect(code).toBe(7); +}); test('checks HammingCode of 889193 hex converted is 9', () => { - const code = HammingCode(parseInt("889193",16)) - expect(code).toBe(9) -}) + const code = HammingCode(parseInt("889193",16)); + expect(code).toBe(9); +}); test('checks HammingCode of 1110101 hex converted is 5', () => { - const code = HammingCode(parseInt("1101110",2)) - expect(code).toBe(5) -}) + const code = HammingCode(parseInt("1101110",2)); + expect(code).toBe(5); +}); test('checks HammingCode of 1101110 is 5', () => { - const code = HammingCode(1101110) - expect(code).toBe(5) -}) + const code = HammingCode(1101110); + expect(code).toBe(5); +}); test('checks HammingCode of 0x10043091 is 7', () => { - const code = HammingCode(0x10043091) - expect(code).toBe(7) -}) + const code = HammingCode(0x10043091); + expect(code).toBe(7); +}); test('checks HammingCode of 0x8891930311 is NaN (above range)', () => { - const code = HammingCode(0x8891930311) - expect(code).toBe(NaN) -}) + const code = HammingCode(0x8891930311); + expect(code).toBeNaN(); +}); test('checks HammingCode of the Number 100 is 3', () => { - const code = HammingCode((Number(100)) - expect(code).toBe(3) -}) \ No newline at end of file + const code = HammingCode((Number(100)); + expect(code).toBe(3); +}); From d3b2f1cf4bddcd85159ac30855b19d91a8dfe2bb Mon Sep 17 00:00:00 2001 From: ChrDek Date: Wed, 4 Oct 2023 15:24:28 +0300 Subject: [PATCH 06/20] Update HammingCode.test.js --- Bit-Manipulation/test/HammingCode.test.js | 48 +++++++++++------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/Bit-Manipulation/test/HammingCode.test.js b/Bit-Manipulation/test/HammingCode.test.js index 0250f05b40..7f49d885b3 100644 --- a/Bit-Manipulation/test/HammingCode.test.js +++ b/Bit-Manipulation/test/HammingCode.test.js @@ -1,41 +1,41 @@ import { HammingCode } from '../HammingCode' test('checks HammingCode of 1110101 hex converted is 5', () => { - const code = HammingCode(parseInt("1110101",16)); - expect(code).toBe(5); -}); + const code = HammingCode(parseInt("1110101",16)) + expect(code).toBe(5) +}) test('checks HammingCode of 10043091 hex converted is 7', () => { - const code = HammingCode(parseInt("10043091",16)); - expect(code).toBe(7); -}); + const code = HammingCode(parseInt("10043091",16)) + expect(code).toBe(7) +}) test('checks HammingCode of 889193 hex converted is 9', () => { - const code = HammingCode(parseInt("889193",16)); - expect(code).toBe(9); -}); + const code = HammingCode(parseInt("889193",16)) + expect(code).toBe(9) +}) test('checks HammingCode of 1110101 hex converted is 5', () => { - const code = HammingCode(parseInt("1101110",2)); - expect(code).toBe(5); -}); + const code = HammingCode(parseInt("1101110",2)) + expect(code).toBe(5) +}) test('checks HammingCode of 1101110 is 5', () => { - const code = HammingCode(1101110); - expect(code).toBe(5); -}); + const code = HammingCode(1101110) + expect(code).toBe(5) +}) test('checks HammingCode of 0x10043091 is 7', () => { - const code = HammingCode(0x10043091); - expect(code).toBe(7); -}); + const code = HammingCode(0x10043091) + expect(code).toBe(7) +}) test('checks HammingCode of 0x8891930311 is NaN (above range)', () => { - const code = HammingCode(0x8891930311); - expect(code).toBeNaN(); -}); + const code = HammingCode(0x8891930311) + expect(code).toBeNaN() +}) test('checks HammingCode of the Number 100 is 3', () => { - const code = HammingCode((Number(100)); - expect(code).toBe(3); -}); + const code = HammingCode((Number(100)) + expect(code).toBe(3) +}) From 002038f255fbf9ac1fce7fba2bcb353a86ff6319 Mon Sep 17 00:00:00 2001 From: ChrDek Date: Wed, 4 Oct 2023 15:33:06 +0300 Subject: [PATCH 07/20] Update HammingCode.test.js --- Bit-Manipulation/test/HammingCode.test.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Bit-Manipulation/test/HammingCode.test.js b/Bit-Manipulation/test/HammingCode.test.js index 7f49d885b3..b94cc80279 100644 --- a/Bit-Manipulation/test/HammingCode.test.js +++ b/Bit-Manipulation/test/HammingCode.test.js @@ -1,41 +1,41 @@ import { HammingCode } from '../HammingCode' test('checks HammingCode of 1110101 hex converted is 5', () => { - const code = HammingCode(parseInt("1110101",16)) + let code = HammingCode(parseInt("1110101",16)) expect(code).toBe(5) }) test('checks HammingCode of 10043091 hex converted is 7', () => { - const code = HammingCode(parseInt("10043091",16)) + let code = HammingCode(parseInt("10043091",16)) expect(code).toBe(7) }) test('checks HammingCode of 889193 hex converted is 9', () => { - const code = HammingCode(parseInt("889193",16)) + let code = HammingCode(parseInt("889193",16)) expect(code).toBe(9) }) test('checks HammingCode of 1110101 hex converted is 5', () => { - const code = HammingCode(parseInt("1101110",2)) + let code = HammingCode(parseInt("1101110",2)) expect(code).toBe(5) }) test('checks HammingCode of 1101110 is 5', () => { - const code = HammingCode(1101110) + let code = HammingCode(1101110) expect(code).toBe(5) }) test('checks HammingCode of 0x10043091 is 7', () => { - const code = HammingCode(0x10043091) + let code = HammingCode(0x10043091) expect(code).toBe(7) }) test('checks HammingCode of 0x8891930311 is NaN (above range)', () => { - const code = HammingCode(0x8891930311) + let code = HammingCode(0x8891930311) expect(code).toBeNaN() }) test('checks HammingCode of the Number 100 is 3', () => { - const code = HammingCode((Number(100)) + let code = HammingCode((Number(100)) expect(code).toBe(3) }) From 3bc23afee58760fa953ebd4eb77ddc63729c242b Mon Sep 17 00:00:00 2001 From: ChrDek Date: Wed, 4 Oct 2023 19:22:50 +0300 Subject: [PATCH 08/20] Update HammingCode.test.js Refactored tests, usage of .each for most test cases. --- Bit-Manipulation/test/HammingCode.test.js | 49 +++++------------------ 1 file changed, 11 insertions(+), 38 deletions(-) diff --git a/Bit-Manipulation/test/HammingCode.test.js b/Bit-Manipulation/test/HammingCode.test.js index b94cc80279..63fb3b311c 100644 --- a/Bit-Manipulation/test/HammingCode.test.js +++ b/Bit-Manipulation/test/HammingCode.test.js @@ -1,41 +1,14 @@ import { HammingCode } from '../HammingCode' -test('checks HammingCode of 1110101 hex converted is 5', () => { - let code = HammingCode(parseInt("1110101",16)) - expect(code).toBe(5) -}) - -test('checks HammingCode of 10043091 hex converted is 7', () => { - let code = HammingCode(parseInt("10043091",16)) - expect(code).toBe(7) -}) - -test('checks HammingCode of 889193 hex converted is 9', () => { - let code = HammingCode(parseInt("889193",16)) - expect(code).toBe(9) -}) - -test('checks HammingCode of 1110101 hex converted is 5', () => { - let code = HammingCode(parseInt("1101110",2)) - expect(code).toBe(5) -}) - -test('checks HammingCode of 1101110 is 5', () => { - let code = HammingCode(1101110) - expect(code).toBe(5) -}) - -test('checks HammingCode of 0x10043091 is 7', () => { - let code = HammingCode(0x10043091) - expect(code).toBe(7) -}) - -test('checks HammingCode of 0x8891930311 is NaN (above range)', () => { - let code = HammingCode(0x8891930311) - expect(code).toBeNaN() -}) - -test('checks HammingCode of the Number 100 is 3', () => { - let code = HammingCode((Number(100)) - expect(code).toBe(3) +describe.each([ + { inputVal:Number(117), expectedVal: 5 }, + { inputVal:parseInt(0x9F9,16), expectedVal: 7 }, + { inputVal:parseInt(0x889193,16), expectedVal: 10 }, + { inputVal:10043091, expectedVal: 14}, + { inputVal:parseInt("1101110",2), expectedVal: 5 }, + { inputVal:parseInt(1101110,2), expectedVal: 5 } +])('Resulting hamming code conversion from $inputVal', ({inputVal, expectedVal}) => { + test(`returns bit count = ${expectedVal}`, () => { + expect(inputVal).toBe(expectedVal) + }) }) From e08154226e257dde0e7ef01f7e45c8878c459540 Mon Sep 17 00:00:00 2001 From: ChrDek Date: Wed, 4 Oct 2023 19:22:52 +0300 Subject: [PATCH 09/20] Update HammingCode.js Changes according to specs. - more understandable variable names/ comments etc. - usage of let/const Note: code refers to bit population count of a binary sequence --- Bit-Manipulation/HammingCode.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/Bit-Manipulation/HammingCode.js b/Bit-Manipulation/HammingCode.js index b1b43ac38f..585e654d5c 100644 --- a/Bit-Manipulation/HammingCode.js +++ b/Bit-Manipulation/HammingCode.js @@ -2,28 +2,33 @@ * github author: chrdek * license: GPL-3.0 or later * - * @param {number} b + * @param {number} b = integer or, binary representation of it * - * The following code generates the hamming code for - * 32-bit integer values (incl. parity bit check) + * The following code generates the hamming code for a binary sequence + * of 32-bit integer values (incl. parity bit check) + * + * Returns the overall of bit count population for values up to 65535, inclusive * **/ function HammingCode(b) { - var bytes = new Array(65536); + //preallocate total number of integers to count the bits population from. + let bytes = new Array(65536); const bitAlloc = (bin) => { - var cnt = 0; + let counter = 0; while (bin > 0) { - cnt += bin & 1; + counter += bin & 1; bin >>=1; } - return cnt; - } //end of bitallocation + return counter; + } -for (var k=0; k < 65536; k++) bytes[k] = bitAlloc(k); +//count all 1-bits from entire bit set +for (let k=0; k < 65536; k++) +bytes[k] = bitAlloc(k); -//perform leftmost shifting for integer value +//perform bit shifting for integer values for bit-populated result return bytes[b & 0xFFFF] + bytes[b >> 16]; } -export { HammingCode } \ No newline at end of file +export { HammingCode } From cabbc71c300493fcd1f26e830d97e78493e87f61 Mon Sep 17 00:00:00 2001 From: ChrDek Date: Wed, 4 Oct 2023 19:28:05 +0300 Subject: [PATCH 10/20] Update HammingCode.test.js --- Bit-Manipulation/test/HammingCode.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bit-Manipulation/test/HammingCode.test.js b/Bit-Manipulation/test/HammingCode.test.js index 63fb3b311c..9f8d43135b 100644 --- a/Bit-Manipulation/test/HammingCode.test.js +++ b/Bit-Manipulation/test/HammingCode.test.js @@ -9,6 +9,6 @@ describe.each([ { inputVal:parseInt(1101110,2), expectedVal: 5 } ])('Resulting hamming code conversion from $inputVal', ({inputVal, expectedVal}) => { test(`returns bit count = ${expectedVal}`, () => { - expect(inputVal).toBe(expectedVal) + expect(HammingCode(inputVal)).toBe(expectedVal) }) }) From 5258492c10d42e515cd788573261940bffbb6612 Mon Sep 17 00:00:00 2001 From: ChrDek Date: Fri, 6 Oct 2023 16:26:24 +0300 Subject: [PATCH 11/20] Create TriangularNumberCheck.test.js --- Maths/test/TriangularNumberCheck.test.js | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Maths/test/TriangularNumberCheck.test.js diff --git a/Maths/test/TriangularNumberCheck.test.js b/Maths/test/TriangularNumberCheck.test.js new file mode 100644 index 0000000000..101f843686 --- /dev/null +++ b/Maths/test/TriangularNumberCheck.test.js @@ -0,0 +1,39 @@ +import { IsTriangularNumber } from '../IsTriangularNumber' + +describe.each([ + { inputVal:55, expectedVal:10 }, + { inputVal:45, expectedVal:9 }, + { inputVal:190, expectedVal:19 }, + { inputVal:66, expectedVal:11 }, + { inputVal:666, expectedVal:36 }, + { inputVal:6, expectedVal:3 }, + { inputVal:10, expectedVal:4 }, + { inputVal:1, expectedVal:1 }, +])('If the number of $inputVal is triangular', ({inputVal, expectedVal}) => { + test(`should return ${expectedVal}`, () => { + expect(IsTriangularNumber(inputVal)).toBe(expectedVal) + }) +}) + +describe.each([ + { inputVal:-1, expectedVal:-1 }, + { inputVal:-10, expectedVal:-1 }, + { inputVal:-9, expectedVal:-1 }, + { inputVal:"foo", expectedVal:-1 }, + { inputVal:"fizz", expectedVal:-1 }, +])('If the number of $inputVal is completely wrong or negative', ({inputVal, expectedVal}) => { + test(`should return ${expectedVal}`, () => { + expect(IsTriangularNumber(inputVal)).not.toBeLessThan(-1) + }) +}) + +describe.each([ + { inputVal:4183059834009, expectedVal:0 }, + { inputVal:69, expectedVal:0 }, + { inputVal:0, expectedVal:0 }, + { inputVal:23, expectedVal:0 } +])('If the number of $inputVal is NOT triangular', ({inputVal, expectedVal}) => { + test(`should return ${expectedVal}`, () => { + expect(IsTriangularNumber(inputVal)).toBe(0) + }) +}) From 9539ec046236b66c32bfc180446d46fedd456f27 Mon Sep 17 00:00:00 2001 From: ChrDek Date: Fri, 6 Oct 2023 16:27:03 +0300 Subject: [PATCH 12/20] Create TriangularNumberCheck.js --- Maths/TriangularNumberCheck.js | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Maths/TriangularNumberCheck.js diff --git a/Maths/TriangularNumberCheck.js b/Maths/TriangularNumberCheck.js new file mode 100644 index 0000000000..4357bdc5ed --- /dev/null +++ b/Maths/TriangularNumberCheck.js @@ -0,0 +1,39 @@ +/** + * github author: chrdek + * license: GPL-3.0 or later + * + * @param {number} n = number to be determined whether is a triangular or not + * + * The function below is a mathematical algorithm to check if a given number n is a triangular number. + * A triangular number is one that can be represented + * in the form of the sum of consecutive positive integers starting from 1. + * + * Returns -1 for error input or negative numerical input, 0 for non-triangular + * and an integer value if the number is triangular. + * + * The variable discriminant is calculated as the square root of (1 + 8 * n) and then its square root is taken again. + * The expression Math.sqrt(1 + 8 * n) is the discriminant of the quadratic equation that represents triangular numbers. + * + **/ + +function IsTriangularNumber(n) { + // Ensure the input n is a non-negative integer, retrurn -1 to indicate error. + if (!Number.isInteger(n) || n < 0) { + return -1; + } + + // Calculate the discriminant of the quadratic equation + const discriminant = Math.sqrt(1 + 8 * n); + + // Check if the discriminant is an integer + if (discriminant % 1 === 0) { + + // Return half of the integer value of the discriminant + return Math.floor(discriminant / 2); + } else { + // If not a triangular number, return 0 + return 0; + } +} + +export { IsTriangularNumber } From 934d81de65b7c94d3ca49aabebaf9606b3fc43f8 Mon Sep 17 00:00:00 2001 From: ChrDek Date: Fri, 6 Oct 2023 16:32:20 +0300 Subject: [PATCH 13/20] Update TriangularNumberCheck.js --- Maths/TriangularNumberCheck.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/TriangularNumberCheck.js b/Maths/TriangularNumberCheck.js index 4357bdc5ed..d4990d2f48 100644 --- a/Maths/TriangularNumberCheck.js +++ b/Maths/TriangularNumberCheck.js @@ -16,7 +16,7 @@ * **/ -function IsTriangularNumber(n) { +function TriangularNumberCheck(n) { // Ensure the input n is a non-negative integer, retrurn -1 to indicate error. if (!Number.isInteger(n) || n < 0) { return -1; @@ -36,4 +36,4 @@ function IsTriangularNumber(n) { } } -export { IsTriangularNumber } +export { TriangularNumberCheck } From 85dc4c6bd9cd3bec46295c6083556221617c3af7 Mon Sep 17 00:00:00 2001 From: ChrDek Date: Fri, 6 Oct 2023 16:32:56 +0300 Subject: [PATCH 14/20] Update TriangularNumberCheck.test.js --- Maths/test/TriangularNumberCheck.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Maths/test/TriangularNumberCheck.test.js b/Maths/test/TriangularNumberCheck.test.js index 101f843686..7ee9b3f892 100644 --- a/Maths/test/TriangularNumberCheck.test.js +++ b/Maths/test/TriangularNumberCheck.test.js @@ -1,4 +1,4 @@ -import { IsTriangularNumber } from '../IsTriangularNumber' +import { TriangularNumberCheck } from '../TriangularNumberCheck' describe.each([ { inputVal:55, expectedVal:10 }, @@ -11,7 +11,7 @@ describe.each([ { inputVal:1, expectedVal:1 }, ])('If the number of $inputVal is triangular', ({inputVal, expectedVal}) => { test(`should return ${expectedVal}`, () => { - expect(IsTriangularNumber(inputVal)).toBe(expectedVal) + expect(TriangularNumberCheck(inputVal)).toBe(expectedVal) }) }) @@ -23,7 +23,7 @@ describe.each([ { inputVal:"fizz", expectedVal:-1 }, ])('If the number of $inputVal is completely wrong or negative', ({inputVal, expectedVal}) => { test(`should return ${expectedVal}`, () => { - expect(IsTriangularNumber(inputVal)).not.toBeLessThan(-1) + expect(TriangularNumberCheck(inputVal)).not.toBeLessThan(-1) }) }) @@ -34,6 +34,6 @@ describe.each([ { inputVal:23, expectedVal:0 } ])('If the number of $inputVal is NOT triangular', ({inputVal, expectedVal}) => { test(`should return ${expectedVal}`, () => { - expect(IsTriangularNumber(inputVal)).toBe(0) + expect(TriangularNumberCheck(inputVal)).toBe(0) }) }) From a989afbc08c9d1af6cbaeb171c20b37fb785f20c Mon Sep 17 00:00:00 2001 From: ChrDek Date: Mon, 9 Oct 2023 15:49:03 +0300 Subject: [PATCH 15/20] Delete Maths/TriangularNumberCheck.js --- Maths/TriangularNumberCheck.js | 39 ---------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 Maths/TriangularNumberCheck.js diff --git a/Maths/TriangularNumberCheck.js b/Maths/TriangularNumberCheck.js deleted file mode 100644 index d4990d2f48..0000000000 --- a/Maths/TriangularNumberCheck.js +++ /dev/null @@ -1,39 +0,0 @@ -/** - * github author: chrdek - * license: GPL-3.0 or later - * - * @param {number} n = number to be determined whether is a triangular or not - * - * The function below is a mathematical algorithm to check if a given number n is a triangular number. - * A triangular number is one that can be represented - * in the form of the sum of consecutive positive integers starting from 1. - * - * Returns -1 for error input or negative numerical input, 0 for non-triangular - * and an integer value if the number is triangular. - * - * The variable discriminant is calculated as the square root of (1 + 8 * n) and then its square root is taken again. - * The expression Math.sqrt(1 + 8 * n) is the discriminant of the quadratic equation that represents triangular numbers. - * - **/ - -function TriangularNumberCheck(n) { - // Ensure the input n is a non-negative integer, retrurn -1 to indicate error. - if (!Number.isInteger(n) || n < 0) { - return -1; - } - - // Calculate the discriminant of the quadratic equation - const discriminant = Math.sqrt(1 + 8 * n); - - // Check if the discriminant is an integer - if (discriminant % 1 === 0) { - - // Return half of the integer value of the discriminant - return Math.floor(discriminant / 2); - } else { - // If not a triangular number, return 0 - return 0; - } -} - -export { TriangularNumberCheck } From 5fa4d6ef961a63e5403560eb3ad16406cdd910c8 Mon Sep 17 00:00:00 2001 From: ChrDek Date: Mon, 9 Oct 2023 15:51:11 +0300 Subject: [PATCH 16/20] Delete Maths/test/TriangularNumberCheck.test.js --- Maths/test/TriangularNumberCheck.test.js | 39 ------------------------ 1 file changed, 39 deletions(-) delete mode 100644 Maths/test/TriangularNumberCheck.test.js diff --git a/Maths/test/TriangularNumberCheck.test.js b/Maths/test/TriangularNumberCheck.test.js deleted file mode 100644 index 7ee9b3f892..0000000000 --- a/Maths/test/TriangularNumberCheck.test.js +++ /dev/null @@ -1,39 +0,0 @@ -import { TriangularNumberCheck } from '../TriangularNumberCheck' - -describe.each([ - { inputVal:55, expectedVal:10 }, - { inputVal:45, expectedVal:9 }, - { inputVal:190, expectedVal:19 }, - { inputVal:66, expectedVal:11 }, - { inputVal:666, expectedVal:36 }, - { inputVal:6, expectedVal:3 }, - { inputVal:10, expectedVal:4 }, - { inputVal:1, expectedVal:1 }, -])('If the number of $inputVal is triangular', ({inputVal, expectedVal}) => { - test(`should return ${expectedVal}`, () => { - expect(TriangularNumberCheck(inputVal)).toBe(expectedVal) - }) -}) - -describe.each([ - { inputVal:-1, expectedVal:-1 }, - { inputVal:-10, expectedVal:-1 }, - { inputVal:-9, expectedVal:-1 }, - { inputVal:"foo", expectedVal:-1 }, - { inputVal:"fizz", expectedVal:-1 }, -])('If the number of $inputVal is completely wrong or negative', ({inputVal, expectedVal}) => { - test(`should return ${expectedVal}`, () => { - expect(TriangularNumberCheck(inputVal)).not.toBeLessThan(-1) - }) -}) - -describe.each([ - { inputVal:4183059834009, expectedVal:0 }, - { inputVal:69, expectedVal:0 }, - { inputVal:0, expectedVal:0 }, - { inputVal:23, expectedVal:0 } -])('If the number of $inputVal is NOT triangular', ({inputVal, expectedVal}) => { - test(`should return ${expectedVal}`, () => { - expect(TriangularNumberCheck(inputVal)).toBe(0) - }) -}) From a35ce8d8b18834d430c08269e78b97f96900ad4c Mon Sep 17 00:00:00 2001 From: ChrDek Date: Mon, 9 Oct 2023 16:37:43 +0300 Subject: [PATCH 17/20] Create HammingWeight.js Changes in name of algorithm. --- Bit-Manipulation/HammingWeight.js | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Bit-Manipulation/HammingWeight.js diff --git a/Bit-Manipulation/HammingWeight.js b/Bit-Manipulation/HammingWeight.js new file mode 100644 index 0000000000..c55ee7a0a3 --- /dev/null +++ b/Bit-Manipulation/HammingWeight.js @@ -0,0 +1,40 @@ +/** + * github author: chrdek + * license: GPL-3.0 or later + * + * @param {number} b = integer or, binary representation of it + * + * The following code generates the "hamming weight" for a binary sequence + * of 32-bit integer values, producing the relevant integer code for it. + * + * Returns the overall of bit count population for values up to 65535, inclusive. + * + * This algorithm utilizes minimum amount of byte space reads and can be very easily + * extended to its 64-bit counterpart. + * + **/ + +function HammingWeight(b) { + //preallocate total number of bytes to count the bits population from + let bytes = new Array(65536); + + //count the bit allocation of the binary sequence by shift in place of the resulting bits + //can be used with xor as well. + const bitAlloc = (bin) => { + let counter = 0; + while (bin > 0) { + counter += bin & 1; + bin >>=1; + } + return counter; + } + + //count all 1-bits from entire bit set + for (let k=0; k < 65536; k++) + bytes[k] = bitAlloc(k); + + //perform bit shifting for integer values for bit-populated result + return bytes[b & 0xFFFF] + bytes[b >> 16]; +} + +export { HammingWeight } From 7004544f9f1727cc47fbc4c37f3556fc344fac74 Mon Sep 17 00:00:00 2001 From: ChrDek Date: Mon, 9 Oct 2023 16:38:33 +0300 Subject: [PATCH 18/20] Create HammingWeight.test.js Additional tests and other name changes. --- Bit-Manipulation/test/HammingWeight.test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Bit-Manipulation/test/HammingWeight.test.js diff --git a/Bit-Manipulation/test/HammingWeight.test.js b/Bit-Manipulation/test/HammingWeight.test.js new file mode 100644 index 0000000000..117cbc084c --- /dev/null +++ b/Bit-Manipulation/test/HammingWeight.test.js @@ -0,0 +1,14 @@ +import { HammingWeight } from '../HammingWeight' + +describe.each([ + { inputVal:Number(117), expectedVal: 5 }, + { inputVal:parseInt(0x9F9,16), expectedVal: 7 }, + { inputVal:parseInt(0x889193,16), expectedVal: 10 }, + { inputVal:10043091, expectedVal: 14}, + { inputVal:parseInt("1101110",2), expectedVal: 5 }, + { inputVal:parseInt(1101110,2), expectedVal: 5 } +])('Resulting code checks from $inputVal', ({inputVal, expectedVal}) => { + test(`returns ${expectedVal}`, () => { + expect(HammingWeight(inputVal)).toBe(expectedVal) + }) +}) From a288dc837799d731a6e229d7b522a40bd63c52d8 Mon Sep 17 00:00:00 2001 From: ChrDek Date: Mon, 9 Oct 2023 16:39:10 +0300 Subject: [PATCH 19/20] Delete Bit-Manipulation/test/HammingCode.test.js --- Bit-Manipulation/test/HammingCode.test.js | 14 -------------- 1 file changed, 14 deletions(-) delete mode 100644 Bit-Manipulation/test/HammingCode.test.js diff --git a/Bit-Manipulation/test/HammingCode.test.js b/Bit-Manipulation/test/HammingCode.test.js deleted file mode 100644 index 9f8d43135b..0000000000 --- a/Bit-Manipulation/test/HammingCode.test.js +++ /dev/null @@ -1,14 +0,0 @@ -import { HammingCode } from '../HammingCode' - -describe.each([ - { inputVal:Number(117), expectedVal: 5 }, - { inputVal:parseInt(0x9F9,16), expectedVal: 7 }, - { inputVal:parseInt(0x889193,16), expectedVal: 10 }, - { inputVal:10043091, expectedVal: 14}, - { inputVal:parseInt("1101110",2), expectedVal: 5 }, - { inputVal:parseInt(1101110,2), expectedVal: 5 } -])('Resulting hamming code conversion from $inputVal', ({inputVal, expectedVal}) => { - test(`returns bit count = ${expectedVal}`, () => { - expect(HammingCode(inputVal)).toBe(expectedVal) - }) -}) From 2f0fa72abca30e82684bd8d6c0b37b2391e46011 Mon Sep 17 00:00:00 2001 From: ChrDek Date: Mon, 9 Oct 2023 16:39:24 +0300 Subject: [PATCH 20/20] Delete Bit-Manipulation/HammingCode.js --- Bit-Manipulation/HammingCode.js | 34 --------------------------------- 1 file changed, 34 deletions(-) delete mode 100644 Bit-Manipulation/HammingCode.js diff --git a/Bit-Manipulation/HammingCode.js b/Bit-Manipulation/HammingCode.js deleted file mode 100644 index 585e654d5c..0000000000 --- a/Bit-Manipulation/HammingCode.js +++ /dev/null @@ -1,34 +0,0 @@ -/** - * github author: chrdek - * license: GPL-3.0 or later - * - * @param {number} b = integer or, binary representation of it - * - * The following code generates the hamming code for a binary sequence - * of 32-bit integer values (incl. parity bit check) - * - * Returns the overall of bit count population for values up to 65535, inclusive - * - **/ - -function HammingCode(b) { - //preallocate total number of integers to count the bits population from. - let bytes = new Array(65536); - const bitAlloc = (bin) => { - let counter = 0; - while (bin > 0) { - counter += bin & 1; - bin >>=1; - } - return counter; - } - -//count all 1-bits from entire bit set -for (let k=0; k < 65536; k++) -bytes[k] = bitAlloc(k); - -//perform bit shifting for integer values for bit-populated result -return bytes[b & 0xFFFF] + bytes[b >> 16]; -} - -export { HammingCode }