forked from MIT-Emerging-Talent/ET6-practice-code-review
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some changes to decimal_to_binary files
- Loading branch information
1 parent
ef51d18
commit 90a3030
Showing
2 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |