diff --git a/hex.py b/hex.py new file mode 100644 index 0000000..dbd99fd --- /dev/null +++ b/hex.py @@ -0,0 +1,121 @@ +class HexCalculator: + """ + Hexadecimal calculator module. + + Supports: + - Hex ↔ Decimal conversions + - Arithmetic operations in HEX + - Complement calculations + - Input validation + """ + + def _validate_hex(self, value: str) -> str: + """ + Validate input format H'AB12' + Returns the internal hex string. + """ + if not isinstance(value, str): + raise ValueError("Input must be a string") + + if not value.startswith("H'") or not value.endswith("'"): + raise ValueError("Invalid HEX format. Expected H'AB12'") + + hex_part = value[2:-1] + + try: + int(hex_part, 16) + except ValueError: + raise ValueError("Invalid hexadecimal digits") + + return hex_part + + # ------------------------------- + # Sravanti will edit this part + # ------------------------------- + def hex_to_decimal(self, value: str) -> str: + """ + Convert HEX → Decimal + + Example: + H'1A5' → D'421' + """ + raise NotImplementedError("hex_to_decimal not implemented yet") + + # ------------------------------- + # Saichaitanya will edit this part + # ------------------------------- + def decimal_to_hex(self, value: str) -> str: + value = value.strip() + + if value.startswith("D'") and value.endswith("'"): + value = value[2:-1] + + if not value.isdigit(): + raise ValueError("Invalid decimal input") + + return f"H'{hex(int(value))[2:].upper()}'" + # ------------------------------- + # Joydip will edit this part + # ------------------------------- + def add(self, a: str, b: str) -> str: + """ + HEX addition + + Example: + H'A' + H'5' → H'F' + """ + raise NotImplementedError("add not implemented yet") + # ------------------------------- + # Pratyush will edit this part + # ------------------------------- + + def subtract(self, a: str, b: str) -> str: + """ + HEX subtraction + """ + hex_a = self._validate_hex(a) + hex_b = self._validate_hex(b) + + result = int(hex_a, 16) - int(hex_b, 16) + + if result < 0: + return f"H'-{format(abs(result), 'X')}'" + + return f"H'{format(result, 'X')}'" + + # ------------------------------- + # Sneha will edit this part + # ------------------------------- + def fifteen_complement(self, value: str) -> str: + """ + Compute 15's complement + """ + hex_part = self._validate_hex(value) + + result = "" + for digit in hex_part: + comp = 15 - int(digit, 16) + result += format(comp, 'X') + + return f"H'{result}'" + + + def sixteen_complement(self, value: str) -> str: + """ + Compute 16's complement + """ + hex_part = self._validate_hex(value) + + # Step 1: 15's complement + comp15 = "" + for digit in hex_part: + comp = 15 - int(digit, 16) + comp15 += format(comp, 'X') + + # Step 2: add 1 + comp16 = hex(int(comp15, 16) + 1)[2:].upper() + + # Step 3: maintain same length + comp16 = comp16.zfill(len(hex_part)) + + return f"H'{comp16}'" diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..e965047 --- /dev/null +++ b/readme.md @@ -0,0 +1 @@ +Hello diff --git a/test_hex.py b/test_hex.py new file mode 100644 index 0000000..ef6c4dd --- /dev/null +++ b/test_hex.py @@ -0,0 +1,88 @@ +# test_hex.py + +import unittest +from hex import HexCalculator + + +class TestHexCalculator(unittest.TestCase): + + def setUp(self): + self.hex_calc = HexCalculator() + + # --------------------------------- + # SRAVANTI 1 TESTS + # --------------------------------- + def test_hex_to_decimal(self): + self.assertEqual( + self.hex_calc.hex_to_decimal("H'1A5'"), + "D'421'" + ) + + # --------------------------------- + # SAICHAITANYA 2 TESTS + # --------------------------------- + def test_decimal_to_hex(self): + self.assertEqual( + self.hex_calc.decimal_to_hex("D'243'"), + "H'F3'" + ) + def test_decimal_to_hex_small(self): + self.assertEqual( + self.hex_calc.decimal_to_hex("D'15'"), + "H'F'" + ) + # --------------------------------- + # JOYDIP 3 TESTS + # --------------------------------- + def test_add(self): + self.assertEqual( + self.hex_calc.add("H'A'", "H'5'"), + "H'F'" + ) + # --------------------------------- + # PRATYUSH 3 TESTS + # --------------------------------- + + def test_subtract(self): + self.assertEqual( + self.hex_calc.subtract("H'F'", "H'5'"), + "H'A'" + ) + + def test_subtract_with_borrow(self): + self.assertEqual( + self.hex_calc.subtract("H'10'", "H'1'"), + "H'F'" + ) + + def test_subtract_negative_result(self): + self.assertEqual( + self.hex_calc.subtract("H'2'", "H'5'"), + "H'-3'" + ) + + # --------------------------------- + # SNEHA 4 TESTS + # --------------------------------- + def test_fifteen_complement(self): + self.assertEqual( + self.hex_calc.fifteen_complement("H'A'"), + "H'5'" + ) + + def test_sixteen_complement(self): + self.assertEqual( + self.hex_calc.sixteen_complement("H'A'"), + "H'6'" + ) + + # --------------------------------- + # COMMON VALIDATION TEST --- Already Done + # --------------------------------- + def test_invalid_hex(self): + with self.assertRaises(ValueError): + self.hex_calc.hex_to_decimal("123") + + +if __name__ == "__main__": + unittest.main()