Skip to content

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
merged 25 commits into from
Aug 20, 2023
Merged
Changes from 15 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c115900
Octal to Binary Convert
Aug 11, 2023
8277f5b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2023
6999155
mention return type
BamaCharanChhandogi Aug 11, 2023
7ad4dd0
code scratch
Aug 13, 2023
2d73603
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 13, 2023
b03c5a3
Merge branch 'TheAlgorithms:master' into OctalToBinary
BamaCharanChhandogi Aug 13, 2023
7b0ac71
mentioned return type
Aug 13, 2023
0f3e4bb
Merge branch 'OctalToBinary' of https://github.com/BamaCharanChhandog…
Aug 17, 2023
33f7f8e
Merge branch 'TheAlgorithms:master' into OctalToBinary
BamaCharanChhandogi Aug 17, 2023
8be37da
Merge branch 'OctalToBinary' of https://github.com/BamaCharanChhandog…
Aug 17, 2023
3040f99
remove comment
Aug 17, 2023
fa33d5c
added documention and some test cases
Aug 18, 2023
ee728ee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 18, 2023
275e12d
Merge branch 'TheAlgorithms:master' into OctalToBinary
BamaCharanChhandogi Aug 18, 2023
9e75dd7
add another test case
Aug 18, 2023
8906c9d
fixes documention
Aug 18, 2023
920fcc4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 18, 2023
80d79eb
Merge branch 'TheAlgorithms:master' into OctalToBinary
BamaCharanChhandogi Aug 19, 2023
d5eacd9
Documention and test cases added
BamaCharanChhandogi Aug 19, 2023
eb4d80e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 19, 2023
37e3391
documention problem solved
BamaCharanChhandogi Aug 19, 2023
25921bd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 19, 2023
7e9f2af
error in exit 1
BamaCharanChhandogi Aug 20, 2023
761e1f7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 20, 2023
2e91bcf
Apply suggestions from code review
tianyizheng02 Aug 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions conversions/octal_to_binary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
* Description: Convert a Octal number to Binary.

reference for better understand

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

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

"""


def octal_to_binary(octal_number: str) -> str:
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

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()