mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
Consolidate decimal to binary iterative and recursive (#8999)
* updating DIRECTORY.md * refactor(decimal-to-binary): Consolidate implementations * updating DIRECTORY.md * refactor(decimal-to-binary): Rename main and helper recursive --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
1984d97171
commit
1210559deb
|
@ -146,7 +146,6 @@
|
|||
* [Convert Number To Words](conversions/convert_number_to_words.py)
|
||||
* [Decimal To Any](conversions/decimal_to_any.py)
|
||||
* [Decimal To Binary](conversions/decimal_to_binary.py)
|
||||
* [Decimal To Binary Recursion](conversions/decimal_to_binary_recursion.py)
|
||||
* [Decimal To Hexadecimal](conversions/decimal_to_hexadecimal.py)
|
||||
* [Decimal To Octal](conversions/decimal_to_octal.py)
|
||||
* [Energy Conversions](conversions/energy_conversions.py)
|
||||
|
|
|
@ -1,27 +1,27 @@
|
|||
"""Convert a Decimal Number to a Binary Number."""
|
||||
|
||||
|
||||
def decimal_to_binary(num: int) -> str:
|
||||
def decimal_to_binary_iterative(num: int) -> str:
|
||||
"""
|
||||
Convert an Integer Decimal Number to a Binary Number as str.
|
||||
>>> decimal_to_binary(0)
|
||||
>>> decimal_to_binary_iterative(0)
|
||||
'0b0'
|
||||
>>> decimal_to_binary(2)
|
||||
>>> decimal_to_binary_iterative(2)
|
||||
'0b10'
|
||||
>>> decimal_to_binary(7)
|
||||
>>> decimal_to_binary_iterative(7)
|
||||
'0b111'
|
||||
>>> decimal_to_binary(35)
|
||||
>>> decimal_to_binary_iterative(35)
|
||||
'0b100011'
|
||||
>>> # negatives work too
|
||||
>>> decimal_to_binary(-2)
|
||||
>>> decimal_to_binary_iterative(-2)
|
||||
'-0b10'
|
||||
>>> # other floats will error
|
||||
>>> decimal_to_binary(16.16) # doctest: +ELLIPSIS
|
||||
>>> decimal_to_binary_iterative(16.16) # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: 'float' object cannot be interpreted as an integer
|
||||
>>> # strings will error as well
|
||||
>>> decimal_to_binary('0xfffff') # doctest: +ELLIPSIS
|
||||
>>> decimal_to_binary_iterative('0xfffff') # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
TypeError: 'str' object cannot be interpreted as an integer
|
||||
|
@ -52,7 +52,58 @@ def decimal_to_binary(num: int) -> str:
|
|||
return "0b" + "".join(str(e) for e in binary)
|
||||
|
||||
|
||||
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))}"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
||||
print(decimal_to_binary_recursive(input("Input a decimal number: ")))
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
def binary_recursive(decimal: int) -> str:
|
||||
"""
|
||||
Take a positive integer value and return its binary equivalent.
|
||||
>>> binary_recursive(1000)
|
||||
'1111101000'
|
||||
>>> binary_recursive("72")
|
||||
'1001000'
|
||||
>>> binary_recursive("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 binary_recursive(div) + str(mod)
|
||||
|
||||
|
||||
def main(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.
|
||||
>>> main(0)
|
||||
'0b0'
|
||||
>>> main(40)
|
||||
'0b101000'
|
||||
>>> main(-40)
|
||||
'-0b101000'
|
||||
>>> main(40.8)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Input value is not an integer
|
||||
>>> main("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{binary_recursive(int(number))}"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from doctest import testmod
|
||||
|
||||
testmod()
|
Loading…
Reference in New Issue
Block a user