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

Octal to Binary Convert #8949

Merged
merged 25 commits into from
Aug 20, 2023
Merged

Octal to Binary Convert #8949

merged 25 commits into from
Aug 20, 2023

Conversation

BamaCharanChhandogi
Copy link
Member

@BamaCharanChhandogi BamaCharanChhandogi commented Aug 11, 2023

Describe your change:

  • 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 include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper bot added the require type hints https://docs.python.org/3/library/typing.html label Aug 11, 2023
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@@ -0,0 +1,28 @@
def octal_to_binary(octal_number):

Choose a reason for hiding this comment

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

Please provide return type hint for the function: octal_to_binary. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: octal_number

Copy link
Member Author

Choose a reason for hiding this comment

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

check again

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Aug 11, 2023
@algorithms-keeper algorithms-keeper bot removed the require type hints https://docs.python.org/3/library/typing.html label Aug 11, 2023
Copy link
Contributor

@rohan472000 rohan472000 left a comment

Choose a reason for hiding this comment

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

You have used int() and bin() functions which are built-in functions in Python used for converting between different number bases.

In this repo, all function should be implemented from scratch without using built-in functions.

Copy link
Contributor

@tianyizheng02 tianyizheng02 left a comment

Choose a reason for hiding this comment

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

As already stated, this implementation just uses built-in Python functions to do all the work of the conversion. Plus, we'd rather have an implementation that converts directly from octal to binary without resorting to converting to decimal first.

@algorithms-keeper algorithms-keeper bot added require tests Tests [doctest/unittest/pytest] are required require type hints https://docs.python.org/3/library/typing.html labels Aug 13, 2023
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@@ -0,0 +1,29 @@
def octal_to_binary(octal_number):

Choose a reason for hiding this comment

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

Please provide return type hint for the function: octal_to_binary. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file conversions/octal_to_binary.py, please provide doctest for the function octal_to_binary

Please provide type hint for the parameter: octal_number

@BamaCharanChhandogi
Copy link
Member Author

Check!

@algorithms-keeper algorithms-keeper bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Aug 13, 2023
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@tianyizheng02 tianyizheng02 linked an issue Aug 13, 2023 that may be closed by this pull request
@algorithms-keeper algorithms-keeper bot removed the require type hints https://docs.python.org/3/library/typing.html label Aug 17, 2023
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

Copy link
Contributor

@tianyizheng02 tianyizheng02 left a comment

Choose a reason for hiding this comment

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

Minor tweaks in the header docstring, but other than that LGTM

Comment on lines 5 to 8
reference for better understand

URL: https://en.wikipedia.org/wiki/Binary_number
URL: https://en.wikipedia.org/wiki/Octal
Copy link
Contributor

Choose a reason for hiding this comment

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

Formatting and grammar fixes

Suggested change
reference for better understand
URL: https://en.wikipedia.org/wiki/Binary_number
URL: https://en.wikipedia.org/wiki/Octal
References for better understanding:
https://en.wikipedia.org/wiki/Binary_number
https://en.wikipedia.org/wiki/Octal

Comment on lines 13 to 37
binary_number = ""
octal_digits = "01234567"

"""
ValueError: String to the function
>>> oct_to_decimal("Av")
Traceback (most recent call last):
...
ValueError: Non-octal value was passed to the function
>>> oct_to_decimal("90")
Traceback (most recent call last):
...
ValueError: Special Character was passed to the function
>>> oct_to_decimal("#$")
Traceback (most recent call last):
...
ValueError: Empty String was passed to the function
>>> oct_to_decimal("")
...
ValueError: octal value was passed to the function
>>> oct_to_decimal("17")
001111
>>> oct_to_decimal("7")
111
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
binary_number = ""
octal_digits = "01234567"
"""
ValueError: String to the function
>>> oct_to_decimal("Av")
Traceback (most recent call last):
...
ValueError: Non-octal value was passed to the function
>>> oct_to_decimal("90")
Traceback (most recent call last):
...
ValueError: Special Character was passed to the function
>>> oct_to_decimal("#$")
Traceback (most recent call last):
...
ValueError: Empty String was passed to the function
>>> oct_to_decimal("")
...
ValueError: octal value was passed to the function
>>> oct_to_decimal("17")
001111
>>> oct_to_decimal("7")
111
"""
"""
ValueError: String to the function
>>> oct_to_decimal("Av")
Traceback (most recent call last):
...
ValueError: Non-octal value was passed to the function
>>> oct_to_decimal("90")
Traceback (most recent call last):
...
ValueError: Special Character was passed to the function
>>> oct_to_decimal("#$")
Traceback (most recent call last):
...
ValueError: Empty String was passed to the function
>>> oct_to_decimal("")
...
ValueError: octal value was passed to the function
>>> oct_to_decimal("17")
001111
>>> oct_to_decimal("7")
111
"""
binary_number = ""
octal_digits = "01234567"

Just noticed that the docstring is in the wrong place—I don't think these tests even pass because the error messages are wrong

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Aug 18, 2023
Copy link
Contributor

@tianyizheng02 tianyizheng02 left a comment

Choose a reason for hiding this comment

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

Please fix your broken doctests—they're breaking because they're wrongly formatted and because your function never raises the right ValueError in each case. Refer to the documentation for the doctest library for more info.

References for better understanding:
https://en.wikipedia.org/wiki/Binary_number
https://en.wikipedia.org/wiki/Octal

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change

Please remove this empty line

@BamaCharanChhandogi
Copy link
Member Author

I have completed all documents and test cases.
but what is "process completed with exit code 1"?

@tianyizheng02
Copy link
Contributor

I have completed all documents and test cases. but what is "process completed with exit code 1"?

It means the build experienced an error. See the build logs:

=================================== FAILURES ===================================
____________ [doctest] conversions.octal_to_binary.octal_to_binary _____________
012 
013     Convert an Octal number to Binary.
014 
015     ValueError: Non-octal value was passed to the function
016     >>> octal_to_binary("Av")
Differences (unified diff with -expected +actual):
    @@ -1,3 +1,8 @@
     Traceback (most recent call last):
    -    ...
    -ValueError: Non-octal value was passed to the function
    +  File "/opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/doctest.py", line 1351, in __run
    +    exec(compile(example.source, filename, "single",
    +  File "<doctest conversions.octal_to_binary.octal_to_binary[0]>", line 1, in <module>
    +    octal_to_binary("Av")
    +  File "/home/runner/work/Python/Python/conversions/octal_to_binary.py", line 46, in octal_to_binary
    +    raise ValueError("Special Character was passed to the function")
    +ValueError: Special Character was passed to the function

It was expecting to get ValueError: Non-octal value was passed to the function but it instead got ValueError: Special Character was passed to the function

'111'
"""
if not octal_number:
raise ValueError("Empty String was passed to the function")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
raise ValueError("Empty String was passed to the function")
raise ValueError("Empty string was passed to the function")

Comment on lines 44 to 50
for digit in octal_number:
if not digit.isdigit():
raise ValueError("Special Character was passed to the function")

if digit < "0" or digit > "7":
raise ValueError("Non-octal value was passed to the function")

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for digit in octal_number:
if not digit.isdigit():
raise ValueError("Special Character was passed to the function")
if digit < "0" or digit > "7":
raise ValueError("Non-octal value was passed to the function")

These checks are unnecessary because you're already checking each character in the for-loop

octal_digits = "01234567"
for digit in octal_number:
if digit not in octal_digits:
raise ValueError("Octal value was passed to the function")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
raise ValueError("Octal value was passed to the function")
raise ValueError("Non-octal value was passed to the function")

This error message is incorrect

Comment on lines 12 to 40
"""
Convert an Octal number to Binary.

ValueError: Non-octal value was passed to the function
>>> octal_to_binary("Av")
Traceback (most recent call last):
...
ValueError: Non-octal value was passed to the function

ValueError: Special Character was passed to the function
>>> octal_to_binary("@#")
Traceback (most recent call last):
...
ValueError: Special Character was passed to the function

ValueError: Empty String was passed to the function
>>> octal_to_binary("")
Traceback (most recent call last):
...
ValueError: Empty String was passed to the function

ValueError: Octal value was passed to the function
>>> octal_to_binary("17")
'001111'

ValueError: Octal value was passed to the function
>>> octal_to_binary("7")
'111'
"""
Copy link
Contributor

@tianyizheng02 tianyizheng02 Aug 19, 2023

Choose a reason for hiding this comment

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

Suggested change
"""
Convert an Octal number to Binary.
ValueError: Non-octal value was passed to the function
>>> octal_to_binary("Av")
Traceback (most recent call last):
...
ValueError: Non-octal value was passed to the function
ValueError: Special Character was passed to the function
>>> octal_to_binary("@#")
Traceback (most recent call last):
...
ValueError: Special Character was passed to the function
ValueError: Empty String was passed to the function
>>> octal_to_binary("")
Traceback (most recent call last):
...
ValueError: Empty String was passed to the function
ValueError: Octal value was passed to the function
>>> octal_to_binary("17")
'001111'
ValueError: Octal value was passed to the function
>>> octal_to_binary("7")
'111'
"""
"""
Convert an Octal number to Binary.
>>> octal_to_binary("17")
'001111'
>>> octal_to_binary("7")
'111'
>>> octal_to_binary("Av")
Traceback (most recent call last):
...
ValueError: Non-octal value was passed to the function
>>> octal_to_binary("@#")
Traceback (most recent call last):
...
ValueError: Non-octal value was passed to the function
>>> octal_to_binary("")
Traceback (most recent call last):
...
ValueError: Empty string was passed to the function
"""

Place the most import doctests at the top (the ones that demonstrate correct output) and edge cases should come after those. Also, there's no need to describe the error when the doctests already shows the error messages.

@BamaCharanChhandogi
Copy link
Member Author

I understand. I am saying that If I remove the Special character test case!
I don't think there need for this type of algo.

@tianyizheng02
Copy link
Contributor

My suggestions do not remove the special character test case. That doctest is still there. My point is that you had already handled special characters in your for-loop, so there's no need to handle it ahead of time before the for-loop.

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Aug 20, 2023
@BamaCharanChhandogi
Copy link
Member Author

Hello, @tianyizheng02 Finally could you check?

Copy link
Contributor

@tianyizheng02 tianyizheng02 left a comment

Choose a reason for hiding this comment

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

Just some minor formatting issues, but other than that LGTM

@algorithms-keeper algorithms-keeper bot removed the require tests Tests [doctest/unittest/pytest] are required label Aug 20, 2023
@tianyizheng02 tianyizheng02 merged commit 062957e into TheAlgorithms:master Aug 20, 2023
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Aug 20, 2023
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* Octal to Binary Convert

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* mention return type

* code scratch

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* mentioned return type

* remove comment

* added documention and some test cases

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add another test case

* fixes documention

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Documention and test cases added

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* documention problem solved

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* error in exit 1

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Apply suggestions from code review

---------

Co-authored-by: BamaCharanChhandogi <b.c.chhandogi@gmailcom>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
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.

Feat:Octal To Binary Converter
3 participants