-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
bin_to_octal #2431
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
bin_to_octal #2431
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7cf647f
bin_to_octal
mohammadreza490 66830ad
Update bin_to_octal
mohammadreza490 cf48d72
Update conversions/bin_to_octal
mohammadreza490 7e81026
Update conversions/bin_to_octal
mohammadreza490 0e42dbb
Update conversions/bin_to_octal
mohammadreza490 39c00ea
Update conversions/bin_to_octal
mohammadreza490 3f60a8c
Rename bin_to_octal to bin_to_octal.py
mohammadreza490 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 hidden or 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,43 @@ | ||
""" | ||
The function below will convert any binary string to the octal equivalent. | ||
|
||
>>> bin_to_octal("1111") | ||
'17' | ||
|
||
>>> bin_to_octal("101010101010011") | ||
'52523' | ||
|
||
mohammadreza490 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> bin_to_octal("") | ||
ValueError("Empty string was passed to the function") | ||
|
||
mohammadreza490 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> bin_to_octal("a-1") | ||
ValueError("Non-binary value was passed to the function") | ||
|
||
mohammadreza490 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
|
||
|
||
def bin_to_octal(bin_string: str) -> str: | ||
if not all("0" <= char <= "1" for char in bin_string): | ||
mohammadreza490 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise ValueError("Non-binary value was passed to the function") | ||
if not bin_string: | ||
raise ValueError("Empty string was passed to the function") | ||
oct_string = "" | ||
while len(bin_string) % 3 != 0: | ||
bin_string = "0" + bin_string | ||
bin_string_in_3_list = [ | ||
bin_string[index : index + 3] | ||
mohammadreza490 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for index, value in enumerate(bin_string) | ||
if index % 3 == 0 | ||
] | ||
for bin_group in bin_string_in_3_list: | ||
oct_val = 0 | ||
for index, val in enumerate(bin_group): | ||
oct_val += int(2 ** (2 - index) * int(val)) | ||
oct_string += str(oct_val) | ||
return oct_string | ||
|
||
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.