<reponame>kamil2789/TasksCollection<filename>MathematicalChallenges/10_GrayCode5Bits/Task10.py
from bitarray import bitarray
from bitarray.util import ba2int
from bitarray.util import int2ba


class Number:
    def __init__(self, bits, gray_code, decimal):
        self.bits = bits
        self.gray_code = gray_code
        self.decimal = decimal

    def __repr__(self):
        return f"number:{self.decimal} bits:{self.bits.to01()} "\
            f"gray:{self.gray_code.to01()}"

    def __str__(self):
        return self.__repr__


def calculate_graye_code():
    result = []
    for item in range(0, 32):
        bits = bitarray(int2ba(item))
        gray_code = bitarray(bits >> 1)
        gray_code ^= bits
        result.append(Number(bits, gray_code, ba2int(bits)))
    return result


print("5 Bits Gray Code")
print(calculate_graye_code())
from bitarray import bitarray
from bitarray.util import ba2int
from bitarray.util import int2ba


class Number:
    def __init__(self, bits, gray_code, decimal):
        self.bits = bits
        self.gray_code = gray_code
        self.decimal = decimal

    def __repr__(self):
        return f"number:{self.decimal} bits:{self.bits.to01()} "\
            f"gray:{self.gray_code.to01()}"

    def __str__(self):
        return self.__repr__


def calculate_graye_code():
    result = []
    for item in range(0, 32):
        bits = bitarray(int2ba(item))
        gray_code = bitarray(bits >> 1)
        gray_code ^= bits
        result.append(Number(bits, gray_code, ba2int(bits)))
    return result


print("5 Bits Gray Code")
print(calculate_graye_code())