-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Octal to Binary Convert #8949
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
tianyizheng02
merged 25 commits into
TheAlgorithms:master
from
BamaCharanChhandogi:OctalToBinary
Aug 20, 2023
Merged
Octal to Binary Convert #8949
Changes from 17 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
c115900
Octal to Binary Convert
8277f5b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6999155
mention return type
BamaCharanChhandogi 7ad4dd0
code scratch
2d73603
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b03c5a3
Merge branch 'TheAlgorithms:master' into OctalToBinary
BamaCharanChhandogi 7b0ac71
mentioned return type
0f3e4bb
Merge branch 'OctalToBinary' of https://github.com/BamaCharanChhandog…
33f7f8e
Merge branch 'TheAlgorithms:master' into OctalToBinary
BamaCharanChhandogi 8be37da
Merge branch 'OctalToBinary' of https://github.com/BamaCharanChhandog…
3040f99
remove comment
fa33d5c
added documention and some test cases
ee728ee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 275e12d
Merge branch 'TheAlgorithms:master' into OctalToBinary
BamaCharanChhandogi 9e75dd7
add another test case
8906c9d
fixes documention
920fcc4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 80d79eb
Merge branch 'TheAlgorithms:master' into OctalToBinary
BamaCharanChhandogi d5eacd9
Documention and test cases added
BamaCharanChhandogi eb4d80e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 37e3391
documention problem solved
BamaCharanChhandogi 25921bd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7e9f2af
error in exit 1
BamaCharanChhandogi 761e1f7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2e91bcf
Apply suggestions from code review
tianyizheng02 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,54 @@ | ||
""" | ||
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) | ||
* Description: Convert a Octal number to Binary. | ||
|
||
References for better understanding: | ||
https://en.wikipedia.org/wiki/Binary_number | ||
https://en.wikipedia.org/wiki/Octal | ||
|
||
""" | ||
|
||
|
||
def octal_to_binary(octal_number: str) -> str: | ||
BamaCharanChhandogi marked this conversation as resolved.
Show resolved
Hide resolved
tianyizheng02 marked this conversation as resolved.
Show resolved
Hide resolved
BamaCharanChhandogi marked this conversation as resolved.
Show resolved
Hide resolved
BamaCharanChhandogi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
ValueError: String to the function | ||
tianyizheng02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> 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 | ||
""" | ||
tianyizheng02 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
binary_number = "" | ||
octal_digits = "01234567" | ||
for digit in octal_number: | ||
if digit not in octal_digits: | ||
raise ValueError("Invalid octal digit") | ||
|
||
binary_digit = "" | ||
value = int(digit) | ||
for _ in range(3): | ||
binary_digit = str(value % 2) + binary_digit | ||
value //= 2 | ||
binary_number += binary_digit | ||
|
||
return binary_number | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.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.
Please remove this empty line