Skip to content

Commit

Permalink
Refactor: Added type annotations, improved docstrings, and simplified…
Browse files Browse the repository at this point in the history
… test case
  • Loading branch information
Khusro-S committed Jan 1, 2025
1 parent e9128f8 commit 35a0464
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions solutions/binary_to_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
Module contents:
- binary_to_decimal: converts binary numbers to decimal.
Created on XX XX XX
Created on 29/12/2024
@author: Khusro Sakhi
"""


def binary_to_decimal(binary_str) -> int:
def binary_to_decimal(binary_str: str) -> int:
"""
Converts a binary number represented as a string into its decimal equivalent.
Expand Down
11 changes: 5 additions & 6 deletions solutions/tests/test_binary_to_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# -*- coding: utf-8 -*-
"""
Test module for binary_to_decimal function.
Contains intentionally buggy tests for debugging practice.
Test categories:
- Standard cases: Typical binary numbers with various lengths and values.
Expand All @@ -12,7 +11,7 @@
Created on XX XX XX
Created on 29/12/2024
@author: Khusro Sakhi
"""
Expand Down Expand Up @@ -59,10 +58,10 @@ def test_leading_zeros(self):
# Large number test
def test_large_binary_number(self):
"""It should correctly convert a very large binary number."""
large_binary = "1" * 50 # A binary number with 50 ones (111...111)
# Compute the expected decimal using Python's int function
expected_decimal = int(large_binary, 2)
self.assertEqual(binary_to_decimal(large_binary), expected_decimal)
self.assertEqual(
binary_to_decimal("11111111111111111111111111111111111111111111111111"),
1125899906842623,
)

# Defensive tests
def test_invalid_characters(self):
Expand Down

0 comments on commit 35a0464

Please sign in to comment.