Skip to content

Commit

Permalink
Refactor: Modified input validation and updated input handling in the…
Browse files Browse the repository at this point in the history
… function.

- Replaced assert statements with specific error handling (TypeError and ValueError).
- Updated test cases to check for the new errors:
-- TypeError for non-integer inputs (string, None, float).
-- ValueError for negative year inputs.
  • Loading branch information
Khusro-S committed Jan 5, 2025
1 parent 24ad85f commit 718d468
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
14 changes: 6 additions & 8 deletions solutions/binary_to_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ def binary_to_decimal(binary_str: str) -> int:
The decimal equivalent of the binary number as an integer
Raises:
AssertionError: If the input is a float.
AssertionError: If the input is None.
AssertionError: If the input is not a string.
AssertionError: If the input contains non-binary characters.
TypeError: If the input is not a string or None.
ValueError: If the input contains non-binary characters.
>>> binary_to_decimal ("1010")
10
Expand All @@ -37,12 +35,12 @@ def binary_to_decimal(binary_str: str) -> int:
4
"""

assert isinstance(binary_str, str), "Entered value is not a string"
if not isinstance(binary_str, str):
raise TypeError("Entered value is not a string.")
if not binary_str:
raise ValueError("Input binary string cannot be empty.")
assert all(
bit in "01" for bit in binary_str
), "Entered string contains non-binary characters"
if not all(bit in "01" for bit in binary_str):
raise ValueError("Entered string contains non-binary characters.")

decimal = 0
length = len(binary_str)
Expand Down
16 changes: 8 additions & 8 deletions solutions/tests/test_binary_to_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,23 @@ def test_large_binary_number(self):

# Defensive tests
def test_invalid_characters(self):
"""It should raise an AssertionError for non-binary characters."""
with self.assertRaises(AssertionError):
"""It should raise a ValueError for non-binary characters."""
with self.assertRaises(ValueError):
binary_to_decimal("10A01")

def test_non_string_input(self):
"""It should raise an AssertionError for non-string input."""
with self.assertRaises(AssertionError):
"""It should raise a TypeError for non-string input."""
with self.assertRaises(TypeError):
binary_to_decimal(1010)

def test_none_input(self):
"""It should raise an AssertionError for None input."""
with self.assertRaises(AssertionError):
"""It should raise a TypeError for None input."""
with self.assertRaises(TypeError):
binary_to_decimal(None)

def test_float_input(self):
"""It should raise an AssertionError for a float input."""
with self.assertRaises(AssertionError):
"""It should raise a TypeError for a float input."""
with self.assertRaises(TypeError):
binary_to_decimal(10.1)


Expand Down

0 comments on commit 718d468

Please sign in to comment.