2019-07-05 08:36:48 +00:00
|
|
|
"""Convert a Decimal Number to a Binary Number."""
|
|
|
|
|
|
|
|
|
2023-08-21 13:25:20 +00:00
|
|
|
def decimal_to_binary_iterative(num: int) -> str:
|
2019-08-03 18:00:10 +00:00
|
|
|
"""
|
2020-09-10 08:31:26 +00:00
|
|
|
Convert an Integer Decimal Number to a Binary Number as str.
|
2023-08-21 13:25:20 +00:00
|
|
|
>>> decimal_to_binary_iterative(0)
|
2020-09-10 08:31:26 +00:00
|
|
|
'0b0'
|
2023-08-21 13:25:20 +00:00
|
|
|
>>> decimal_to_binary_iterative(2)
|
2020-09-10 08:31:26 +00:00
|
|
|
'0b10'
|
2023-08-21 13:25:20 +00:00
|
|
|
>>> decimal_to_binary_iterative(7)
|
2020-09-10 08:31:26 +00:00
|
|
|
'0b111'
|
2023-08-21 13:25:20 +00:00
|
|
|
>>> decimal_to_binary_iterative(35)
|
2020-09-10 08:31:26 +00:00
|
|
|
'0b100011'
|
|
|
|
>>> # negatives work too
|
2023-08-21 13:25:20 +00:00
|
|
|
>>> decimal_to_binary_iterative(-2)
|
2020-09-10 08:31:26 +00:00
|
|
|
'-0b10'
|
|
|
|
>>> # other floats will error
|
2023-08-21 13:25:20 +00:00
|
|
|
>>> decimal_to_binary_iterative(16.16) # doctest: +ELLIPSIS
|
2020-09-10 08:31:26 +00:00
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
...
|
2020-09-10 08:31:26 +00:00
|
|
|
TypeError: 'float' object cannot be interpreted as an integer
|
|
|
|
>>> # strings will error as well
|
2023-08-21 13:25:20 +00:00
|
|
|
>>> decimal_to_binary_iterative('0xfffff') # doctest: +ELLIPSIS
|
2020-09-10 08:31:26 +00:00
|
|
|
Traceback (most recent call last):
|
2022-10-27 17:42:30 +00:00
|
|
|
...
|
2020-09-10 08:31:26 +00:00
|
|
|
TypeError: 'str' object cannot be interpreted as an integer
|
2019-08-03 18:00:10 +00:00
|
|
|
"""
|
|
|
|
|
2021-04-04 13:25:49 +00:00
|
|
|
if isinstance(num, float):
|
2019-08-03 18:00:10 +00:00
|
|
|
raise TypeError("'float' object cannot be interpreted as an integer")
|
2021-04-04 13:25:49 +00:00
|
|
|
if isinstance(num, str):
|
2019-08-03 18:00:10 +00:00
|
|
|
raise TypeError("'str' object cannot be interpreted as an integer")
|
|
|
|
|
|
|
|
if num == 0:
|
|
|
|
return "0b0"
|
|
|
|
|
|
|
|
negative = False
|
|
|
|
|
|
|
|
if num < 0:
|
|
|
|
negative = True
|
|
|
|
num = -num
|
|
|
|
|
2021-04-04 13:25:49 +00:00
|
|
|
binary: list[int] = []
|
2019-07-05 08:36:48 +00:00
|
|
|
while num > 0:
|
|
|
|
binary.insert(0, num % 2)
|
|
|
|
num >>= 1
|
|
|
|
|
2019-08-03 18:00:10 +00:00
|
|
|
if negative:
|
|
|
|
return "-0b" + "".join(str(e) for e in binary)
|
2019-07-05 08:36:48 +00:00
|
|
|
|
2019-08-03 18:00:10 +00:00
|
|
|
return "0b" + "".join(str(e) for e in binary)
|
2019-07-05 08:36:48 +00:00
|
|
|
|
|
|
|
|
2023-08-21 13:25:20 +00:00
|
|
|
def decimal_to_binary_recursive_helper(decimal: int) -> str:
|
|
|
|
"""
|
|
|
|
Take a positive integer value and return its binary equivalent.
|
|
|
|
>>> decimal_to_binary_recursive_helper(1000)
|
|
|
|
'1111101000'
|
|
|
|
>>> decimal_to_binary_recursive_helper("72")
|
|
|
|
'1001000'
|
|
|
|
>>> decimal_to_binary_recursive_helper("number")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: invalid literal for int() with base 10: 'number'
|
|
|
|
"""
|
|
|
|
decimal = int(decimal)
|
|
|
|
if decimal in (0, 1): # Exit cases for the recursion
|
|
|
|
return str(decimal)
|
|
|
|
div, mod = divmod(decimal, 2)
|
|
|
|
return decimal_to_binary_recursive_helper(div) + str(mod)
|
|
|
|
|
|
|
|
|
|
|
|
def decimal_to_binary_recursive(number: str) -> str:
|
|
|
|
"""
|
|
|
|
Take an integer value and raise ValueError for wrong inputs,
|
|
|
|
call the function above and return the output with prefix "0b" & "-0b"
|
|
|
|
for positive and negative integers respectively.
|
|
|
|
>>> decimal_to_binary_recursive(0)
|
|
|
|
'0b0'
|
|
|
|
>>> decimal_to_binary_recursive(40)
|
|
|
|
'0b101000'
|
|
|
|
>>> decimal_to_binary_recursive(-40)
|
|
|
|
'-0b101000'
|
|
|
|
>>> decimal_to_binary_recursive(40.8)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Input value is not an integer
|
|
|
|
>>> decimal_to_binary_recursive("forty")
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: Input value is not an integer
|
|
|
|
"""
|
|
|
|
number = str(number).strip()
|
|
|
|
if not number:
|
|
|
|
raise ValueError("No input value was provided")
|
|
|
|
negative = "-" if number.startswith("-") else ""
|
|
|
|
number = number.lstrip("-")
|
|
|
|
if not number.isnumeric():
|
|
|
|
raise ValueError("Input value is not an integer")
|
|
|
|
return f"{negative}0b{decimal_to_binary_recursive_helper(int(number))}"
|
|
|
|
|
|
|
|
|
2019-08-03 18:00:10 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2019-08-03 18:00:10 +00:00
|
|
|
doctest.testmod()
|
2023-08-21 13:25:20 +00:00
|
|
|
|
|
|
|
print(decimal_to_binary_recursive(input("Input a decimal number: ")))
|