From 5f8c52854e00e25c2ceb8cd8913a546bb355f964 Mon Sep 17 00:00:00 2001 From: Ewan John Dennis Date: Tue, 21 Apr 2026 10:03:37 +0530 Subject: [PATCH 1/2] Simplify bin_to_hexadecimal function Rewrote bin_to_hexadecimal to use Python's built-in int(s, 2) and hex() instead of a manual lookup table and bit-grouping loop. The old approach had a padding bug: inputs whose length was already a multiple of 4 got an extra 4 zeros prepended, so '1010' came out as '0x0a' instead of '0xa'. The new approach skips padding entirely and lets the stdlib handle it correctly. Also added a guard for "-" as input, which previously slipped past the empty-string check and raised an unhelpful ValueError about non-binary values. Removes BITS_TO_HEX, the divmod padding expression, and the grouping loop. Doctests updated to reflect the corrected output. --- conversions/binary_to_hexadecimal.py | 43 +++++----------------------- 1 file changed, 7 insertions(+), 36 deletions(-) diff --git a/conversions/binary_to_hexadecimal.py b/conversions/binary_to_hexadecimal.py index a3855bb70b52..fae3d2459673 100644 --- a/conversions/binary_to_hexadecimal.py +++ b/conversions/binary_to_hexadecimal.py @@ -1,31 +1,11 @@ -BITS_TO_HEX = { - "0000": "0", - "0001": "1", - "0010": "2", - "0011": "3", - "0100": "4", - "0101": "5", - "0110": "6", - "0111": "7", - "1000": "8", - "1001": "9", - "1010": "a", - "1011": "b", - "1100": "c", - "1101": "d", - "1110": "e", - "1111": "f", -} - - def bin_to_hexadecimal(binary_str: str) -> str: """ - Converting a binary string into hexadecimal using Grouping Method + Convert a binary string to hexadecimal. >>> bin_to_hexadecimal('101011111') '0x15f' >>> bin_to_hexadecimal(' 1010 ') - '0x0a' + '0xa' >>> bin_to_hexadecimal('-11101') '-0x1d' >>> bin_to_hexadecimal('a') @@ -37,30 +17,21 @@ def bin_to_hexadecimal(binary_str: str) -> str: ... ValueError: Empty string was passed to the function """ - # Sanitising parameter binary_str = str(binary_str).strip() - # Exceptions if not binary_str: raise ValueError("Empty string was passed to the function") + is_negative = binary_str[0] == "-" binary_str = binary_str[1:] if is_negative else binary_str - if not all(char in "01" for char in binary_str): - raise ValueError("Non-binary value was passed to the function") - binary_str = ( - "0" * (4 * (divmod(len(binary_str), 4)[0] + 1) - len(binary_str)) + binary_str - ) - - hexadecimal = [] - for x in range(0, len(binary_str), 4): - hexadecimal.append(BITS_TO_HEX[binary_str[x : x + 4]]) - hexadecimal_str = "0x" + "".join(hexadecimal) + if not binary_str or not all(char in "01" for char in binary_str): + raise ValueError("Non-binary value was passed to the function") - return "-" + hexadecimal_str if is_negative else hexadecimal_str + hex_str = "0x" + hex(int(binary_str, 2))[2:] + return "-" + hex_str if is_negative else hex_str if __name__ == "__main__": from doctest import testmod - testmod() From 9f2a343e09aab76526ecbbb17bce46d5cb6c4c2f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 21 Apr 2026 04:35:27 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/binary_to_hexadecimal.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conversions/binary_to_hexadecimal.py b/conversions/binary_to_hexadecimal.py index fae3d2459673..17d657a35f71 100644 --- a/conversions/binary_to_hexadecimal.py +++ b/conversions/binary_to_hexadecimal.py @@ -34,4 +34,5 @@ def bin_to_hexadecimal(binary_str: str) -> str: if __name__ == "__main__": from doctest import testmod + testmod()