From 51d8fd87606d2fbd4a4b31f9d7303a87b8dcc7d9 Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Mon, 16 Mar 2026 15:06:40 +0530 Subject: [PATCH 01/31] init structuring added init py and binop module --- modules/__init__.py | 0 modules/binop.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 modules/__init__.py create mode 100644 modules/binop.py diff --git a/modules/__init__.py b/modules/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/modules/binop.py b/modules/binop.py new file mode 100644 index 0000000..e69de29 From 13e636209d9aaccecdc18a074142dda5b91eff61 Mon Sep 17 00:00:00 2001 From: Jana Saiyam Santosh Date: Mon, 16 Mar 2026 16:32:26 +0530 Subject: [PATCH 02/31] add readme file --- README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..3dbe4fa --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +BinOP.py --> 3 module +1st module --> conversion.py +2nd module --> op.py +3rd module --> complement.py \ No newline at end of file From da50bbb36a40a91aaf67f0caacf74c6abd27e55f Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Fri, 20 Mar 2026 20:34:41 +0530 Subject: [PATCH 03/31] project structure creation --- modules/__init__.py | 0 modules/binary.py | 0 modules/exceptions.py | 0 tests/__init__.py | 0 tests/testbinary.py | 0 5 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 modules/__init__.py create mode 100644 modules/binary.py create mode 100644 modules/exceptions.py create mode 100644 tests/__init__.py create mode 100644 tests/testbinary.py diff --git a/modules/__init__.py b/modules/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/modules/binary.py b/modules/binary.py new file mode 100644 index 0000000..e69de29 diff --git a/modules/exceptions.py b/modules/exceptions.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/testbinary.py b/tests/testbinary.py new file mode 100644 index 0000000..e69de29 From 8527b636ffd31f66c5334d71702bfb07434d0695 Mon Sep 17 00:00:00 2001 From: Jana Saiyam Santosh Date: Fri, 20 Mar 2026 21:50:09 +0530 Subject: [PATCH 04/31] feature: add binary arithmetic operations --- modules/binary.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 modules/binary.py diff --git a/modules/binary.py b/modules/binary.py new file mode 100644 index 0000000..9e0b8ac --- /dev/null +++ b/modules/binary.py @@ -0,0 +1,43 @@ + +def binary_add(a: str, b: str) -> str: + """ + Add two binary numbers. + e.g. binary_add("B'011", "B'010") -> "B'101" + """ + da = int(binary_to_decimal(a).split("'")[1]) + db = int(binary_to_decimal(b).split("'")[1]) + return decimal_to_binary(f"D'{da + db}") + + +def binary_subtract(a: str, b: str) -> str: + """ + Subtract two binary numbers. + e.g. binary_subtract("B'101", "B'010") -> "B'011" + """ + da = int(binary_to_decimal(a).split("'")[1]) + db = int(binary_to_decimal(b).split("'")[1]) + if da < db: + raise NegativeBinaryResultError() + return decimal_to_binary(f"D'{da - db}") + + +def binary_multiply(a: str, b: str) -> str: + """ + Multiply two binary numbers. + e.g. binary_multiply("B'011", "B'010") -> "B'110" + """ + da = int(binary_to_decimal(a).split("'")[1]) + db = int(binary_to_decimal(b).split("'")[1]) + return decimal_to_binary(f"D'{da * db}") + + +def binary_divide(a: str, b: str) -> str: + """ + Integer divide two binary numbers. + e.g. binary_divide("B'110", "B'010") -> "B'11" + """ + da = int(binary_to_decimal(a).split("'")[1]) + db = int(binary_to_decimal(b).split("'")[1]) + if db == 0: + raise ValueError("Binary division by zero") + return decimal_to_binary(f"D'{da // db}") \ No newline at end of file From bd1f65b741973a9f973fa9ec4faf8a7dd3bfe496 Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Fri, 20 Mar 2026 22:29:04 +0530 Subject: [PATCH 05/31] Delete binop.py --- modules/binop.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 modules/binop.py diff --git a/modules/binop.py b/modules/binop.py deleted file mode 100644 index e69de29..0000000 From b1ef391bf7c207e057a4d76a68336c339ca03f55 Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Fri, 20 Mar 2026 22:31:23 +0530 Subject: [PATCH 06/31] Update binary.py --- modules/binary.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/modules/binary.py b/modules/binary.py index e69de29..be86798 100644 --- a/modules/binary.py +++ b/modules/binary.py @@ -0,0 +1,40 @@ +# -- Pritam 038 ------------------------------------------------------- + +from exceptions import InvalidBinaryInputError, NegativeBinaryResultError + + +def _strip(b: str) -> str: + """Strip B' prefix and whitespace.""" + b = b.strip().upper() + if b.startswith("B'"): + b = b[2:] + return b + + +def _validate(b: str) -> None: + if not b or not all(c in '01' for c in b): + raise InvalidBinaryInputError(b) + + +def binary_to_decimal(b: str) -> str: + """ + Convert binary string to decimal. + e.g. binary_to_decimal("B'1010") -> "D'10" + """ + raw = _strip(b) + _validate(raw) + return f"D'{int(raw, 2)}" + + +def decimal_to_binary(d: str) -> str: + """ + Convert decimal string to binary. + e.g. decimal_to_binary("D'10") -> "B'1010" + """ + d = d.strip().upper() + if d.startswith("D'"): + d = d[2:] + n = int(d) + if n < 0: + raise ValueError("Negative decimal not supported in basic mode") + return f"B'{bin(n)[2:]}" \ No newline at end of file From 19f793bc3a50874b3d5061a10b61535c2732363d Mon Sep 17 00:00:00 2001 From: SuhaniBharti <156918664+SuhaniBharti@users.noreply.github.com> Date: Sat, 21 Mar 2026 18:13:53 +0530 Subject: [PATCH 07/31] feature: add binary complement operations --- modules/binary.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/modules/binary.py b/modules/binary.py index 8e9b398..4d11778 100644 --- a/modules/binary.py +++ b/modules/binary.py @@ -85,3 +85,30 @@ def binary_divide(a: str, b: str) -> str: if db == 0: raise ValueError("Binary division by zero") return decimal_to_binary(f"D'{da // db}") + + + +#-----------suhani 034: complement------------------------------- + + +def ones_complement(b: str) -> str: + """ + Flip all bits. + e.g. ones_complement("B'1010") -> "B'0101" + """ + raw = _strip(b) + _validate(raw) + return "B'" + ''.join('1' if bit == '0' else '0' for bit in raw) + + +def twos_complement(b: str) -> str: + """ + Add 1 to the one's complement. + e.g. twos_complement("B'0111") -> "B'1001" + """ + raw = _strip(b) + _validate(raw) + ones = ones_complement(f"B'{raw}")[2:] # strip B' prefix + val = int(ones, 2) + 1 + result = bin(val)[2:][-len(ones):].zfill(len(ones)) + return f"B'{result}" \ No newline at end of file From 09edc93bcaeaf740b0fcde55fac702e61acea6a5 Mon Sep 17 00:00:00 2001 From: harshkr212 Date: Sun, 22 Mar 2026 14:43:59 +0530 Subject: [PATCH 08/31] Added invalid Check by Harsh --- modules/binary.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/modules/binary.py b/modules/binary.py index 8e9b398..788f1c4 100644 --- a/modules/binary.py +++ b/modules/binary.py @@ -1,3 +1,12 @@ +# ---- Harsh 035: Exceptions ---------- +class InvalidBinaryInputError(ValueError): + def __init__(self, value: str): + super().__init__(f"'{value}' is not a valid binary string") + +class NegativeBinaryResultError(ValueError): + def __init__(self): + super().__init__("Binary subtraction result cannot be negative") + # -- Pritam 038 ------------------------------------------------------- from exceptions import InvalidBinaryInputError, NegativeBinaryResultError From 44d9aa2b3b9ac76eecd46ce505bd7e02e486c0b9 Mon Sep 17 00:00:00 2001 From: deepjitpalmain-dot Date: Sun, 22 Mar 2026 15:07:32 +0530 Subject: [PATCH 09/31] Update binary.py by deepjit --- modules/binary.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/modules/binary.py b/modules/binary.py index 8e9b398..40c8ac3 100644 --- a/modules/binary.py +++ b/modules/binary.py @@ -85,3 +85,87 @@ def binary_divide(a: str, b: str) -> str: if db == 0: raise ValueError("Binary division by zero") return decimal_to_binary(f"D'{da // db}") + + +#--------Deepjit 036---------- + +import unittest +from modules.binary import ( + binary_to_decimal, decimal_to_binary, + binary_add, binary_subtract, + binary_multiply, binary_divide, + ones_complement, twos_complement, +) +from exceptions import InvalidBinaryInputError, NegativeBinaryResultError + + +class TestBinaryConversions(unittest.TestCase): + def test_bin_to_dec_basic(self): + self.assertEqual(binary_to_decimal("B'1010"), "D'10") + + def test_bin_to_dec_zero(self): + self.assertEqual(binary_to_decimal("B'0"), "D'0") + + def test_bin_to_dec_one(self): + self.assertEqual(binary_to_decimal("B'1"), "D'1") + + def test_dec_to_bin_basic(self): + self.assertEqual(decimal_to_binary("D'10"), "B'1010") + + def test_dec_to_bin_zero(self): + self.assertEqual(decimal_to_binary("D'0"), "B'0") + + +class TestBinaryArithmetic(unittest.TestCase): + def test_add(self): + self.assertEqual(binary_add("B'011", "B'010"), "B'101") + + def test_subtract(self): + self.assertEqual(binary_subtract("B'101", "B'010"), "B'011") + + def test_subtract_negative_raises(self): + with self.assertRaises(NegativeBinaryResultError): + binary_subtract("B'001", "B'010") + + def test_multiply(self): + self.assertEqual(binary_multiply("B'011", "B'010"), "B'110") + + def test_divide(self): + self.assertEqual(binary_divide("B'110", "B'010"), "B'11") + + def test_divide_by_zero(self): + with self.assertRaises(ValueError): + binary_divide("B'110", "B'0") + + +class TestComplements(unittest.TestCase): + def test_ones_complement(self): + self.assertEqual(ones_complement("B'1010"), "B'0101") + + def test_ones_all_zeros(self): + self.assertEqual(ones_complement("B'0000"), "B'1111") + + def test_twos_complement(self): + self.assertEqual(twos_complement("B'0111"), "B'1001") + + def test_twos_complement_zero(self): + self.assertEqual(twos_complement("B'0000"), "B'10000") + + +class TestInvalidInput(unittest.TestCase): + def test_invalid_char(self): + with self.assertRaises(InvalidBinaryInputError): + binary_to_decimal("B'1021") + + def test_empty_string(self): + with self.assertRaises(InvalidBinaryInputError): + binary_to_decimal("B'") + + def test_missing_prefix(self): + with self.assertRaises(InvalidBinaryInputError): + binary_to_decimal("1021") + + +if __name__ == '__main__': + unittest.main() + From 1cbdbe7833df1199a763341c200ebaa52dd2f884 Mon Sep 17 00:00:00 2001 From: Pritam Bag <68804912+prtm-bg@users.noreply.github.com> Date: Sun, 22 Mar 2026 17:17:56 +0530 Subject: [PATCH 10/31] Revert "Update binary.py by deepjit" --- modules/binary.py | 84 ----------------------------------------------- 1 file changed, 84 deletions(-) diff --git a/modules/binary.py b/modules/binary.py index 37c4365..788f1c4 100644 --- a/modules/binary.py +++ b/modules/binary.py @@ -94,87 +94,3 @@ def binary_divide(a: str, b: str) -> str: if db == 0: raise ValueError("Binary division by zero") return decimal_to_binary(f"D'{da // db}") - - -#--------Deepjit 036---------- - -import unittest -from modules.binary import ( - binary_to_decimal, decimal_to_binary, - binary_add, binary_subtract, - binary_multiply, binary_divide, - ones_complement, twos_complement, -) -from exceptions import InvalidBinaryInputError, NegativeBinaryResultError - - -class TestBinaryConversions(unittest.TestCase): - def test_bin_to_dec_basic(self): - self.assertEqual(binary_to_decimal("B'1010"), "D'10") - - def test_bin_to_dec_zero(self): - self.assertEqual(binary_to_decimal("B'0"), "D'0") - - def test_bin_to_dec_one(self): - self.assertEqual(binary_to_decimal("B'1"), "D'1") - - def test_dec_to_bin_basic(self): - self.assertEqual(decimal_to_binary("D'10"), "B'1010") - - def test_dec_to_bin_zero(self): - self.assertEqual(decimal_to_binary("D'0"), "B'0") - - -class TestBinaryArithmetic(unittest.TestCase): - def test_add(self): - self.assertEqual(binary_add("B'011", "B'010"), "B'101") - - def test_subtract(self): - self.assertEqual(binary_subtract("B'101", "B'010"), "B'011") - - def test_subtract_negative_raises(self): - with self.assertRaises(NegativeBinaryResultError): - binary_subtract("B'001", "B'010") - - def test_multiply(self): - self.assertEqual(binary_multiply("B'011", "B'010"), "B'110") - - def test_divide(self): - self.assertEqual(binary_divide("B'110", "B'010"), "B'11") - - def test_divide_by_zero(self): - with self.assertRaises(ValueError): - binary_divide("B'110", "B'0") - - -class TestComplements(unittest.TestCase): - def test_ones_complement(self): - self.assertEqual(ones_complement("B'1010"), "B'0101") - - def test_ones_all_zeros(self): - self.assertEqual(ones_complement("B'0000"), "B'1111") - - def test_twos_complement(self): - self.assertEqual(twos_complement("B'0111"), "B'1001") - - def test_twos_complement_zero(self): - self.assertEqual(twos_complement("B'0000"), "B'10000") - - -class TestInvalidInput(unittest.TestCase): - def test_invalid_char(self): - with self.assertRaises(InvalidBinaryInputError): - binary_to_decimal("B'1021") - - def test_empty_string(self): - with self.assertRaises(InvalidBinaryInputError): - binary_to_decimal("B'") - - def test_missing_prefix(self): - with self.assertRaises(InvalidBinaryInputError): - binary_to_decimal("1021") - - -if __name__ == '__main__': - unittest.main() - From 4b59734705650404a029a5f17d2cee71666f9ac2 Mon Sep 17 00:00:00 2001 From: Pritam Bag <68804912+prtm-bg@users.noreply.github.com> Date: Sun, 22 Mar 2026 17:18:49 +0530 Subject: [PATCH 11/31] Revert "Add input validation for binary strings" --- modules/binary.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/modules/binary.py b/modules/binary.py index 788f1c4..8e9b398 100644 --- a/modules/binary.py +++ b/modules/binary.py @@ -1,12 +1,3 @@ -# ---- Harsh 035: Exceptions ---------- -class InvalidBinaryInputError(ValueError): - def __init__(self, value: str): - super().__init__(f"'{value}' is not a valid binary string") - -class NegativeBinaryResultError(ValueError): - def __init__(self): - super().__init__("Binary subtraction result cannot be negative") - # -- Pritam 038 ------------------------------------------------------- from exceptions import InvalidBinaryInputError, NegativeBinaryResultError From e91eefe71abd1f539ad01ff42ec440a1f4a4b6dd Mon Sep 17 00:00:00 2001 From: harshkr212 Date: Sun, 22 Mar 2026 17:31:45 +0530 Subject: [PATCH 12/31] feature : validations --- modules/binary.py | 9 --------- modules/exceptions.py | 8 ++++++++ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/modules/binary.py b/modules/binary.py index 788f1c4..8e9b398 100644 --- a/modules/binary.py +++ b/modules/binary.py @@ -1,12 +1,3 @@ -# ---- Harsh 035: Exceptions ---------- -class InvalidBinaryInputError(ValueError): - def __init__(self, value: str): - super().__init__(f"'{value}' is not a valid binary string") - -class NegativeBinaryResultError(ValueError): - def __init__(self): - super().__init__("Binary subtraction result cannot be negative") - # -- Pritam 038 ------------------------------------------------------- from exceptions import InvalidBinaryInputError, NegativeBinaryResultError diff --git a/modules/exceptions.py b/modules/exceptions.py index e69de29..ddadab7 100644 --- a/modules/exceptions.py +++ b/modules/exceptions.py @@ -0,0 +1,8 @@ +# ---- Harsh 035: Exceptions ---------- +class InvalidBinaryInputError(ValueError): + def __init__(self, value: str): + super().__init__(f"'{value}' is not a valid binary string") + +class NegativeBinaryResultError(ValueError): + def __init__(self): + super().__init__("Binary subtraction result cannot be negative") \ No newline at end of file From 773990327a2c9ccdc11325856397c1d54fc7ae3f Mon Sep 17 00:00:00 2001 From: deepjitpalmain-dot Date: Sun, 22 Mar 2026 17:52:31 +0530 Subject: [PATCH 13/31] Delete modules/exceptions.py --- modules/exceptions.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 modules/exceptions.py diff --git a/modules/exceptions.py b/modules/exceptions.py deleted file mode 100644 index e69de29..0000000 From e6bc45e37722b21c184bfdba6b81127dde202c24 Mon Sep 17 00:00:00 2001 From: deepjitpalmain-dot Date: Sun, 22 Mar 2026 17:54:24 +0530 Subject: [PATCH 14/31] Create test_binary.py --- modules/test_binary.py | 83 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 modules/test_binary.py diff --git a/modules/test_binary.py b/modules/test_binary.py new file mode 100644 index 0000000..2c62ca0 --- /dev/null +++ b/modules/test_binary.py @@ -0,0 +1,83 @@ + +# ── Deepjit Pal (036): Exceptions───────────────────────────────────────────────────── + + +import unittest +from modules.binary import ( + binary_to_decimal, decimal_to_binary, + binary_add, binary_subtract, + binary_multiply, binary_divide, + ones_complement, twos_complement, +) +from exceptions import InvalidBinaryInputError, NegativeBinaryResultError + + +class TestBinaryConversions(unittest.TestCase): + def test_bin_to_dec_basic(self): + self.assertEqual(binary_to_decimal("B'1010"), "D'10") + + def test_bin_to_dec_zero(self): + self.assertEqual(binary_to_decimal("B'0"), "D'0") + + def test_bin_to_dec_one(self): + self.assertEqual(binary_to_decimal("B'1"), "D'1") + + def test_dec_to_bin_basic(self): + self.assertEqual(decimal_to_binary("D'10"), "B'1010") + + def test_dec_to_bin_zero(self): + self.assertEqual(decimal_to_binary("D'0"), "B'0") + + +class TestBinaryArithmetic(unittest.TestCase): + def test_add(self): + self.assertEqual(binary_add("B'011", "B'010"), "B'101") + + def test_subtract(self): + self.assertEqual(binary_subtract("B'101", "B'010"), "B'011") + + def test_subtract_negative_raises(self): + with self.assertRaises(NegativeBinaryResultError): + binary_subtract("B'001", "B'010") + + def test_multiply(self): + self.assertEqual(binary_multiply("B'011", "B'010"), "B'110") + + def test_divide(self): + self.assertEqual(binary_divide("B'110", "B'010"), "B'11") + + def test_divide_by_zero(self): + with self.assertRaises(ValueError): + binary_divide("B'110", "B'0") + + +class TestComplements(unittest.TestCase): + def test_ones_complement(self): + self.assertEqual(ones_complement("B'1010"), "B'0101") + + def test_ones_all_zeros(self): + self.assertEqual(ones_complement("B'0000"), "B'1111") + + def test_twos_complement(self): + self.assertEqual(twos_complement("B'0111"), "B'1001") + + def test_twos_complement_zero(self): + self.assertEqual(twos_complement("B'0000"), "B'10000") + + +class TestInvalidInput(unittest.TestCase): + def test_invalid_char(self): + with self.assertRaises(InvalidBinaryInputError): + binary_to_decimal("B'1021") + + def test_empty_string(self): + with self.assertRaises(InvalidBinaryInputError): + binary_to_decimal("B'") + + def test_missing_prefix(self): + with self.assertRaises(InvalidBinaryInputError): + binary_to_decimal("1021") + + +if __name__ == '__main__': + unittest.main() From 055022761014520805ec53d359e68cb819715ff4 Mon Sep 17 00:00:00 2001 From: deepjitpalmain-dot Date: Sun, 22 Mar 2026 17:55:21 +0530 Subject: [PATCH 15/31] Update binary.py --- modules/binary.py | 79 ----------------------------------------------- 1 file changed, 79 deletions(-) diff --git a/modules/binary.py b/modules/binary.py index 40c8ac3..d12cc78 100644 --- a/modules/binary.py +++ b/modules/binary.py @@ -87,85 +87,6 @@ def binary_divide(a: str, b: str) -> str: return decimal_to_binary(f"D'{da // db}") -#--------Deepjit 036---------- - -import unittest -from modules.binary import ( - binary_to_decimal, decimal_to_binary, - binary_add, binary_subtract, - binary_multiply, binary_divide, - ones_complement, twos_complement, -) -from exceptions import InvalidBinaryInputError, NegativeBinaryResultError - - -class TestBinaryConversions(unittest.TestCase): - def test_bin_to_dec_basic(self): - self.assertEqual(binary_to_decimal("B'1010"), "D'10") - - def test_bin_to_dec_zero(self): - self.assertEqual(binary_to_decimal("B'0"), "D'0") - - def test_bin_to_dec_one(self): - self.assertEqual(binary_to_decimal("B'1"), "D'1") - - def test_dec_to_bin_basic(self): - self.assertEqual(decimal_to_binary("D'10"), "B'1010") - - def test_dec_to_bin_zero(self): - self.assertEqual(decimal_to_binary("D'0"), "B'0") - - -class TestBinaryArithmetic(unittest.TestCase): - def test_add(self): - self.assertEqual(binary_add("B'011", "B'010"), "B'101") - - def test_subtract(self): - self.assertEqual(binary_subtract("B'101", "B'010"), "B'011") - - def test_subtract_negative_raises(self): - with self.assertRaises(NegativeBinaryResultError): - binary_subtract("B'001", "B'010") - - def test_multiply(self): - self.assertEqual(binary_multiply("B'011", "B'010"), "B'110") - - def test_divide(self): - self.assertEqual(binary_divide("B'110", "B'010"), "B'11") - - def test_divide_by_zero(self): - with self.assertRaises(ValueError): - binary_divide("B'110", "B'0") - - -class TestComplements(unittest.TestCase): - def test_ones_complement(self): - self.assertEqual(ones_complement("B'1010"), "B'0101") - - def test_ones_all_zeros(self): - self.assertEqual(ones_complement("B'0000"), "B'1111") - - def test_twos_complement(self): - self.assertEqual(twos_complement("B'0111"), "B'1001") - - def test_twos_complement_zero(self): - self.assertEqual(twos_complement("B'0000"), "B'10000") - - -class TestInvalidInput(unittest.TestCase): - def test_invalid_char(self): - with self.assertRaises(InvalidBinaryInputError): - binary_to_decimal("B'1021") - - def test_empty_string(self): - with self.assertRaises(InvalidBinaryInputError): - binary_to_decimal("B'") - - def test_missing_prefix(self): - with self.assertRaises(InvalidBinaryInputError): - binary_to_decimal("1021") -if __name__ == '__main__': - unittest.main() From 6ecad9eaf6eddfd44d8c7c9fb642c0ae98cb07ee Mon Sep 17 00:00:00 2001 From: deepjitpalmain-dot Date: Sun, 22 Mar 2026 18:00:01 +0530 Subject: [PATCH 16/31] Create exceptions.py --- modules/exceptions.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 modules/exceptions.py diff --git a/modules/exceptions.py b/modules/exceptions.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/modules/exceptions.py @@ -0,0 +1 @@ + From 73f3e217250d8863d0aa0d972d6947c347b917c8 Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Sun, 22 Mar 2026 18:16:16 +0530 Subject: [PATCH 17/31] updated calculator.py and init.py calculator.py now has function drivers for evaluating binary operations. --- calculator.py | 58 ++++++++++++++++++++++++++++++++++++++++----- modules/__init__.py | 8 +++++++ 2 files changed, 60 insertions(+), 6 deletions(-) diff --git a/calculator.py b/calculator.py index 5daac9a..e3b8c90 100644 --- a/calculator.py +++ b/calculator.py @@ -1,11 +1,57 @@ +import re +from modules.binary import ( + binary_to_decimal, decimal_to_binary, + binary_add, binary_subtract, + binary_multiply, binary_divide, + ones_complement, twos_complement, +) + + class Calculator: - def add(self, a, b): - return a + b - def subtract(self, a, b): - return a - b - def multiply(self, a, b): - return a * b + def add(self, a, b): return a + b + def subtract(self, a, b): return a - b + def multiply(self, a, b): return a * b def divide(self, a, b): if b == 0: raise ValueError("Division by zero") return a / b + + def evaluate(self, expression: str) -> str: + """ + Route string expressions to the correct module. + Examples: + "bin(10)" -> "B'1010" + "dec(B'1010)" -> "D'10" + "1s(B'1010)" -> "B'0101" + "2s(B'0111)" -> "B'1001" + "B'011 + B'010" -> "B'101" + """ + expr = expression.strip() + + if re.match(r"^bin\(", expr, re.I): + val = re.search(r"\((.+)\)", expr).group(1).strip() + return decimal_to_binary(f"D'{val}" if not val.upper().startswith("D'") else val) + + if re.match(r"^dec\(", expr, re.I): + val = re.search(r"\((.+)\)", expr).group(1).strip() + return binary_to_decimal(val) + + if re.match(r"^1s\(", expr, re.I): + val = re.search(r"\((.+)\)", expr).group(1).strip() + return ones_complement(val) + + if re.match(r"^2s\(", expr, re.I): + val = re.search(r"\((.+)\)", expr).group(1).strip() + return twos_complement(val) + + # Binary arithmetic: "B'011 + B'010" + m = re.match( + r"(B'[01]+)\s*([\+\-\*\/])\s*(B'[01]+)", expr, re.I + ) + if m: + a, op, b = m.group(1), m.group(2), m.group(3) + ops = {'+': binary_add, '-': binary_subtract, + '*': binary_multiply, '/': binary_divide} + return ops[op](a, b) + + raise ValueError(f"Unrecognised expression: {expression}") \ No newline at end of file diff --git a/modules/__init__.py b/modules/__init__.py index e69de29..0be3fc7 100644 --- a/modules/__init__.py +++ b/modules/__init__.py @@ -0,0 +1,8 @@ +from .binary import ( + binary_to_decimal, + decimal_to_binary, + binary_add, + binary_subtract, + ones_complement, + twos_complement, +) \ No newline at end of file From 0c8c6415da9e66a31ae2737ae761894e3caebaae Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Sun, 22 Mar 2026 18:27:18 +0530 Subject: [PATCH 18/31] Update __init__.py --- tests/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..9d76c9a 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1 @@ +from .testbinary import * \ No newline at end of file From 7a2dcc2980684838c766c87e53bb0563789a5ef0 Mon Sep 17 00:00:00 2001 From: deepjitpalmain-dot Date: Sun, 22 Mar 2026 19:01:16 +0530 Subject: [PATCH 19/31] Delete modules/test_binary.py --- modules/test_binary.py | 83 ------------------------------------------ 1 file changed, 83 deletions(-) delete mode 100644 modules/test_binary.py diff --git a/modules/test_binary.py b/modules/test_binary.py deleted file mode 100644 index 2c62ca0..0000000 --- a/modules/test_binary.py +++ /dev/null @@ -1,83 +0,0 @@ - -# ── Deepjit Pal (036): Exceptions───────────────────────────────────────────────────── - - -import unittest -from modules.binary import ( - binary_to_decimal, decimal_to_binary, - binary_add, binary_subtract, - binary_multiply, binary_divide, - ones_complement, twos_complement, -) -from exceptions import InvalidBinaryInputError, NegativeBinaryResultError - - -class TestBinaryConversions(unittest.TestCase): - def test_bin_to_dec_basic(self): - self.assertEqual(binary_to_decimal("B'1010"), "D'10") - - def test_bin_to_dec_zero(self): - self.assertEqual(binary_to_decimal("B'0"), "D'0") - - def test_bin_to_dec_one(self): - self.assertEqual(binary_to_decimal("B'1"), "D'1") - - def test_dec_to_bin_basic(self): - self.assertEqual(decimal_to_binary("D'10"), "B'1010") - - def test_dec_to_bin_zero(self): - self.assertEqual(decimal_to_binary("D'0"), "B'0") - - -class TestBinaryArithmetic(unittest.TestCase): - def test_add(self): - self.assertEqual(binary_add("B'011", "B'010"), "B'101") - - def test_subtract(self): - self.assertEqual(binary_subtract("B'101", "B'010"), "B'011") - - def test_subtract_negative_raises(self): - with self.assertRaises(NegativeBinaryResultError): - binary_subtract("B'001", "B'010") - - def test_multiply(self): - self.assertEqual(binary_multiply("B'011", "B'010"), "B'110") - - def test_divide(self): - self.assertEqual(binary_divide("B'110", "B'010"), "B'11") - - def test_divide_by_zero(self): - with self.assertRaises(ValueError): - binary_divide("B'110", "B'0") - - -class TestComplements(unittest.TestCase): - def test_ones_complement(self): - self.assertEqual(ones_complement("B'1010"), "B'0101") - - def test_ones_all_zeros(self): - self.assertEqual(ones_complement("B'0000"), "B'1111") - - def test_twos_complement(self): - self.assertEqual(twos_complement("B'0111"), "B'1001") - - def test_twos_complement_zero(self): - self.assertEqual(twos_complement("B'0000"), "B'10000") - - -class TestInvalidInput(unittest.TestCase): - def test_invalid_char(self): - with self.assertRaises(InvalidBinaryInputError): - binary_to_decimal("B'1021") - - def test_empty_string(self): - with self.assertRaises(InvalidBinaryInputError): - binary_to_decimal("B'") - - def test_missing_prefix(self): - with self.assertRaises(InvalidBinaryInputError): - binary_to_decimal("1021") - - -if __name__ == '__main__': - unittest.main() From c89fc195489f5df8f6e3384433c9ca44e79f9226 Mon Sep 17 00:00:00 2001 From: deepjitpalmain-dot Date: Sun, 22 Mar 2026 19:01:55 +0530 Subject: [PATCH 20/31] Update testbinary.py --- tests/testbinary.py | 85 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/tests/testbinary.py b/tests/testbinary.py index e69de29..f7f20a4 100644 --- a/tests/testbinary.py +++ b/tests/testbinary.py @@ -0,0 +1,85 @@ + +# ── deepjit pal (036): Exceptions───────────────────────────────────────────────────── + + +import unittest +from modules.binary import ( + binary_to_decimal, decimal_to_binary, + binary_add, binary_subtract, + binary_multiply, binary_divide, + ones_complement, twos_complement, +) +from exceptions import InvalidBinaryInputError, NegativeBinaryResultError + + +class TestBinaryConversions(unittest.TestCase): + def test_bin_to_dec_basic(self): + self.assertEqual(binary_to_decimal("B'1010"), "D'10") + + def test_bin_to_dec_zero(self): + self.assertEqual(binary_to_decimal("B'0"), "D'0") + + def test_bin_to_dec_one(self): + self.assertEqual(binary_to_decimal("B'1"), "D'1") + + def test_dec_to_bin_basic(self): + self.assertEqual(decimal_to_binary("D'10"), "B'1010") + + def test_dec_to_bin_zero(self): + self.assertEqual(decimal_to_binary("D'0"), "B'0") + + +class TestBinaryArithmetic(unittest.TestCase): + def test_add(self): + self.assertEqual(binary_add("B'011", "B'010"), "B'101") + + def test_subtract(self): + self.assertEqual(binary_subtract("B'101", "B'010"), "B'011") + + def test_subtract_negative_raises(self): + with self.assertRaises(NegativeBinaryResultError): + binary_subtract("B'001", "B'010") + + def test_multiply(self): + self.assertEqual(binary_multiply("B'011", "B'010"), "B'110") + + def test_divide(self): + self.assertEqual(binary_divide("B'110", "B'010"), "B'11") + + def test_divide_by_zero(self): + with self.assertRaises(ValueError): + binary_divide("B'110", "B'0") + + +class TestComplements(unittest.TestCase): + def test_ones_complement(self): + self.assertEqual(ones_complement("B'1010"), "B'0101") + + def test_ones_all_zeros(self): + self.assertEqual(ones_complement("B'0000"), "B'1111") + + def test_twos_complement(self): + self.assertEqual(twos_complement("B'0111"), "B'1001") + + + def test_twos_complement_zero(self): + self.assertEqual(twos_complement("B'0000"), "B'10000") + + +class TestInvalidInput(unittest.TestCase): + def test_invalid_char(self): + with self.assertRaises(InvalidBinaryInputError): + binary_to_decimal("B'1021") + + def test_empty_string(self): + with self.assertRaises(InvalidBinaryInputError): + binary_to_decimal("B'") + + def test_missing_prefix(self): + with self.assertRaises(InvalidBinaryInputError): + binary_to_decimal("1021") + + +if __name__ == '__main__': + unittest.main() + From 76dac91a45b9ea379c145571c4465fdc24615450 Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Sun, 22 Mar 2026 19:27:19 +0530 Subject: [PATCH 21/31] Update test_calculator.py fixed duplicate divide. --- test_calculator.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test_calculator.py b/test_calculator.py index 7cb79e9..5bb6c42 100644 --- a/test_calculator.py +++ b/test_calculator.py @@ -15,13 +15,13 @@ def test_sub(self): def test_multiply(self): self.assertEqual(self.calc.multiply(2, 3), 6) - def test_divide(self): + def test_divide_positive(self): self.assertEqual(self.calc.divide(2, 4), 0.5) - def test_divide(self): + def test_divide_negative(self): self.assertEqual(self.calc.divide(4, -2), -2) - - def test_divide_fail(self): # this will fail + + def test_divide_fail(self): self.assertNotEqual(self.calc.divide(4, -2), 2) def test_divide_by_zero(self): From 594be35315e71d759165fb5cdacbc24fb30ed8d6 Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Sun, 22 Mar 2026 19:28:01 +0530 Subject: [PATCH 22/31] Update README.md --- README.md | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3dbe4fa..e676cf7 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,29 @@ -BinOP.py --> 3 module -1st module --> conversion.py -2nd module --> op.py -3rd module --> complement.py \ No newline at end of file +# SE Calculator — Feature 5.6: Binary Number System + +Group 6 | Branch: FeatureA_6 | Process: Classical Waterfall + +## Project Structure +SE_CALCULATOR/ +├── calculator.py # Base calculator (add, sub, mul, div) +├── test_calculator.py # Base unit tests +├── modules/ +│ ├── __init__.py # Re-exports public API +│ ├── binary.py # Binary operations +│ └── exceptions.py # Custom exceptions +├── tests/ +│ ├── __init__.py +│ └── testbinary.py # Unit tests for binary module +└── .github/workflows/ # CI pipeline + +## Features +- Binary ↔ Decimal conversion +- Binary arithmetic (add, sub, mul, div) +- 1's and 2's complement +- Input validation with custom exceptions + +## Running Tests + pytest tests/testbinary.py -v + +## Input Format +- Binary: B'1010 +- Decimal: D'10 \ No newline at end of file From fc35335628502d04e29f6e47ab710cf812c35635 Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Sun, 22 Mar 2026 19:29:15 +0530 Subject: [PATCH 23/31] Create tests.yml --- .github/workflows/tests.yml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 .github/workflows/tests.yml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..eb2c507 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,32 @@ +name: Run Tests + +on: + push: + branches: ["**"] + pull_request: + branches: ["**"] + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Install dependencies + run: pip install pytest pytest-cov + + - name: Run base calculator tests + run: pytest test_calculator.py -v + + - name: Run binary module tests + run: pytest tests/testbinary.py -v + + - name: Coverage report + run: pytest tests/testbinary.py --cov=modules --cov-report=term-missing \ No newline at end of file From a300249e71a54ac145e5ffc6d15880b8447628e4 Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Sun, 22 Mar 2026 19:30:23 +0530 Subject: [PATCH 24/31] Create .gitignore --- .gitignore | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9fb5e81 --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +# Virtual environment +.venv/ +env/ +venv/ + +# Python cache +__pycache__/ +*.pyc +*.pyo +*.pyd + +# Pytest cache +.pytest_cache/ +.coverage +htmlcov/ + +# IDE +.vscode/ +.idea/ + +# OS +.DS_Store +Thumbs.db \ No newline at end of file From 0a848aa8d97856d5b00a0e5b526cf1ae9914c091 Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Sun, 22 Mar 2026 19:31:04 +0530 Subject: [PATCH 25/31] Update binary.py fixed import path for exceptions module --- modules/binary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/binary.py b/modules/binary.py index 4d11778..c7603ef 100644 --- a/modules/binary.py +++ b/modules/binary.py @@ -1,6 +1,6 @@ # -- Pritam 038 ------------------------------------------------------- -from exceptions import InvalidBinaryInputError, NegativeBinaryResultError +from .exceptions import InvalidBinaryInputError, NegativeBinaryResultError def _strip(b: str) -> str: From 68ad37742477e0c91fa43c01794f4fb1a7f8bf8c Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Sun, 22 Mar 2026 19:32:37 +0530 Subject: [PATCH 26/31] Update __init__.py added imports for multiply and divide --- modules/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/__init__.py b/modules/__init__.py index 0be3fc7..eefee6b 100644 --- a/modules/__init__.py +++ b/modules/__init__.py @@ -3,6 +3,8 @@ decimal_to_binary, binary_add, binary_subtract, + binary_multiply, + binary_divide, ones_complement, twos_complement, ) \ No newline at end of file From 283e97b032c70eb3a26bf6df6d5ba1ef3d547360 Mon Sep 17 00:00:00 2001 From: Pritam Bag <68804912+prtm-bg@users.noreply.github.com> Date: Sun, 22 Mar 2026 19:33:40 +0530 Subject: [PATCH 27/31] Fix typo in README project structure --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e676cf7..a46375e 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Group 6 | Branch: FeatureA_6 | Process: Classical Waterfall SE_CALCULATOR/ ├── calculator.py # Base calculator (add, sub, mul, div) ├── test_calculator.py # Base unit tests -├── modules/ +├── modules// │ ├── __init__.py # Re-exports public API │ ├── binary.py # Binary operations │ └── exceptions.py # Custom exceptions @@ -26,4 +26,4 @@ SE_CALCULATOR/ ## Input Format - Binary: B'1010 -- Decimal: D'10 \ No newline at end of file +- Decimal: D'10 From b71c849738c13738da172db086b5e72e81b00992 Mon Sep 17 00:00:00 2001 From: Pritam Bag <68804912+prtm-bg@users.noreply.github.com> Date: Sun, 22 Mar 2026 19:33:57 +0530 Subject: [PATCH 28/31] Update README to remove project structure details Removed project structure section from README. --- README.md | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/README.md b/README.md index a46375e..c586371 100644 --- a/README.md +++ b/README.md @@ -2,19 +2,6 @@ Group 6 | Branch: FeatureA_6 | Process: Classical Waterfall -## Project Structure -SE_CALCULATOR/ -├── calculator.py # Base calculator (add, sub, mul, div) -├── test_calculator.py # Base unit tests -├── modules// -│ ├── __init__.py # Re-exports public API -│ ├── binary.py # Binary operations -│ └── exceptions.py # Custom exceptions -├── tests/ -│ ├── __init__.py -│ └── testbinary.py # Unit tests for binary module -└── .github/workflows/ # CI pipeline - ## Features - Binary ↔ Decimal conversion - Binary arithmetic (add, sub, mul, div) From 1ee359444c653b9a134594785937d0aacde5e937 Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Sun, 22 Mar 2026 19:38:59 +0530 Subject: [PATCH 29/31] Update testbinary.py fixed import path for exceptions module --- tests/testbinary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testbinary.py b/tests/testbinary.py index f7f20a4..702c3e5 100644 --- a/tests/testbinary.py +++ b/tests/testbinary.py @@ -9,7 +9,7 @@ binary_multiply, binary_divide, ones_complement, twos_complement, ) -from exceptions import InvalidBinaryInputError, NegativeBinaryResultError +from modules.exceptions import InvalidBinaryInputError, NegativeBinaryResultError class TestBinaryConversions(unittest.TestCase): From 81e499e0f9a053eee142efbcd46067cc44c97b70 Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Sun, 22 Mar 2026 19:42:21 +0530 Subject: [PATCH 30/31] Update binary.py fixed subtraction function --- modules/binary.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/binary.py b/modules/binary.py index c7603ef..a78e0b4 100644 --- a/modules/binary.py +++ b/modules/binary.py @@ -62,7 +62,9 @@ def binary_subtract(a: str, b: str) -> str: db = int(binary_to_decimal(b).split("'")[1]) if da < db: raise NegativeBinaryResultError() - return decimal_to_binary(f"D'{da - db}") + bit_len = max(len(_strip(a)), len(_strip(b))) + result = bin(da - db)[2:].zfill(bit_len) + return f"B'{result}" def binary_multiply(a: str, b: str) -> str: From b7b62ba6e5e488b99ef603ff83e2dffdd5cd1b7e Mon Sep 17 00:00:00 2001 From: prtm-bg Date: Sun, 22 Mar 2026 19:43:45 +0530 Subject: [PATCH 31/31] Update binary.py fixed ones complement function: issue: modular arithmetic kills the carry fix: let the carry naturally extend the bit length --- modules/binary.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/binary.py b/modules/binary.py index a78e0b4..063df4e 100644 --- a/modules/binary.py +++ b/modules/binary.py @@ -110,7 +110,7 @@ def twos_complement(b: str) -> str: """ raw = _strip(b) _validate(raw) - ones = ones_complement(f"B'{raw}")[2:] # strip B' prefix - val = int(ones, 2) + 1 - result = bin(val)[2:][-len(ones):].zfill(len(ones)) + ones = ones_complement(f"B'{raw}")[2:] + val = int(ones, 2) + 1 # no modulo + result = bin(val)[2:].zfill(len(ones)) # zfill won't truncate carry return f"B'{result}" \ No newline at end of file