Skip to content

Commit bee2e01

Browse files
committed
fix: reject empty negative binary input
1 parent 574d100 commit bee2e01

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

‎conversions/binary_to_decimal.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ def bin_to_decimal(bin_string: str) -> int:
1818
Traceback (most recent call last):
1919
...
2020
ValueError: Empty string was passed to the function
21+
>>> bin_to_decimal("-")
22+
Traceback (most recent call last):
23+
...
24+
ValueError: Non-binary value was passed to the function
25+
>>> bin_to_decimal(" - ")
26+
Traceback (most recent call last):
27+
...
28+
ValueError: Non-binary value was passed to the function
2129
>>> bin_to_decimal("39")
2230
Traceback (most recent call last):
2331
...
@@ -29,7 +37,7 @@ def bin_to_decimal(bin_string: str) -> int:
2937
is_negative = bin_string[0] == "-"
3038
if is_negative:
3139
bin_string = bin_string[1:]
32-
if not all(char in "01" for char in bin_string):
40+
if not bin_string or not all(char in "01" for char in bin_string):
3341
raise ValueError("Non-binary value was passed to the function")
3442
decimal_number = 0
3543
for char in bin_string:

0 commit comments

Comments
 (0)