-
-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
724662a
Added function to convert from decimal to another base
Kevin-Escobedo 1a6ccc8
Update conversions/decimal_to_any.py
Kevin-Escobedo 9953dc0
Update conversions/decimal_to_any.py
Kevin-Escobedo 5efa4cf
Update conversions/decimal_to_any.py
Kevin-Escobedo 02bb070
Update conversions/decimal_to_any.py
Kevin-Escobedo 3759fba
Update conversions/decimal_to_any.py
Kevin-Escobedo e10f69b
Update conversions/decimal_to_any.py
Kevin-Escobedo ce8841a
Update conversions/decimal_to_any.py
Kevin-Escobedo 89b8f52
Update conversions/decimal_to_any.py
Kevin-Escobedo c900fad
Made changes and improved function
Kevin-Escobedo c765bf9
Update conversions/decimal_to_any.py
Kevin-Escobedo 3b8854a
Changed action for bad input
Kevin-Escobedo 0d236d4
Merge branch 'develop' of github.com:Kevin-Escobedo/Python into develop
Kevin-Escobedo ddde621
Added support for conversions up to base 36 (#2087)
Kevin-Escobedo e2c9244
Added support for conversions up to base 36 and renamed HEXADECIMAL d…
Kevin-Escobedo 933c039
Fixed whitespace issue (#2087)
Kevin-Escobedo 685adde
Fixed issue with line length (#2087)
Kevin-Escobedo 4adc6dc
Fixed issue with conversions past base-10 failing (#2087)
Kevin-Escobedo 41a5de9
Added more robust testing (#2087)
Kevin-Escobedo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
Kevin-Escobedo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 type(num) == float: | ||
Kevin-Escobedo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 == 0 or base == 1: | ||
Kevin-Escobedo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return | ||
while ((div != 1) or (div != 0)): | ||
Kevin-Escobedo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mod = num % base | ||
Kevin-Escobedo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if base == 16 and (mod > 9 and mod < 16): | ||
Kevin-Escobedo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.