Update 06-binary-converter/binary-converter.py

This commit is contained in:
William Mantly 2023-09-24 21:03:02 +00:00
parent 9a4c854521
commit fd1b456a08

View File

@ -1,16 +1,23 @@
def binary_to_decimal(num): def binary_to_decimal(num):
pass if num == 0: return 0
decimal = 0
t = [decimal += 2**i for i, char in enumerate(str(num)[:: -1]) if char != '0']
return int(decimal)
def decimal_to_binary(num): def decimal_to_binary(num):
pass pass
print(binary_to_decimal(11100101),binary_to_decimal(10001101),binary_to_decimal(10010000))
assert binary_to_decimal(0) == 0, "0 to decimal should return 0" assert binary_to_decimal(0) == 0, "0 to decimal should return 0"
assert binary_to_decimal(1011) == 11, "1011 to decimal should return 11" assert binary_to_decimal(1011) == 11, "1011 to decimal should return 11"
assert binary_to_decimal(101011) == 43, "101011 to decimal should return 43" assert binary_to_decimal(101011) == 43, "101011 to decimal should return 43"
assert binary_to_decimal(101011101) == 349, "101011101 to decimal should return 349" assert binary_to_decimal(101011101) == 349, "101011101 to decimal should return 349"
assert decimal_to_binary(0) == 0, "0 to binary should return 0" # assert decimal_to_binary(0) == 0, "0 to binary should return 0"
assert decimal_to_binary(5) == 101, "5 to binary should return 101" # assert decimal_to_binary(5) == 101, "5 to binary should return 101"
assert decimal_to_binary(10) == 1010, "10 to binary should return 1010" # assert decimal_to_binary(10) == 1010, "10 to binary should return 1010"
assert decimal_to_binary(113) == 1110001, "113 to binary should return 1110001" # assert decimal_to_binary(113) == 1110001, "113 to binary should return 1110001"