2023-08-18 11:45:30 +05:30
|
|
|
"""
|
|
|
|
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
|
|
|
|
* Description: Convert a Octal number to Binary.
|
|
|
|
|
|
|
|
reference for better understand
|
|
|
|
|
|
|
|
URL: https://en.wikipedia.org/wiki/Binary_number
|
|
|
|
URL: https://en.wikipedia.org/wiki/Octal
|
|
|
|
"""
|
2023-08-13 15:12:42 +05:30
|
|
|
def octal_to_binary(octal_number: str) -> str:
|
2023-08-13 14:20:02 +05:30
|
|
|
binary_number = ""
|
|
|
|
octal_digits = "01234567"
|
2023-08-11 17:11:58 +05:30
|
|
|
|
2023-08-18 11:45:30 +05:30
|
|
|
"""
|
|
|
|
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: octal value was passed to the function
|
|
|
|
>>> oct_to_decimal("17")
|
|
|
|
001111
|
|
|
|
>>> oct_to_decimal("7")
|
|
|
|
111
|
|
|
|
"""
|
2023-08-13 14:20:02 +05:30
|
|
|
for digit in octal_number:
|
|
|
|
if digit not in octal_digits:
|
|
|
|
raise ValueError("Invalid octal digit")
|
2023-08-13 08:50:58 +00:00
|
|
|
|
2023-08-13 14:20:02 +05:30
|
|
|
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
|
2023-08-11 11:44:02 +00:00
|
|
|
|
2023-08-13 08:50:58 +00:00
|
|
|
|
2023-08-11 17:11:58 +05:30
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
2023-08-13 08:50:58 +00:00
|
|
|
|
|
|
|
doctest.testmod()
|