Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary decimal conversion #52

Merged
merged 22 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions solutions/binary_decimal_conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""
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)
Muna-S marked this conversation as resolved.
Show resolved Hide resolved
>>> 110001000

"""
if not isinstance(decimal, int):
Muna-S marked this conversation as resolved.
Show resolved Hide resolved
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: BinaryToDecimal('decimal', 10011101)
>>> 157
"""
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)
66 changes: 66 additions & 0 deletions solutions/tests/test_binary_decimal_conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""
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 binary_decimal_conversion import decimal_to_binary, binary_decimal_conversion, binary_to_decimal
Muna-S marked this conversation as resolved.
Show resolved Hide resolved

class TestBinaryDecimalConversion(unittest.TestCase):

Muna-S marked this conversation as resolved.
Show resolved Hide resolved
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()
Loading