mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-07 11:37:36 +08:00
Renamed files and fixed Doctest (#2421)
* * Renamed files * Fiexed doctest * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
43
conversions/binary_to_decimal.py
Normal file
43
conversions/binary_to_decimal.py
Normal file
@ -0,0 +1,43 @@
|
||||
def bin_to_decimal(bin_string: str) -> int:
|
||||
"""
|
||||
Convert a binary value to its decimal equivalent
|
||||
|
||||
>>> bin_to_decimal("101")
|
||||
5
|
||||
>>> bin_to_decimal(" 1010 ")
|
||||
10
|
||||
>>> bin_to_decimal("-11101")
|
||||
-29
|
||||
>>> bin_to_decimal("0")
|
||||
0
|
||||
>>> bin_to_decimal("a")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Non-binary value was passed to the function
|
||||
>>> bin_to_decimal("")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Empty string was passed to the function
|
||||
>>> bin_to_decimal("39")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Non-binary value was passed to the function
|
||||
"""
|
||||
bin_string = str(bin_string).strip()
|
||||
if not bin_string:
|
||||
raise ValueError("Empty string was passed to the function")
|
||||
is_negative = bin_string[0] == "-"
|
||||
if is_negative:
|
||||
bin_string = bin_string[1:]
|
||||
if not all(char in "01" for char in bin_string):
|
||||
raise ValueError("Non-binary value was passed to the function")
|
||||
decimal_number = 0
|
||||
for char in bin_string:
|
||||
decimal_number = 2 * decimal_number + int(char)
|
||||
return -decimal_number if is_negative else decimal_number
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from doctest import testmod
|
||||
|
||||
testmod()
|
Reference in New Issue
Block a user