Sunday, 15 December 2024

Day 39: Python Program to Convert Gray to binary Code

 


def gray_to_binary(gray_str):

    """

    Function to convert a Gray Code to its equivalent Binary Code.

    :param gray_str: The input Gray Code string.

    :return: The converted Binary Code string.

    """

    binary_code = gray_str[0]

    for i in range(1, len(gray_str)):

        binary_bit = str(int(binary_code[i - 1]) ^ int(gray_str[i]))

        binary_code += binary_bit  

    return binary_code

if __name__ == "__main__":

    gray_input = input("Enter a Gray Code to convert into Binary Code: ")

     if all(bit in '01' for bit in gray_input):

        result = gray_to_binary(gray_input)

        

        print(f"Gray Code: {gray_input}")

        print(f"Corresponding Binary Code: {result}")

    else:

        print("Invalid input. Please enter a valid Gray Code consisting only of 0's and 1's.")

        #source code --> clcoding.com 

Code Explanation:

1. Function: gray_to_binary
Purpose
This function takes a Gray Code string as input and converts it into the corresponding Binary Code.

Parameters
gray_str: A string representing the Gray Code (e.g., "1011").
Logic

First Bit:
In Binary Code, the first bit is always the same as the first bit of the Gray Code:
binary_code = gray_str[0]

Subsequent Bits:
For each subsequent bit, the binary bit is calculated using the XOR operation between:
The last calculated binary bit (binary_code[i - 1]).
The current bit in the Gray Code (gray_str[i]).
Formula:
binary_bit = str(int(binary_code[i - 1]) ^ int(gray_str[i]))
int() converts the binary characters into integers.

^ performs the XOR operation.
str() converts the result back to a string for concatenation.

Concatenate the Binary Bit:
Append the computed binary bit to binary_code:

binary_code += binary_bit

Return Result:
After processing all bits, return the final Binary Code string.

Example Walkthrough:
For gray_str = "1011":
binary_code[0] = gray_str[0] = 1
Iteration 1 (i = 1):
binary_bit = binary_code[0] XOR gray_str[1] = 1 XOR 0 = 1
binary_code = "11"
Iteration 2 (i = 2):
binary_bit = binary_code[1] XOR gray_str[2] = 1 XOR 1 = 0
binary_code = "110"
Iteration 3 (i = 3):
binary_bit = binary_code[2] XOR gray_str[3] = 0 XOR 1 = 1
binary_code = "1101"
Final Binary Code: "1101"

2. Input Handling
The program prompts the user for a Gray Code:

gray_input = input("Enter a Gray Code to convert into Binary Code: ")

Validation
The program ensures the input contains only 0 and 1:
if all(bit in '01' for bit in gray_input):
all(bit in '01' for bit in gray_input):
Checks if every character in gray_input is either '0' or '1'.

If valid:
Calls the gray_to_binary function to compute the Binary Code.
Prints the input Gray Code and the corresponding Binary Code.
If invalid:
Displays an error message:
print("Invalid input. Please enter a valid Gray Code consisting only of 0's and 1's.")

3. Output
Valid Input:
For example:
Enter a Gray Code to convert into Binary Code: 1011
Gray Code: 1011
Corresponding Binary Code: 1101
Invalid Input:
If the user enters invalid characters:
Enter a Gray Code to convert into Binary Code: 12A
Invalid input. Please enter a valid Gray Code consisting only of 0's and 1's.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (41) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (225) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (925) Python Coding Challenge (343) Python Quiz (12) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses