-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Created octal_to_binary.py and fixed issue #8921 #8951
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
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9da661b
Create octal_to_binary.py
navanitnandakumar 56efdb8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 79eb033
Updated octal_to_binary.py
navanitnandakumar 7e4c060
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e57c0f5
Update octal_to_binary.py
navanitnandakumar d14d5fd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f1c6a65
Update octal_to_binary.py
navanitnandakumar c9e7bf6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b944eec
Update octal_to_binary.py
navanitnandakumar 6e742ab
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b8b3b8c
Update octal_to_binary.py
navanitnandakumar eedc593
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
def octal_to_binary(octal: str) -> str: | ||
""" | ||
Convert an octal value to its binary equivalent | ||
>>> octal_to_binary("") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Empty string 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("e") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Non-octal value was passed to the function | ||
>>> octal_to_binary(8) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Non-octal value was passed to the function | ||
>>> octal_to_binary("-e") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Non-octal value was passed to the function | ||
>>> octal_to_binary("-8") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Non-octal value was passed to the function | ||
>>> octal_to_binary("1") | ||
'0b1' | ||
>>> octal_to_binary("-1") | ||
'-0b1' | ||
>>> octal_to_binary("12") | ||
'0b1010' | ||
>>> octal_to_binary(" 12 ") | ||
'0b1010' | ||
>>> octal_to_binary("-45") | ||
'-0b100101' | ||
>>> octal_to_binary("-") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Non-octal value was passed to the function | ||
>>> octal_to_binary("0") | ||
'0b0' | ||
>>> octal_to_binary("-4055") | ||
'-0b100000101101' | ||
>>> octal_to_binary("2-0Fm") | ||
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 | ||
>>> octal_to_binary("19") | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Non-octal value was passed to the function | ||
""" | ||
oct_string = str(octal).strip() | ||
if not oct_string: | ||
raise ValueError("Empty string was passed to the function") | ||
is_negative = oct_string.startswith("-") | ||
if is_negative: | ||
oct_string = oct_string[1:] | ||
binary_num = "-0b" | ||
else: | ||
binary_num = "0b" | ||
if not oct_string.isdigit() or not all(0 <= int(char) <= 7 for char in oct_string): | ||
raise ValueError("Non-octal value was passed to the function") | ||
binary_num += str(bin(int(oct_string, 8)))[2:] | ||
return binary_num | ||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 still converting from octal to decimal to binary.
int
converts the input from octal to decimal, andbin
converts that value from decimal to binary.