From 734e1df0b6b79cbb624dc680c01f9026c4fb650b Mon Sep 17 00:00:00 2001 From: Marc Al Darazi Date: Sat, 11 Jan 2025 10:57:18 +0300 Subject: [PATCH 1/4] hex_to_binary challenge i have done with the second challenge hex_to_binary and it's ready to be reviewed --- solutions/hex_to_binary.py | 41 +++++++++++++++++++++ solutions/tests/test_hex_to_binary.py | 52 +++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 solutions/hex_to_binary.py create mode 100644 solutions/tests/test_hex_to_binary.py diff --git a/solutions/hex_to_binary.py b/solutions/hex_to_binary.py new file mode 100644 index 000000000..32b2a323a --- /dev/null +++ b/solutions/hex_to_binary.py @@ -0,0 +1,41 @@ +""" +hex_to_binary.py + +This module provides a utility function to convert a hexadecimal string +to its binary representation. + +Functions: +- hex_to_binary: Converts a hexadecimal string to its binary equivalent. +""" + +def hex_to_binary(hex_string): + """ + Convert a hexadecimal string to its binary representation. + + Parameters: + hex_string (str): A string representing a hexadecimal number. + It can include a leading '0x' prefix or not. + + Returns: + str: A binary string representation of the given hexadecimal number, + with leading zeros preserved. + + Raises: + ValueError: If the input string is not a valid hexadecimal number. + + Example: + >>> hex_to_binary("1A") + '11010' + >>> hex_to_binary("0xFF") + '11111111' + """ + # Remove '0x' prefix if present + if hex_string.startswith("0x"): + hex_string = hex_string[2:] + + try: + # Convert to integer, then to binary + binary_string = bin(int(hex_string, 16))[2:] # Remove '0b' prefix + return binary_string + except ValueError: + raise ValueError("Invalid hexadecimal string") diff --git a/solutions/tests/test_hex_to_binary.py b/solutions/tests/test_hex_to_binary.py new file mode 100644 index 000000000..e3ea6d603 --- /dev/null +++ b/solutions/tests/test_hex_to_binary.py @@ -0,0 +1,52 @@ +import unittest +from hex_to_binary import hex_to_binary + +class TestHexToBinary(unittest.TestCase): + """ + Unit tests for the hex_to_binary function. + + These tests validate that the hex_to_binary function correctly converts + hexadecimal strings to binary strings, handling both cases with and + without the '0x' prefix and invalid inputs. + """ + + def test_valid_hex_without_prefix(self): + """ + Test converting a valid hexadecimal string without the '0x' prefix. + """ + self.assertEqual(hex_to_binary("1A"), "11010") + + def test_valid_hex_with_prefix(self): + """ + Test converting a valid hexadecimal string with the '0x' prefix. + """ + self.assertEqual(hex_to_binary("0xFF"), "11111111") + + def test_single_digit_hex(self): + """ + Test converting a single digit hexadecimal string. + """ + self.assertEqual(hex_to_binary("9"), "1001") + + def test_hex_with_lowercase_letters(self): + """ + Test converting a hexadecimal string with lowercase letters. + """ + self.assertEqual(hex_to_binary("a"), "1010") + + def test_invalid_hex_with_non_hex_characters(self): + """ + Test an invalid hexadecimal string with non-hex characters. + """ + with self.assertRaises(ValueError): + hex_to_binary("G1") + + def test_empty_string(self): + """ + Test an empty string as input, which should raise a ValueError. + """ + with self.assertRaises(ValueError): + hex_to_binary("") + +if __name__ == "_main_": + unittest.main() From e4b83cae56a712775c22b5377c77a400adc42502 Mon Sep 17 00:00:00 2001 From: Marc Al Darazi Date: Sat, 11 Jan 2025 15:00:14 +0300 Subject: [PATCH 2/4] updated hex_to_binary updated hex_to_binary --- solutions/hex_to_binary.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solutions/hex_to_binary.py b/solutions/hex_to_binary.py index 32b2a323a..f6f475309 100644 --- a/solutions/hex_to_binary.py +++ b/solutions/hex_to_binary.py @@ -13,12 +13,12 @@ def hex_to_binary(hex_string): Convert a hexadecimal string to its binary representation. Parameters: - hex_string (str): A string representing a hexadecimal number. - It can include a leading '0x' prefix or not. + hex_string (str): A string representing a hexadecimal number. + It can include a leading '0x' prefix or not. Returns: str: A binary string representation of the given hexadecimal number, - with leading zeros preserved. + with leading zeros preserved. Raises: ValueError: If the input string is not a valid hexadecimal number. From ffc5cf51459f1aeea3ff453a8201afbeabecad7f Mon Sep 17 00:00:00 2001 From: Marc Al Darazi Date: Sat, 11 Jan 2025 21:24:06 +0300 Subject: [PATCH 3/4] fixed py formatting fixed py formatting --- solutions/hex_to_binary.py | 9 ++++++--- solutions/tests/test_hex_to_binary.py | 26 +++++++++++++++++--------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/solutions/hex_to_binary.py b/solutions/hex_to_binary.py index f6f475309..fcaaeee4b 100644 --- a/solutions/hex_to_binary.py +++ b/solutions/hex_to_binary.py @@ -1,13 +1,14 @@ """ -hex_to_binary.py - This module provides a utility function to convert a hexadecimal string to its binary representation. Functions: - hex_to_binary: Converts a hexadecimal string to its binary equivalent. + +Author: Marc Darazi """ + def hex_to_binary(hex_string): """ Convert a hexadecimal string to its binary representation. @@ -28,11 +29,13 @@ def hex_to_binary(hex_string): '11010' >>> hex_to_binary("0xFF") '11111111' + >>> hex_to_binary("0x0") + '0' """ # Remove '0x' prefix if present if hex_string.startswith("0x"): hex_string = hex_string[2:] - + try: # Convert to integer, then to binary binary_string = bin(int(hex_string, 16))[2:] # Remove '0b' prefix diff --git a/solutions/tests/test_hex_to_binary.py b/solutions/tests/test_hex_to_binary.py index e3ea6d603..b90f4a0be 100644 --- a/solutions/tests/test_hex_to_binary.py +++ b/solutions/tests/test_hex_to_binary.py @@ -1,6 +1,17 @@ +""" +This module provides a utility function to convert a hexadecimal string +to its binary representation. + +Functions: +- hex_to_binary: Converts a hexadecimal string to its binary equivalent. + +Author: Marc Darazi +""" + import unittest from hex_to_binary import hex_to_binary + class TestHexToBinary(unittest.TestCase): """ Unit tests for the hex_to_binary function. @@ -9,44 +20,41 @@ class TestHexToBinary(unittest.TestCase): hexadecimal strings to binary strings, handling both cases with and without the '0x' prefix and invalid inputs. """ - + def test_valid_hex_without_prefix(self): """ Test converting a valid hexadecimal string without the '0x' prefix. """ self.assertEqual(hex_to_binary("1A"), "11010") - + def test_valid_hex_with_prefix(self): """ Test converting a valid hexadecimal string with the '0x' prefix. """ self.assertEqual(hex_to_binary("0xFF"), "11111111") - + def test_single_digit_hex(self): """ Test converting a single digit hexadecimal string. """ self.assertEqual(hex_to_binary("9"), "1001") - + def test_hex_with_lowercase_letters(self): """ Test converting a hexadecimal string with lowercase letters. """ self.assertEqual(hex_to_binary("a"), "1010") - + def test_invalid_hex_with_non_hex_characters(self): """ Test an invalid hexadecimal string with non-hex characters. """ with self.assertRaises(ValueError): hex_to_binary("G1") - + def test_empty_string(self): """ Test an empty string as input, which should raise a ValueError. """ with self.assertRaises(ValueError): hex_to_binary("") - -if __name__ == "_main_": - unittest.main() From c9178d46cbe7db8c047445a04514af735bc93f06 Mon Sep 17 00:00:00 2001 From: Marc Al Darazi Date: Sat, 11 Jan 2025 21:27:19 +0300 Subject: [PATCH 4/4] fixing py_testing fixing py_testing --- solutions/tests/test_hex_to_binary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/tests/test_hex_to_binary.py b/solutions/tests/test_hex_to_binary.py index b90f4a0be..d6c2c66ab 100644 --- a/solutions/tests/test_hex_to_binary.py +++ b/solutions/tests/test_hex_to_binary.py @@ -9,7 +9,7 @@ """ import unittest -from hex_to_binary import hex_to_binary +from ..hex_to_binary import hex_to_binary class TestHexToBinary(unittest.TestCase):