forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added decimal_to_binary_recursion.py (TheAlgorithms#3266)
* Added decimal_to_binary_recursion.py * Added decimal_to_binary_recursion.py * Made changes to docstring * Use divmod() * binary_recursive(div) + str(mod) * Be kind with user input if possible * Update decimal_to_binary_recursion.py * ValueError: invalid literal for int() with base 10: 'number' Co-authored-by: Christian Clauss <[email protected]>
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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() |