forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoctal_to_binary.py
54 lines (44 loc) · 1.4 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
50
51
52
53
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
"""
defoctal_to_binary(octal_number: str) ->str:
"""
Convert an Octal number to Binary.
>>> octal_to_binary("17")
'001111'
>>> octal_to_binary("7")
'111'
>>> octal_to_binary("Av")
Traceback (most recent call last):
...
ValueError: Non-octal value was passed to the function
>>> octal_to_binary("@#")
Traceback (most recent call last):
...
ValueError: Non-octal value was passed to the function
>>> octal_to_binary("")
Traceback (most recent call last):
...
ValueError: Empty string was passed to the function
"""
ifnotoctal_number:
raiseValueError("Empty string was passed to the function")
binary_number=""
octal_digits="01234567"
fordigitinoctal_number:
ifdigitnotinoctal_digits:
raiseValueError("Non-octal value was passed to the function")
binary_digit=""
value=int(digit)
for_inrange(3):
binary_digit=str(value%2) +binary_digit
value//=2
binary_number+=binary_digit
returnbinary_number
if__name__=="__main__":
importdoctest
doctest.testmod()