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

Added function to convert from decimal to another base #2087

Merged
merged 19 commits into from
Jun 11, 2020

Conversation

Kevin-Escobedo
Copy link
Contributor

@Kevin-Escobedo Kevin-Escobedo commented Jun 8, 2020

Describe your change:

I added a new function that converts from decimal to any other base.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@TravisBuddy
Copy link

Hey @Kevin-Escobedo,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: 1824b6e0-a9dc-11ea-9161-4f77e253115e

Copy link
Member

@cclauss cclauss left a 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.

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

Kevin-Escobedo and others added 6 commits June 8, 2020 19:17
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>
Kevin-Escobedo and others added 2 commits June 8, 2020 19:26
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>
@TravisBuddy
Copy link

Travis tests have failed

Hey @Kevin-Escobedo,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: d8305090-a9f8-11ea-9161-4f77e253115e

@TravisBuddy
Copy link

Travis tests have failed

Hey @Kevin-Escobedo,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: 089959c0-a9f9-11ea-9161-4f77e253115e

Comment on lines 50 to 51
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

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.

@TravisBuddy
Copy link

Hey @Kevin-Escobedo,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: ae066990-a9fc-11ea-9161-4f77e253115e

@TravisBuddy
Copy link

Hey @Kevin-Escobedo,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: e3b00d80-a9fc-11ea-9161-4f77e253115e

@TravisBuddy
Copy link

Hey @Kevin-Escobedo,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: 3628b940-a9fd-11ea-9161-4f77e253115e

@Kevin-Escobedo Kevin-Escobedo requested a review from cclauss June 9, 2020 03:21
@TheSuperNoob
Copy link
Contributor

This will only work on bases up to 16, so maybe we should guard against entering a base higher than 16?

@cclauss
Copy link
Member

cclauss commented Jun 9, 2020

@TheSuperNoob
Copy link
Contributor

TheSuperNoob commented Jun 9, 2020

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 HEXADECIMAL dictionary to add support up to 36 like Python's standard library does.

@Kevin-Escobedo
Copy link
Contributor Author

Good idea! I'll start working on that

@Kevin-Escobedo
Copy link
Contributor Author

Adding support for conversions up to base-36 was actually much simpler than I thought it would be. Anyway, looking forward to your review

@TravisBuddy
Copy link

Hey @Kevin-Escobedo,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

View build log

TravisBuddy Request Identifier: ab0c0780-aabb-11ea-9064-4d6590cbb359

@TravisBuddy
Copy link

Hey @Kevin-Escobedo,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

View build log

TravisBuddy Request Identifier: ec0b2b70-aabc-11ea-9064-4d6590cbb359

@TravisBuddy
Copy link

Hey @Kevin-Escobedo,
Your changes look good to me!

View build log

TravisBuddy Request Identifier: 84d87d70-aabe-11ea-9064-4d6590cbb359

@cclauss
Copy link
Member

cclauss commented Jun 11, 2020

@Kevin-Escobedo Thanks for this submission. Very cool. I think things look good up until base 11.
If we add these lines to the end of __main__...

    for base in range(2, 37):
        print(f"Testing {base = }...")
        for num in range(1000):
            assert int(decimal_to_any(num, base), base) == num, (
                num, base, decimal_to_any(num, base),
                int(decimal_to_any(num, base), base)
            )

...we get:

Testing base = 2...
Testing base = 3...
Testing base = 4...
Testing base = 5...
Testing base = 6...
Testing base = 7...
Testing base = 8...
Testing base = 9...
Testing base = 10...
Testing base = 11...
Traceback (most recent call last):
  File "decimal_to_any.py", line 105, in <module>
    assert int(decimal_to_any(num, base), base) == num, (
AssertionError: (10, 11, '01', 1)

@cclauss
Copy link
Member

cclauss commented Jun 11, 2020

Another interesting test is:

    for base in range(2, 37):
        assert decimal_to_any(base, base) == '10', base  # fails at 16

@Kevin-Escobedo
Copy link
Contributor Author

@cclauss Thanks for testing and pointing out the flaws in my code, I feel it's really helped improve the overall algorithm!

Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

AWESOME WORK HERE!!!

@cclauss cclauss merged commit 19c3871 into TheAlgorithms:master Jun 11, 2020
@TravisBuddy
Copy link

Hey @Kevin-Escobedo,
Something went wrong with the build.

TravisCI finished with status errored, which means the build failed because of something unrelated to the tests, such as a problem with a dependency or the build process itself.

View build log

TravisBuddy Request Identifier: bb0310b0-abfc-11ea-a7dd-93c9ffbfb007

Kevin-Escobedo added a commit to Kevin-Escobedo/Python that referenced this pull request Jun 11, 2020
@Kevin-Escobedo
Copy link
Contributor Author

@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?

@cclauss
Copy link
Member

cclauss commented Jun 11, 2020

No troubles... psf/black will fix it automatically when a maintainer runs the autoblack GitHub Action.

stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
…#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>
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

Successfully merging this pull request may close these issues.

4 participants