Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dec2base 函数出问题 #150

Open
StevenJinyanCheng opened this issue Nov 26, 2024 · 1 comment
Open

dec2base 函数出问题 #150

StevenJinyanCheng opened this issue Nov 26, 2024 · 1 comment

Comments

@StevenJinyanCheng
Copy link

(base) PS C:\Users\Steve> python
Python 3.11.7 | packaged by Anaconda, Inc. | (main, Dec 15 2023, 18:05:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from cyaron import *
>>> dec2base
<function dec2base at 0x0000020A73598900>
>>> dec2base(123,11)
'102'
>>> dec2base(123,111)
'1C'
>>> dec2base(1234,111)
'BD'
>>> dec2base(12345,111)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Steve\AppData\Roaming\Python\Python311\site-packages\cyaron\math.py", line 494, in dec2base
    return dec2base(n // base, base) + convert_string[n % base]
                                       ~~~~~~~~~~~~~~^^^^^^^^^^
IndexError: string index out of range
>>>

本应出ValueError
可改为:

#source:
# http://interactivepython.org/runestone/static/pythonds/Recursion/pythondsConvertinganIntegertoaStringinAnyBase.html
def dec2base(n: int, base: int) -> str:
    """
    Convert a decimal number to a specified base.
    Args:
        n: The decimal number to convert.
        base: The base to convert the number to. Must be between 2 and 16.
    Returns:
        The number represented in the specified base.
    Raises:
        ValueError: If the base is not between 2 and 16.
    """
    # 增加下列几行
    if (base < 2) or (base > 16):
        raise ValueError("base is not between 2 and 16. ")
    # 结束
    convert_string = "0123456789ABCDEF"
    if n < base:
        return convert_string[n]
    return dec2base(n // base, base) + convert_string[n % base]
@weilycoder
Copy link
Contributor

weilycoder commented Nov 26, 2024

现在维护者是不是认为报错就行?其他文件中甚至有用 Exception 抛出异常的。

你可以去开个 pr,或者看他啥时候心情好了修掉。

但是你开 pr 可能也得看他啥时候有时间 merge

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants