From 99061865879503900137414917ee4dceab7c1aa9 Mon Sep 17 00:00:00 2001 From: Sarthak Joshi <100042684+Craniace@users.noreply.github.com> Date: Fri, 31 Oct 2025 22:45:40 +0530 Subject: [PATCH 1/3] Create Binary_to_gray.py --- conversions/Binary_to_gray.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 conversions/Binary_to_gray.py diff --git a/conversions/Binary_to_gray.py b/conversions/Binary_to_gray.py new file mode 100644 index 000000000000..d1761e48d682 --- /dev/null +++ b/conversions/Binary_to_gray.py @@ -0,0 +1,25 @@ +# Function to convert Binary number to Gray code +def binary_to_gray(binary): + # Convert binary (string) to integer + binary = int(binary, 2) + + # Perform XOR between binary and binary right-shifted by 1 bit + gray = binary ^ (binary >> 1) + + # Convert back to binary string and remove '0b' prefix + gray_code = bin(gray)[2:] + + return gray_code + + +# --- Main Program --- + +# Taking input from the user +binary_input = input("Enter a binary number: ") + +# Input validation +if not all(bit in '01' for bit in binary_input): + print("❌ Invalid input! Please enter a binary number (0s and 1s only).") +else: + gray_result = binary_to_gray(binary_input) + print(f"✅ The Gray code for binary {binary_input} is: {gray_result}") From f0f0692671f0c1d52091d9c9c58089d19466dc16 Mon Sep 17 00:00:00 2001 From: Sarthak Joshi <100042684+Craniace@users.noreply.github.com> Date: Fri, 31 Oct 2025 22:54:25 +0530 Subject: [PATCH 2/3] Update and rename Binary_to_gray.py to binary_to_gray.py --- conversions/Binary_to_gray.py | 25 ------------------ conversions/binary_to_gray.py | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 25 deletions(-) delete mode 100644 conversions/Binary_to_gray.py create mode 100644 conversions/binary_to_gray.py diff --git a/conversions/Binary_to_gray.py b/conversions/Binary_to_gray.py deleted file mode 100644 index d1761e48d682..000000000000 --- a/conversions/Binary_to_gray.py +++ /dev/null @@ -1,25 +0,0 @@ -# Function to convert Binary number to Gray code -def binary_to_gray(binary): - # Convert binary (string) to integer - binary = int(binary, 2) - - # Perform XOR between binary and binary right-shifted by 1 bit - gray = binary ^ (binary >> 1) - - # Convert back to binary string and remove '0b' prefix - gray_code = bin(gray)[2:] - - return gray_code - - -# --- Main Program --- - -# Taking input from the user -binary_input = input("Enter a binary number: ") - -# Input validation -if not all(bit in '01' for bit in binary_input): - print("❌ Invalid input! Please enter a binary number (0s and 1s only).") -else: - gray_result = binary_to_gray(binary_input) - print(f"✅ The Gray code for binary {binary_input} is: {gray_result}") diff --git a/conversions/binary_to_gray.py b/conversions/binary_to_gray.py new file mode 100644 index 000000000000..afc1e6f4935c --- /dev/null +++ b/conversions/binary_to_gray.py @@ -0,0 +1,49 @@ +""" +Binary to Gray code conversion algorithm. + +Reference: +https://en.wikipedia.org/wiki/Gray_code +""" + +def binary_to_gray(binary: str) -> str: + """ + Convert a binary number (as string) to its equivalent Gray code. + + The Gray code is generated by XOR-ing each bit with the bit just before it. + + Args: + binary (str): A string representing a binary number (e.g., "10101010"). + + Returns: + str: The corresponding Gray code string. + + Example: + >>> binary_to_gray("10101010") + '11111111' + + >>> binary_to_gray("1101") + '1011' + """ + # Convert binary string to integer + binary_int = int(binary, 2) + + # XOR the binary number with itself shifted right by 1 bit + gray_int = binary_int ^ (binary_int >> 1) + + # Convert the integer result back to a binary string (remove '0b' prefix) + gray_code = bin(gray_int)[2:] + + # Pad with leading zeros to maintain same bit length as input + return gray_code.zfill(len(binary)) + + +if __name__ == "__main__": + # Take input from user + binary_input = input("Enter a binary number: ").strip() + + # Validate the input (only 0s and 1s allowed) + if not all(bit in "01" for bit in binary_input): + print("❌ Invalid input! Please enter only 0s and 1s.") + else: + result = binary_to_gray(binary_input) + print(f"✅ The Gray code for binary {binary_input} is: {result}") From 2396b8b36f813215acdfb76549569317ccf4050c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 31 Oct 2025 17:27:17 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/binary_to_gray.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conversions/binary_to_gray.py b/conversions/binary_to_gray.py index afc1e6f4935c..f68e3a8832dd 100644 --- a/conversions/binary_to_gray.py +++ b/conversions/binary_to_gray.py @@ -5,6 +5,7 @@ https://en.wikipedia.org/wiki/Gray_code """ + def binary_to_gray(binary: str) -> str: """ Convert a binary number (as string) to its equivalent Gray code.