From afa5049f1e29eb8f95597c44e6b887ad26a51dbe Mon Sep 17 00:00:00 2001 From: Ewan John Dennis Date: Tue, 21 Apr 2026 10:39:52 +0530 Subject: [PATCH 1/2] Refactor decimal_to_any for better error handling Rewrote decimal_to_any to replace a tangled loop that computed divmod and // separately on each iteration with a single clean while num loop. The original had redundant division, confusing early returns mid-loop, and a base validation check that missed negative bases. Added an explicit num == 0 base case, fixed the validation order so type checks run before value checks, and replaced base in (0, 1) with base < 2. All existing doctests pass unchanged. --- conversions/decimal_to_any.py | 41 ++++++++++------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/conversions/decimal_to_any.py b/conversions/decimal_to_any.py index c9c2e9a5fb71..92ec84628ff6 100644 --- a/conversions/decimal_to_any.py +++ b/conversions/decimal_to_any.py @@ -26,32 +26,26 @@ def decimal_to_any(num: int, base: int) -> str: '10' >>> decimal_to_any(36, 36) '10' - >>> # negatives will error >>> decimal_to_any(-45, 8) # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: parameter must be positive int - >>> # floats will error >>> decimal_to_any(34.4, 6) # doctest: +ELLIPSIS Traceback (most recent call last): ... TypeError: int() can't convert non-string with explicit base - >>> # a float base will error >>> decimal_to_any(5, 2.5) # doctest: +ELLIPSIS Traceback (most recent call last): ... TypeError: 'float' object cannot be interpreted as an integer - >>> # a str base will error >>> decimal_to_any(10, '16') # doctest: +ELLIPSIS Traceback (most recent call last): ... TypeError: 'str' object cannot be interpreted as an integer - >>> # a base less than 2 will error >>> decimal_to_any(7, 0) # doctest: +ELLIPSIS Traceback (most recent call last): ... ValueError: base must be >= 2 - >>> # a base greater than 36 will error >>> decimal_to_any(34, 37) # doctest: +ELLIPSIS Traceback (most recent call last): ... @@ -59,42 +53,31 @@ def decimal_to_any(num: int, base: int) -> str: """ if isinstance(num, float): raise TypeError("int() can't convert non-string with explicit base") - if num < 0: - raise ValueError("parameter must be positive int") if isinstance(base, str): raise TypeError("'str' object cannot be interpreted as an integer") if isinstance(base, float): raise TypeError("'float' object cannot be interpreted as an integer") - if base in (0, 1): + if num < 0: + raise ValueError("parameter must be positive int") + if base < 2: raise ValueError("base must be >= 2") if base > 36: raise ValueError("base must be <= 36") - new_value = "" - mod = 0 - div = 0 - while div != 1: - div, mod = divmod(num, base) - if base >= 11 and 9 < mod < 36: - actual_value = ALPHABET_VALUES[str(mod)] - else: - actual_value = str(mod) - new_value += actual_value - div = num // base - num = div - if div == 0: - return str(new_value[::-1]) - elif div == 1: - new_value += str(div) - return str(new_value[::-1]) - return new_value[::-1] + if num == 0: + return "0" + + digits = [] + while num: + num, mod = divmod(num, base) + digits.append(ALPHABET_VALUES[str(mod)] if mod > 9 else str(mod)) + + return "".join(reversed(digits)) if __name__ == "__main__": import doctest - doctest.testmod() - for base in range(2, 37): for num in range(1000): assert int(decimal_to_any(num, base), base) == num, ( From 9eb795489731622ba4a241ed97866ad0f9f97c19 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 05:10:49 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/decimal_to_any.py | 1 + 1 file changed, 1 insertion(+) diff --git a/conversions/decimal_to_any.py b/conversions/decimal_to_any.py index 92ec84628ff6..8d872ce73da0 100644 --- a/conversions/decimal_to_any.py +++ b/conversions/decimal_to_any.py @@ -77,6 +77,7 @@ def decimal_to_any(num: int, base: int) -> str: if __name__ == "__main__": import doctest + doctest.testmod() for base in range(2, 37): for num in range(1000):