-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
add an octal_to_binary converter #8940
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,49 @@ | ||||
""" | ||||
Convert a octal value to its binary equivalent | ||||
>>> octal_to_binary("17") | ||||
'1111' | ||||
>>> octal_to_binary("52523") | ||||
'101010101010011' | ||||
>>> octal_to_binary("532.51") | ||||
'101011010.101001' | ||||
""" | ||||
|
||||
|
||||
from operator import index | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This import is never used, and it's causing pre-commit to fail. |
||||
|
||||
|
||||
def octal_to_binary(octal_string: str) -> str: | ||||
if not all(char in "01234567." for char in octal_string): | ||||
raise ValueError("Non-octal value was passed to the function") | ||||
if not octal_string: | ||||
raise ValueError("Empty string was passed to the function") | ||||
binary_string = "" | ||||
dict_of_octal_to_binary = { | ||||
"0": "000", | ||||
"1": "001", | ||||
"2": "010", | ||||
"3": "011", | ||||
"4": "100", | ||||
"5": "101", | ||||
"6": "110", | ||||
"7": "111", | ||||
} | ||||
indexl = 0 | ||||
for i in octal_string: | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we do this with a comprehension instead of manually appending to a string? |
||||
if i == ".": | ||||
binary_string += "." | ||||
continue | ||||
if binary_string == "": | ||||
for char in dict_of_octal_to_binary[i]: | ||||
if char != "0" or indexl >= 1: | ||||
binary_string += char | ||||
indexl += 1 | ||||
Comment on lines
+36
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of having a special case for handling 0's at the start of the binary string, why not just append to the binary string as normal and handle the starting 0's at the very end? |
||||
else: | ||||
binary_string += dict_of_octal_to_binary[i] | ||||
return binary_string | ||||
|
||||
|
||||
if __name__ == "__main__": | ||||
from doctest import testmod | ||||
|
||||
testmod() |
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.
These doctests should go inside a docstring within the function, not at the top of the file.