Skip to content

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

Merged
merged 19 commits into from
Jun 11, 2020
Merged
Changes from 7 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions conversions/decimal_to_any.py
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.
Copy link
Member

@cclauss cclauss Jun 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Convert a Integer Decimal Number to a Binary Number as str.
Convert a positive integer to another base 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
Copy link
Member

@cclauss cclauss Jun 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception text should match the exception raised by int(34.4, base=6)
https://docs.python.org/3/library/functions.html?highlight=open#int

>>> #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
"""
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:
raise TypeError("'str' object cannot be interpreted as an integer")
if type(base) == float:
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bad input should raise exceptions, not return None.

>>> int('77', base=0)
77
>>> int('77', base=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: int() base must be >= 2 and <= 36, or 0

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()