-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added function to convert from decimal to another base #2087
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
Changes from 7 commits
724662a
1a6ccc8
9953dc0
5efa4cf
02bb070
3759fba
e10f69b
ce8841a
89b8f52
c900fad
c765bf9
3b8854a
0d236d4
ddde621
e2c9244
933c039
685adde
4adc6dc
41a5de9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
"""Convert a positive Decimal Number to Any Other Representation""" | ||
|
||
|
||
def decimal_to_any(num: int, base: int) -> str: | ||
|
||
""" | ||
Convert a Integer Decimal Number to a Binary Number as str. | ||
>>> decimal_to_any(0, 2) | ||
'0' | ||
>>> decimal_to_any(5, 4) | ||
'11' | ||
>>> decimal_to_any(20, 3) | ||
'202' | ||
>>> decimal_to_any(58, 16) | ||
'3A' | ||
>>> # 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: 'float' object cannot be interpreted as an integer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The exception text should match the exception raised by |
||
>>> #a float base will error | ||
Kevin-Escobedo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> 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 | ||
""" | ||
if isinstance(num, float): | ||
raise TypeError("'float' object cannot be interpreted as an integer") | ||
if num < 0: | ||
raise ValueError("parameter must be positive int") | ||
if type(base) == str: | ||
Kevin-Escobedo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise TypeError("'str' object cannot be interpreted as an integer") | ||
if type(base) == float: | ||
Kevin-Escobedo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise TypeError("'float' object cannot be interpreted as an integer") | ||
|
||
HEXADECIMAL = {'10': 'A', '11': 'B', '12': 'C', '13': 'D', '14': 'E', '15': 'F'} | ||
new_value = "" | ||
mod = 0 | ||
div = 0 | ||
if base in (0, 1): | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bad input should raise exceptions, not return None.
|
||
while div not in (0, 1): | ||
div, mod = divmod(num, base) | ||
if base == 16 and 9 < mod < 16: | ||
actual_value = HEXADECIMAL[str(mod)] | ||
mod = actual_value | ||
new_value += str(mod) | ||
div = num // base | ||
num = div | ||
if div == 0: | ||
if base != 16: | ||
return str(new_value[::-1]) | ||
else: | ||
if new_value[::-1] in HEXADECIMAL: | ||
return HEXADECIMAL[new_value[::-1]] | ||
return new_value[::-1] | ||
elif div == 1: | ||
new_value += str(div) | ||
if base != 16: | ||
return str(new_value[::-1]) | ||
else: | ||
if new_value[::-1] in HEXADECIMAL: | ||
return HEXADECIMAL[new_value[::-1]] | ||
return new_value[::-1] | ||
|
||
return new_value[::-1] | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.