-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat: added Gray Code algorithm #1425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f21e1c4
feat:added Gray Code algorithm
Harshdev098 75d7649
Changes made in GrayCodes.js
Harshdev098 e798afc
Added more test cases in GrayCode.test.js
Harshdev098 630d318
The return value is a `number[]`, not a `string[]`
appgurueu f0982bd
changes made in test cases
Harshdev098 7374111
Make test use `describe / each`, use binary literals
appgurueu 04510e0
added some comments and resources
Harshdev098 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* 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) { | ||
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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { generateGrayCodes } from '../GrayCodes.js' | ||
|
||
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) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.