-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Added function to convert from decimal to another base #2087
Conversation
Hey @Kevin-Escobedo, TravisBuddy Request Identifier: 1824b6e0-a9dc-11ea-9161-4f77e253115e |
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.
This is super cool. Thanks for your contribution.
conversions/decimal_to_any.py
Outdated
>>> 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 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
Changed type() to isinstance() Co-authored-by: Christian Clauss <cclauss@me.com>
Changed to base in (0, 1) Co-authored-by: Christian Clauss <cclauss@me.com>
Updated to div not in (0, 1) Co-authored-by: Christian Clauss <cclauss@me.com>
Updated to make condition clearer Co-authored-by: Christian Clauss <cclauss@me.com>
Using divmod() instead of % operator Co-authored-by: Christian Clauss <cclauss@me.com>
Improved readability on a docstring test Co-authored-by: Christian Clauss <cclauss@me.com>
Changed use of type() to isinstance() Co-authored-by: Christian Clauss <cclauss@me.com>
Changed from use of type() to isinstance() Co-authored-by: Christian Clauss <cclauss@me.com>
Travis tests have failedHey @Kevin-Escobedo, TravisBuddy Request Identifier: d8305090-a9f8-11ea-9161-4f77e253115e |
Travis tests have failedHey @Kevin-Escobedo, TravisBuddy Request Identifier: 089959c0-a9f9-11ea-9161-4f77e253115e |
conversions/decimal_to_any.py
Outdated
if base in (0, 1): | ||
return |
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.
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
conversions/decimal_to_any.py
Outdated
def decimal_to_any(num: int, base: int) -> str: | ||
|
||
""" | ||
Convert a Integer Decimal Number to a Binary Number as str. |
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.
Convert a Integer Decimal Number to a Binary Number as str. | |
Convert a positive integer to another base as str. |
Added space to docstring test Co-authored-by: Christian Clauss <cclauss@me.com>
Hey @Kevin-Escobedo, TravisBuddy Request Identifier: ae066990-a9fc-11ea-9161-4f77e253115e |
Hey @Kevin-Escobedo, TravisBuddy Request Identifier: e3b00d80-a9fc-11ea-9161-4f77e253115e |
Hey @Kevin-Escobedo, TravisBuddy Request Identifier: 3628b940-a9fd-11ea-9161-4f77e253115e |
This will only work on bases up to 16, so maybe we should guard against entering a base higher than 16? |
https://docs.python.org/3/library/functions.html?highlight=open#int goes up to base 32. |
Yes, but this algorithm does not currently have support for any more symbols than base 16. (F) However you could add the rest of the alphabet to the |
Good idea! I'll start working on that |
Adding support for conversions up to base-36 was actually much simpler than I thought it would be. Anyway, looking forward to your review |
Hey @Kevin-Escobedo, TravisCI finished with status TravisBuddy Request Identifier: ab0c0780-aabb-11ea-9064-4d6590cbb359 |
Hey @Kevin-Escobedo, TravisCI finished with status TravisBuddy Request Identifier: ec0b2b70-aabc-11ea-9064-4d6590cbb359 |
Hey @Kevin-Escobedo, TravisBuddy Request Identifier: 84d87d70-aabe-11ea-9064-4d6590cbb359 |
@Kevin-Escobedo Thanks for this submission. Very cool. I think things look good up until base 11.
...we get:
|
Another interesting test is:
|
@cclauss Thanks for testing and pointing out the flaws in my code, I feel it's really helped improve the overall algorithm! |
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.
AWESOME WORK HERE!!!
Hey @Kevin-Escobedo, TravisCI finished with status TravisBuddy Request Identifier: bb0310b0-abfc-11ea-a7dd-93c9ffbfb007 |
@cclauss Wait, there was an error with the TravisCI build because of an issue with parentheses not having the same indentation, I tried to fix it, but the pull request was already approved, does it matter? |
No troubles... psf/black will fix it automatically when a maintainer runs the autoblack GitHub Action. |
…#2087) * Added function to convert from decimal to another base * Update conversions/decimal_to_any.py Changed type() to isinstance() Co-authored-by: Christian Clauss <cclauss@me.com> * Update conversions/decimal_to_any.py Changed to base in (0, 1) Co-authored-by: Christian Clauss <cclauss@me.com> * Update conversions/decimal_to_any.py Updated to div not in (0, 1) Co-authored-by: Christian Clauss <cclauss@me.com> * Update conversions/decimal_to_any.py Updated to make condition clearer Co-authored-by: Christian Clauss <cclauss@me.com> * Update conversions/decimal_to_any.py Using divmod() instead of % operator Co-authored-by: Christian Clauss <cclauss@me.com> * Update conversions/decimal_to_any.py Improved readability on a docstring test Co-authored-by: Christian Clauss <cclauss@me.com> * Update conversions/decimal_to_any.py Changed use of type() to isinstance() Co-authored-by: Christian Clauss <cclauss@me.com> * Update conversions/decimal_to_any.py Changed from use of type() to isinstance() Co-authored-by: Christian Clauss <cclauss@me.com> * Made changes and improved function * Update conversions/decimal_to_any.py Added space to docstring test Co-authored-by: Christian Clauss <cclauss@me.com> * Changed action for bad input * Added support for conversions up to base 36 (TheAlgorithms#2087) * Added support for conversions up to base 36 and renamed HEXADECIMAL dict (TheAlgorithms#2087) * Fixed whitespace issue (TheAlgorithms#2087) * Fixed issue with line length (TheAlgorithms#2087) * Fixed issue with conversions past base-10 failing (TheAlgorithms#2087) * Added more robust testing (TheAlgorithms#2087) Co-authored-by: Christian Clauss <cclauss@me.com>
Describe your change:
I added a new function that converts from decimal to any other base.
Checklist:
Fixes: #{$ISSUE_NO}
.