From f21e1c4703caea91d94b6ec2fa622a1048c36b33 Mon Sep 17 00:00:00 2001 From: Harsh Dev Pathak Date: Wed, 4 Oct 2023 11:27:53 +0530 Subject: [PATCH 1/7] feat:added Gray Code algorithm --- Bit-Manipulation/GrayCodes.js | 19 +++++++++++++++++++ Bit-Manipulation/test/GrayCodes.test.js | 8 ++++++++ 2 files changed, 27 insertions(+) create mode 100644 Bit-Manipulation/GrayCodes.js create mode 100644 Bit-Manipulation/test/GrayCodes.test.js diff --git a/Bit-Manipulation/GrayCodes.js b/Bit-Manipulation/GrayCodes.js new file mode 100644 index 0000000000..4d607cf678 --- /dev/null +++ b/Bit-Manipulation/GrayCodes.js @@ -0,0 +1,19 @@ +// To generate Gray codes using bit manipulation in JavaScript, we can create a function that takes an integer n as input and returns an array of Gray codes up to 2^n - 1 +function generateGrayCodes(n) { + if (n <= 0) { + return [0] + } + + const grayCodes = [0, 1] + + for (let i = 1; i < n; i++) { + const highestBit = 1 << i + for (let j = grayCodes.length - 1; j >= 0; j--) { + grayCodes.push(highestBit | grayCodes[j]) + } + } + + return grayCodes +} + +export { generateGrayCodes } // Export the function for testing diff --git a/Bit-Manipulation/test/GrayCodes.test.js b/Bit-Manipulation/test/GrayCodes.test.js new file mode 100644 index 0000000000..d711dfcb6e --- /dev/null +++ b/Bit-Manipulation/test/GrayCodes.test.js @@ -0,0 +1,8 @@ +import { generateGrayCodes } from '../GrayCodes.js' // Import the function + +test('Generate Gray codes for n=3', () => { + const n = 3 + const expectedGrayCodes = [0, 1, 3, 2, 6, 7, 5, 4] + const grayCodes = generateGrayCodes(n) + expect(grayCodes).toEqual(expectedGrayCodes) +}) From 75d764988b2e910902d6e49b3833ac12f479c85d Mon Sep 17 00:00:00 2001 From: Harsh Dev Pathak <118347330+Harshdev098@users.noreply.github.com> Date: Wed, 4 Oct 2023 19:29:01 +0530 Subject: [PATCH 2/7] Changes made in GrayCodes.js --- Bit-Manipulation/GrayCodes.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Bit-Manipulation/GrayCodes.js b/Bit-Manipulation/GrayCodes.js index 4d607cf678..5b48b4e792 100644 --- a/Bit-Manipulation/GrayCodes.js +++ b/Bit-Manipulation/GrayCodes.js @@ -1,4 +1,8 @@ -// To generate Gray codes using bit manipulation in JavaScript, we can create a function that takes an integer n as input and returns an array of Gray codes up to 2^n - 1 +/** + * Generates a Gray code sequence for the given number of bits. + * @param {number} n - The number of bits in the Gray code sequence. + * @returns {string[]} - An array of Gray codes in binary format. + */ function generateGrayCodes(n) { if (n <= 0) { return [0] @@ -16,4 +20,4 @@ function generateGrayCodes(n) { return grayCodes } -export { generateGrayCodes } // Export the function for testing +export { generateGrayCodes } From e798afc08c5ca2c9941722d92f17db844e6a9aea Mon Sep 17 00:00:00 2001 From: Harsh Dev Pathak <118347330+Harshdev098@users.noreply.github.com> Date: Wed, 4 Oct 2023 19:31:35 +0530 Subject: [PATCH 3/7] Added more test cases in GrayCode.test.js --- Bit-Manipulation/test/GrayCodes.test.js | 37 ++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/Bit-Manipulation/test/GrayCodes.test.js b/Bit-Manipulation/test/GrayCodes.test.js index d711dfcb6e..b24124a604 100644 --- a/Bit-Manipulation/test/GrayCodes.test.js +++ b/Bit-Manipulation/test/GrayCodes.test.js @@ -1,4 +1,8 @@ -import { generateGrayCodes } from '../GrayCodes.js' // Import the function +import { generateGrayCodes } from '../GrayCodes.js' + +/** + * Test cases for the generateGrayCodes function. + */ test('Generate Gray codes for n=3', () => { const n = 3 @@ -6,3 +10,34 @@ test('Generate Gray codes for n=3', () => { const grayCodes = generateGrayCodes(n) expect(grayCodes).toEqual(expectedGrayCodes) }) +test('Generate Gray codes for n=0', () => { + const n = 0; + const expectedGrayCodes = [0]; + const grayCodes = generateGrayCodes(n); + expect(grayCodes).toEqual(expectedGrayCodes); + }); + + test('Generate Gray codes for n=1', () => { + const n = 1; + const expectedGrayCodes = [0, 1]; + const grayCodes = generateGrayCodes(n); + expect(grayCodes).toEqual(expectedGrayCodes); + }); + + test('Generate Gray codes for n=4', () => { + const n = 4; + const expectedGrayCodes = [ + 0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8 + ]; + const grayCodes = generateGrayCodes(n); + expect(grayCodes).toEqual(expectedGrayCodes); + }); + + test('Generate Gray codes for n=5', () => { + const n = 5; + const expectedGrayCodes = [ + 0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8, 24, 25, 27, 26, 30, 31, 29, 28, 20, 21, 23, 22, 18, 19, 17, 16 + ]; + const grayCodes = generateGrayCodes(n); + expect(grayCodes).toEqual(expectedGrayCodes); + }); From 630d318cab786914f3429a911474af02f91dac5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Wed, 4 Oct 2023 16:35:01 +0200 Subject: [PATCH 4/7] The return value is a `number[]`, not a `string[]` --- Bit-Manipulation/GrayCodes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bit-Manipulation/GrayCodes.js b/Bit-Manipulation/GrayCodes.js index 5b48b4e792..a72f2c7de7 100644 --- a/Bit-Manipulation/GrayCodes.js +++ b/Bit-Manipulation/GrayCodes.js @@ -1,7 +1,7 @@ /** * Generates a Gray code sequence for the given number of bits. * @param {number} n - The number of bits in the Gray code sequence. - * @returns {string[]} - An array of Gray codes in binary format. + * @returns {number[]} - An array of Gray codes in binary format. */ function generateGrayCodes(n) { if (n <= 0) { From f0982bd28c361a82841a780eff912f757657e8b8 Mon Sep 17 00:00:00 2001 From: Harsh Dev Pathak <118347330+Harshdev098@users.noreply.github.com> Date: Wed, 4 Oct 2023 22:10:16 +0530 Subject: [PATCH 5/7] changes made in test cases --- Bit-Manipulation/test/GrayCodes.test.js | 46 ++++++------------------- 1 file changed, 11 insertions(+), 35 deletions(-) diff --git a/Bit-Manipulation/test/GrayCodes.test.js b/Bit-Manipulation/test/GrayCodes.test.js index b24124a604..ee219d3420 100644 --- a/Bit-Manipulation/test/GrayCodes.test.js +++ b/Bit-Manipulation/test/GrayCodes.test.js @@ -3,41 +3,17 @@ import { generateGrayCodes } from '../GrayCodes.js' /** * Test cases for the generateGrayCodes function. */ +const testCases = [ + { n: 0, expected: [0] }, + { n: 1, expected: [0, 1] }, + { n: 2, expected: [0, 1, 3, 2] }, + { n: 3, expected: [0, 1, 3, 2, 6, 7, 5, 4] }, + { n: 4, expected: [0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8] }, +]; -test('Generate Gray codes for n=3', () => { - const n = 3 - const expectedGrayCodes = [0, 1, 3, 2, 6, 7, 5, 4] - const grayCodes = generateGrayCodes(n) - expect(grayCodes).toEqual(expectedGrayCodes) -}) -test('Generate Gray codes for n=0', () => { - const n = 0; - const expectedGrayCodes = [0]; +testCases.forEach(({ n, expected }) => { + test(`Generate Gray codes for n=${n}`, () => { const grayCodes = generateGrayCodes(n); - expect(grayCodes).toEqual(expectedGrayCodes); - }); - - test('Generate Gray codes for n=1', () => { - const n = 1; - const expectedGrayCodes = [0, 1]; - const grayCodes = generateGrayCodes(n); - expect(grayCodes).toEqual(expectedGrayCodes); - }); - - test('Generate Gray codes for n=4', () => { - const n = 4; - const expectedGrayCodes = [ - 0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8 - ]; - const grayCodes = generateGrayCodes(n); - expect(grayCodes).toEqual(expectedGrayCodes); - }); - - test('Generate Gray codes for n=5', () => { - const n = 5; - const expectedGrayCodes = [ - 0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8, 24, 25, 27, 26, 30, 31, 29, 28, 20, 21, 23, 22, 18, 19, 17, 16 - ]; - const grayCodes = generateGrayCodes(n); - expect(grayCodes).toEqual(expectedGrayCodes); + expect(grayCodes).toEqual(expected); }); +}); From 7374111dcd016dc9af633638a659eaa3c34997ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Thu, 5 Oct 2023 01:15:49 +0200 Subject: [PATCH 6/7] Make test use `describe / each`, use binary literals --- Bit-Manipulation/test/GrayCodes.test.js | 36 ++++++++++++------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Bit-Manipulation/test/GrayCodes.test.js b/Bit-Manipulation/test/GrayCodes.test.js index ee219d3420..2e8c46cb8e 100644 --- a/Bit-Manipulation/test/GrayCodes.test.js +++ b/Bit-Manipulation/test/GrayCodes.test.js @@ -1,19 +1,19 @@ -import { generateGrayCodes } from '../GrayCodes.js' +import { generateGrayCodes } from '../GrayCodes.js' -/** - * Test cases for the generateGrayCodes function. - */ -const testCases = [ - { n: 0, expected: [0] }, - { n: 1, expected: [0, 1] }, - { n: 2, expected: [0, 1, 3, 2] }, - { n: 3, expected: [0, 1, 3, 2, 6, 7, 5, 4] }, - { n: 4, expected: [0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8] }, -]; - -testCases.forEach(({ n, expected }) => { - test(`Generate Gray codes for n=${n}`, () => { - const grayCodes = generateGrayCodes(n); - expect(grayCodes).toEqual(expected); - }); -}); +describe('Gray codes', () => { + test.each([ + [0, [0b0]], + [1, [0b0, 0b1]], + [2, [0b00, 0b01, 0b11, 0b10]], + [3, [0b000, 0b001, 0b011, 0b010, 0b110, 0b111, 0b101, 0b100]], + [ + 4, + [ + 0b0000, 0b0001, 0b0011, 0b0010, 0b0110, 0b0111, 0b0101, 0b0100, 0b1100, + 0b1101, 0b1111, 0b1110, 0b1010, 0b1011, 0b1001, 0b1000 + ] + ] + ])('n = %i -> %j', (n, expected) => { + expect(generateGrayCodes(n)).toEqual(expected) + }) +}) From 04510e08b7a12b7ea2f0848fb5d123ddc4e5a88c Mon Sep 17 00:00:00 2001 From: Harsh Dev Pathak <118347330+Harshdev098@users.noreply.github.com> Date: Thu, 5 Oct 2023 16:37:02 +0530 Subject: [PATCH 7/7] added some comments and resources --- Bit-Manipulation/GrayCodes.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Bit-Manipulation/GrayCodes.js b/Bit-Manipulation/GrayCodes.js index a72f2c7de7..c9ce8ecab9 100644 --- a/Bit-Manipulation/GrayCodes.js +++ b/Bit-Manipulation/GrayCodes.js @@ -2,6 +2,25 @@ * Generates a Gray code sequence for the given number of bits. * @param {number} n - The number of bits in the Gray code sequence. * @returns {number[]} - An array of Gray codes in binary format. + * @description + * Gray codes are binary sequences in which two successive values differ in only one bit. + * This function generates a Gray code sequence of length 2^n for the given number of bits. + * + * The algorithm follows these steps: + * + * 1. Initialize an array `grayCodes` to store the Gray codes. Start with [0, 1] for n = 1. + * 2. Iterate from 1 to n: + * a. Calculate `highestBit` as 2^i, where `i` is the current iteration index. + * b. Iterate in reverse order through the existing Gray codes: + * - For each Gray code `code`, add `highestBit | code` to `grayCodes`. + * - This operation flips a single bit in each existing code, creating new codes. + * 3. Return the `grayCodes` array containing the Gray codes in decimal representation. + * + *resources: [GFG](https://www.geeksforgeeks.org/generate-n-bit-gray-codes/) + * @example + * const n = 3; + * const grayCodes = generateGrayCodes(n); + * // grayCodes will be [0, 1, 3, 2, 6, 7, 5, 4] for n=3. */ function generateGrayCodes(n) { if (n <= 0) {