-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathoctal_to_binary.py
49 lines (42 loc) · 1.17 KB
/
octal_to_binary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
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:
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
else:
binary_string += dict_of_octal_to_binary[i]
return binary_string
if __name__ == "__main__":
from doctest import testmod
testmod()