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.
Merge pull request #52 from MIT-Emerging-Talent/binary_decimal_conver…
…sion Binary decimal conversion
- Loading branch information
Showing
2 changed files
with
182 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,105 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
""" | ||
A module for converting between binary and decimal numbers. | ||
Created by Muna Sattouf on 29/12/2024 | ||
Completed on 1/1/2025 | ||
""" | ||
|
||
|
||
def decimal_to_binary(decimal: int) -> str: | ||
""" | ||
This function takes a decimal number from command line as paramter, | ||
and returns its binary representation. | ||
Example: DecimalToBinary('binary', 392) | ||
>>> 110001000 | ||
""" | ||
|
||
if not isinstance(decimal, int): | ||
raise ValueError | ||
""" | ||
Input must be an integer | ||
""" | ||
if decimal < 0: | ||
decimal = -decimal | ||
return "-" + decimal_to_binary(decimal) | ||
if decimal == 0: | ||
return "0" | ||
binarynumber = "" | ||
while decimal > 0: | ||
binarynumber = str(decimal % 2) + binarynumber | ||
decimal = decimal // 2 | ||
return binarynumber | ||
|
||
|
||
def binary_to_decimal(binary: str) -> int: | ||
""" | ||
This function takes a binary number from command line as paramter, | ||
and returns its decimal representation. | ||
Example: binary_to_decimal('decimal', 10011101) | ||
>>> 157 | ||
Example: binary_to_decimal('decimal', 'hello') | ||
>>> ValueError: Input must only contain 0s and 1s | ||
""" | ||
if not all(c in "01" for c in binary): | ||
raise ValueError("Input must only contain 0s and 1s") | ||
decimalnumber = 0 | ||
for c in binary: | ||
decimalnumber = decimalnumber * 2 + int(c) | ||
return decimalnumber | ||
|
||
|
||
def binary_decimal_conversion(conversion_type: str, number: str) -> str: | ||
""" | ||
This is the main function: It 'coordinates' and decides | ||
which of the two above functions to use | ||
""" | ||
if conversion_type == "decimal": | ||
if not all(c in "01" for c in number): | ||
raise ValueError( | ||
"If decimal conversion, input must be in binary, consisting only of 0 and 1" | ||
) | ||
return binary_to_decimal(number) | ||
elif conversion_type == "binary": | ||
if not number.isdigit(): | ||
raise ValueError("If binary conversion, input must be a positive integer") | ||
decimal = int(number) | ||
return decimal_to_binary(decimal) | ||
else: | ||
raise ValueError("Conversion Type must be 'binary' or 'decimal ' ") | ||
|
||
|
||
if __name__ == "__main__": | ||
import sys | ||
|
||
if len(sys.argv) != 3: | ||
print( | ||
"To use properly, the first argument is conversion type. the second is the number you want to convert." | ||
) | ||
sys.exit(1) | ||
|
||
conversion_type = sys.argv[1].lower() | ||
number = sys.argv[2] | ||
|
||
if conversion_type not in ["binary", "decimal"]: | ||
print("Error: Conversion type must be binary or decimal") | ||
sys.exit(1) | ||
|
||
if conversion_type == "decimal": | ||
if not all(c in "01" for c in number): | ||
print( | ||
"Error: for decimal conversion, input must be in binary, containing only 1 and 0" | ||
) | ||
sys.exit(1) | ||
result = binary_to_decimal(number) | ||
|
||
elif conversion_type == "binary": | ||
if not number.isdigit(): | ||
print("Error: for binary conversion, input must be positive integer") | ||
sys.exit(1) | ||
result = decimal_to_binary(int(number)) | ||
print(result) |
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,77 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
""" | ||
Module: test_binary_decimal_conversion | ||
contains test cases for the solution code. | ||
Created by Muna Sattouf on January 2, 2024 | ||
Completed on January 2, 2024 | ||
""" | ||
|
||
import unittest | ||
from solutions.binary_decimal_conversion import ( | ||
decimal_to_binary, | ||
binary_decimal_conversion, | ||
binary_to_decimal, | ||
) | ||
|
||
|
||
class TestBinaryDecimalConversion(unittest.TestCase): | ||
"""Tests for binaru_decimal_conversion function""" | ||
|
||
def test_decimal_to_binary(self): | ||
"""Should convert positive decimal numbers to binary""" | ||
self.assertEqual(decimal_to_binary(10), "1010") | ||
|
||
def test_binary_to_decimal(self): | ||
"""Should convert positive binary to decimal""" | ||
self.assertEqual(binary_to_decimal("10011101"), 157) | ||
|
||
def test_zero_decimal_to_binary(self): | ||
"""should return 0""" | ||
self.assertEqual(decimal_to_binary(0), "0") | ||
|
||
def test_zero_binary_to_decimal(self): | ||
"""should return 0""" | ||
self.assertEqual(binary_to_decimal("0"), 0) | ||
|
||
def test_large_decimal_to_binary(self): | ||
"""Should correctly handle large decimal numbers""" | ||
self.assertEqual(decimal_to_binary(1000000), "11110100001001000000") | ||
|
||
def test_large_binary_to_decimal(self): | ||
"""Should correctly handle large binary numbers""" | ||
self.assertEqual(binary_to_decimal("11110100001001000000"), 1000000) | ||
|
||
def test_binarydecimalconversion_decimal(self): | ||
"""Should convert binary to decimal when conversion type is decimal""" | ||
self.assertEqual(binary_decimal_conversion("decimal", "10011101"), 157) | ||
|
||
def test_binarydecimalconversion_binary(self): | ||
"""Should convert decimal to binary when conversion type is binary""" | ||
self.assertEqual(binary_decimal_conversion("binary", "392"), "110001000") | ||
|
||
def test_invalid_decimal_input(self): | ||
"""Should raise ValueError for non-integer decimal input""" | ||
with self.assertRaises(ValueError): | ||
decimal_to_binary("notAnumber") | ||
|
||
def test_invalid_binary_input(self): | ||
"""Should raise ValueError for non-binary input""" | ||
with self.assertRaises(ValueError): | ||
binary_to_decimal("1234") | ||
|
||
def test_invalid_decimal_conversion_input(self): | ||
"""Should raise ValueError if a non-binary string is provided to convert to decimal""" | ||
with self.assertRaises(ValueError): | ||
binary_decimal_conversion("decimal", "1020") | ||
|
||
def test_invalid_binary_conversion_input(self): | ||
"""Should raise ValueError if a non-integer was provided to convert to binary""" | ||
with self.assertRaises(ValueError): | ||
binary_decimal_conversion("binary", "word") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |