feat(conversions): Add Binary and Gray Code conversion algorithms#13634
Open
JDeep1234 wants to merge 1 commit intoTheAlgorithms:masterfrom
Open
feat(conversions): Add Binary and Gray Code conversion algorithms#13634JDeep1234 wants to merge 1 commit intoTheAlgorithms:masterfrom
JDeep1234 wants to merge 1 commit intoTheAlgorithms:masterfrom
Conversation
- Implement binary_to_gray() for converting binary to Gray Code - Implement gray_to_binary() for converting Gray Code to binary - Implement decimal_to_gray() for decimal to Gray Code string - Implement gray_to_decimal() for Gray Code string to decimal - Add comprehensive doctests (39 tests, all passing) - Add type hints for all function parameters and returns - Add input validation with proper error handling - Include Wikipedia reference and detailed documentation - Demonstrate practical use cases with example table Gray Code is a binary numeral system where successive values differ in only one bit, useful for error correction, digital communications, and position encoders.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
This PR adds comprehensive Binary Gray Code conversion utilities to the conversions module.
Gray Code (also known as reflected binary code) is a binary numeral system where two successive values differ in only one bit. This property makes it particularly useful in:
What's Added
New File:
conversions/binary_to_gray_code.py4 Functions implemented:
binary_to_gray(binary_number: int) -> intgray = binary XOR (binary >> 1)gray_to_binary(gray_number: int) -> intdecimal_to_gray(decimal_number: int) -> strgray_to_decimal(gray_string: str) -> intFeatures
Comprehensive Testing
Code Quality
ruff check(no linting errors)python -m doctest -v(39/39 tests pass)Documentation
__main__Error Handling
Example Usage
`python
from conversions.binary_to_gray_code import binary_to_gray, gray_to_binary
Binary to Gray Code
binary_to_gray(10) # Returns: 15 (binary 1010 Gray 1111)
Gray Code to Binary
gray_to_binary(15) # Returns: 10 (Gray 1111 binary 1010)
Decimal to Gray Code string
decimal_to_gray(7) # Returns: '100' (decimal 7 Gray Code 100)
Gray Code string to Decimal
gray_to_decimal('100') # Returns: 7
`
Verification Table
Checklist
ruff checkbinary_to_gray_code.pyRelated Issues
This adds a new algorithm that was not previously in the repository. Gray Code conversion is a fundamental algorithm in digital systems and complements the existing conversion utilities.
Hacktoberfest 2025 Contribution