Skip to content

Comments

feat(conversions): Add Binary and Gray Code conversion algorithms#13634

Open
JDeep1234 wants to merge 1 commit intoTheAlgorithms:masterfrom
JDeep1234:feature/add-gray-code-conversion
Open

feat(conversions): Add Binary and Gray Code conversion algorithms#13634
JDeep1234 wants to merge 1 commit intoTheAlgorithms:masterfrom
JDeep1234:feature/add-gray-code-conversion

Conversation

@JDeep1234
Copy link

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:

  • Error correction and detection
  • Digital communications
  • Rotary position encoders
  • Analog-to-digital converters
  • Karnaugh maps in digital logic design

What's Added

New File: conversions/binary_to_gray_code.py

4 Functions implemented:

  1. binary_to_gray(binary_number: int) -> int

    • Convert binary number to Gray Code using XOR operation
    • Formula: gray = binary XOR (binary >> 1)
  2. gray_to_binary(gray_number: int) -> int

    • Convert Gray Code back to binary
    • Uses iterative XOR with right-shifted values
  3. decimal_to_gray(decimal_number: int) -> str

    • Convert decimal to Gray Code binary string representation
    • Combines decimalbinaryGray Code conversion
  4. gray_to_decimal(gray_string: str) -> int

    • Convert Gray Code binary string to decimal
    • Includes binary string validation

Features

Comprehensive Testing

  • 39 doctests covering all functions
  • Tests for valid inputs (0, 1, 2, 4, 7, 10, 15, 255...)
  • Tests for edge cases (0, negative numbers, floats)
  • Tests for error handling (TypeError, ValueError)
  • All tests passing locally

Code Quality

  • Passes ruff check (no linting errors)
  • Passes python -m doctest -v (39/39 tests pass)
  • Python 3.13+ compatible
  • Type hints for all parameters and return values
  • PEP 8 compliant naming conventions

Documentation

  • Detailed docstrings for every function
  • Wikipedia reference included
  • Clear explanation of Gray Code properties
  • Usage examples in doctests
  • Interactive demonstration in __main__

Error Handling

  • Validates input types (must be int for numeric functions)
  • Validates input values (must be non-negative)
  • Validates binary strings (only '0' and '1' allowed)
  • Meaningful error messages for invalid inputs

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

Decimal Binary Gray Code
0 0000 0000
1 0001 0001
2 0010 0011
3 0011 0010
4 0100 0110
5 0101 0111
6 0110 0101
7 0111 0100

Checklist

  • I did my work - no plagiarism
  • Work will be distributed under MIT License
  • Code follows repository styles and standards
  • New implementation (not duplicate of existing code)
  • Python 3.13+ syntax
  • Descriptive function and variable names
  • Python naming conventions (snake_case)
  • Type hints included
  • Comprehensive doctests
  • All doctests pass locally
  • Passes ruff check
  • Wikipedia reference included
  • Proper input validation and error handling
  • File name uses snake_case: binary_to_gray_code.py
  • Fits into existing directory structure (conversions/)
  • Code compiles and runs successfully

Related 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

- 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant