Skip to content

Commit

Permalink
Added some changes to decimal_to_binary files
Browse files Browse the repository at this point in the history
  • Loading branch information
Melat-arch committed Jan 12, 2025
1 parent ef51d18 commit 90a3030
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
36 changes: 36 additions & 0 deletions solutions/decimal_to_binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# solutions/convert_decimal_to_binary.py

"""
This module provides a function to convert a non-negative integer to its binary representation.
Function:
- convert_decimal_to_binary: Return the binary representation of a given non-negative integer.
Author: Melat Assefa
Date: January 6, 2025
"""


def convert_decimal_to_binary(n: int) -> str:
"""
Converts a non-negative integer to its binary representation.
Parameters:
n (int): A non-negative integer to be converted to binary.
Returns:
str: The binary representation of the given integer.
Raises:
ValueError: If n is negative.
Examples:
>>> convert_decimal_to_binary(0)
'0'
>>> convert_decimal_to_binary(5)
'101'
>>> convert_decimal_to_binary(23)
'10111'
"""
if n < 0:
raise ValueError("Input should be a non-negative integer.")
return bin(n)[2:]
63 changes: 63 additions & 0 deletions solutions/tests/test_decimal_to_binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# tests/test_convert_decimal_to_binary.py

"""
Unit tests for the convert_decimal_to_binary function.
This test suite includes:
- Regular cases: Typical inputs that the function is expected to handle.
- Edge cases: Inputs that are at the boundary of what the function should handle.
- Error cases: Input that should raise exceptions due to invalid input.
@author: Melat Assefa
Date: January 6, 2025
"""

import unittest
from solutions.decimal_to_binary import convert_decimal_to_binary


class TestConvertDecimalToBinary(unittest.TestCase):
"""Test cases for the convert_decimal_to_binary function."""

# Regular Cases
def test_binary_of_zero(self):
"""Regular case: Test that the function returns '0' for input 0."""
self.assertEqual(
convert_decimal_to_binary(0),
"0",
"The binary representation of 0 should be '0'.",
)

def test_binary_of_five(self):
"""Regular case: Test that the function returns '101' for input 5."""
self.assertEqual(
convert_decimal_to_binary(5),
"101",
"The binary representation of 5 should be '101'.",
)

def test_binary_of_twenty_three(self):
"""Regular case: Test that the function returns '10111' for input 23."""
self.assertEqual(
convert_decimal_to_binary(23),
"10111",
"The binary representation of 23 should be '10111'.",
)

# Edge Cases
def test_large_number(self):
"""Edge case: Test the function with a large number."""
self.assertEqual(
convert_decimal_to_binary(1023),
"1111111111",
"The binary representation of 1023 should be '1111111111'.",
)

# Error Cases
def test_negative_input_raises_value_error(self):
"""Error case: Test that the function raises a ValueError for negative input."""
with self.assertRaises(ValueError):
convert_decimal_to_binary(-1)


if __name__ == "__main__":
unittest.main()

0 comments on commit 90a3030

Please sign in to comment.