Skip to content

Modified octal_to_decimal #3243

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

Closed
wants to merge 2 commits into from
Closed

Modified octal_to_decimal #3243

wants to merge 2 commits into from

Conversation

cybov
Copy link
Contributor

@cybov cybov commented Oct 12, 2020

Describe your 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}.

@cybov cybov requested a review from cclauss as a code owner October 12, 2020 22:23
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.

I am not seeing an improvement in these changes.

@@ -9,26 +9,40 @@ def oct_to_decimal(oct_string: str) -> int:
>>> oct_to_decimal("-45")
-37
>>> oct_to_decimal("2-0Fm")
Traceback (most recent call last):
Copy link
Member

Choose a reason for hiding this comment

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

These are optional and do not add much value

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 added them because the doctest failed without them.

ValueError: Non-octal value was passed to the function
"""
# Strip oct_string of whitespaces
Copy link
Member

Choose a reason for hiding this comment

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

This comment repeats what the code says. Comments should not be about what the code is doing, but should be about why we are doing something. Why are we removing whitespace? Why are we removing the minus sign.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

raise ValueError("Empty string was passed to the function")
raise ValueError("Empty string value was passed to the function")

# Check if oct_string is a negative value
Copy link
Member

Choose a reason for hiding this comment

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

This comment repeats what the code says.

is_negative = oct_string[0] == "-"
if is_negative:
# Remove (-) from oct_string
Copy link
Member

Choose a reason for hiding this comment

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

This comment repeats what the code says.

if not all(0 <= int(char) <= 7 for char in oct_string):

# check if oct_string is an octal value and convert
if oct_string.isdecimal() and all(0 <= int(char) <= 7 for char in oct_string):
Copy link
Member

Choose a reason for hiding this comment

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

These are redundant tests. The first one cannon pass if the second one does not pass. The original formulation is more consise.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for reviewing. In the original, int(char) in the iterable passed to the all() function returns error that is not handled by the ValueError raised when char is not an integer value. It's why I modified it so.

Copy link
Member

@cclauss cclauss Oct 13, 2020

Choose a reason for hiding this comment

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

A ValueError is still raised. This is why I encourage doctests. It is not necessary that we raise all exceptions ourselves. It is only important that an appropriate exception is raised when garbage data is received.

>>> int('a')
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'a'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok

decimal_number = -decimal_number
return decimal_number
else:
# else raise exception
Copy link
Member

Choose a reason for hiding this comment

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

This comment repeats what the code says.

@cclauss cclauss closed this Oct 12, 2020
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.

2 participants