Skip to content

Fixed issue #1368 #1482

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 4 commits into from
Dec 8, 2019
Merged
Changes from all commits
Commits
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
10 changes: 7 additions & 3 deletions conversions/decimal_to_octal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
# https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/DecimalToOctal.js


def decimal_to_octal(num):
"""Convert a Decimal Number to an Octal Number."""
def decimal_to_octal(num: int) -> str:
"""Convert a Decimal Number to an Octal Number.

>>> all(decimal_to_octal(i) == oct(i) for i in (0, 2, 8, 64, 65, 216, 255, 256, 512))
True
"""
octal = 0
counter = 0
while num > 0:
Expand All @@ -16,7 +20,7 @@ def decimal_to_octal(num):
counter += 1
Copy link
Member

@cclauss cclauss Oct 28, 2019

Choose a reason for hiding this comment

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

Let’s add some doctests because the current function is not quite right in a few ways... Please change the top of the function to:

def decimal_to_octal(num: int) -> str:
    """Convert a Decimal Number to an Octal Number.
    
    >>> for i in (0, 2, 8, 64, 65, 216, 255, 256, 512):
    ...     decimal_to_octal(i) == oct(i)
    True
    """

Then change line 19 to something like return f"0o{int(octal)}" and then do python3 -m doctest -v conversions/decimal_to_octal.py to make sure those tests pass.

@PatOnTheBack

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test case fails, but when I do it manually there is no error as such

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed the code as suggested nevertheless

num = math.floor(num / 8) # basically /= 8 without remainder if any
# This formatting removes trailing '.0' from `octal`.
return "{0:g}".format(float(octal))
return f"0o{int(octal)}"


def main():
Expand Down