From 9da661b098b89caf993149e3db0ee0518f5dd338 Mon Sep 17 00:00:00 2001 From: Navanit Nandakumar Date: Sat, 12 Aug 2023 07:27:50 +0530 Subject: [PATCH 01/12] Create octal_to_binary.py Added ocatl_to_binary.py --- conversions/octal_to_binary.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 conversions/octal_to_binary.py diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py new file mode 100644 index 000000000000..c75a3589f284 --- /dev/null +++ b/conversions/octal_to_binary.py @@ -0,0 +1,22 @@ +def octal_to_binary(octal): + # Converting Octal to Decimal + decimal = 0 + power = 0 + while octal != 0: + decimal += (octal % 10) * pow(8, power) + octal //= 10 + power += 1 + + # Converting Decimal to Binary + binary = 0 + digit_place = 1 + while decimal != 0: + binary += (decimal % 2) * digit_place + decimal //= 2 + digit_place *= 10 + + return binary + +octal_number = int(input("Enter octal number: ")) +binary_number = octal_to_binary(octal_number) +print(f"The binary equivalent of {octal_number} is {binary_number}") From 56efdb87a0c52b1d6fd9716d7770f32dbbe8d79e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Aug 2023 02:04:44 +0000 Subject: [PATCH 02/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/octal_to_binary.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index c75a3589f284..9f868c46e405 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -6,7 +6,7 @@ def octal_to_binary(octal): decimal += (octal % 10) * pow(8, power) octal //= 10 power += 1 - + # Converting Decimal to Binary binary = 0 digit_place = 1 @@ -14,9 +14,10 @@ def octal_to_binary(octal): binary += (decimal % 2) * digit_place decimal //= 2 digit_place *= 10 - + return binary - + + octal_number = int(input("Enter octal number: ")) binary_number = octal_to_binary(octal_number) print(f"The binary equivalent of {octal_number} is {binary_number}") From 79eb0337264da8b6d78cf2ceb91600156d87ae73 Mon Sep 17 00:00:00 2001 From: Navanit Nandakumar Date: Sat, 12 Aug 2023 07:38:03 +0530 Subject: [PATCH 03/12] Updated octal_to_binary.py Fixed whitespaces in blank lines. --- conversions/octal_to_binary.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 9f868c46e405..d2deee8a883d 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -6,7 +6,6 @@ def octal_to_binary(octal): decimal += (octal % 10) * pow(8, power) octal //= 10 power += 1 - # Converting Decimal to Binary binary = 0 digit_place = 1 @@ -14,10 +13,7 @@ def octal_to_binary(octal): binary += (decimal % 2) * digit_place decimal //= 2 digit_place *= 10 - return binary - - octal_number = int(input("Enter octal number: ")) binary_number = octal_to_binary(octal_number) print(f"The binary equivalent of {octal_number} is {binary_number}") From 7e4c0605609e6d8ca034b436f1efe6f58300107f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Aug 2023 02:08:36 +0000 Subject: [PATCH 04/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/octal_to_binary.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index d2deee8a883d..92709f2acfb3 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -14,6 +14,8 @@ def octal_to_binary(octal): decimal //= 2 digit_place *= 10 return binary + + octal_number = int(input("Enter octal number: ")) binary_number = octal_to_binary(octal_number) print(f"The binary equivalent of {octal_number} is {binary_number}") From e57c0f53913cfed64ff28a89b2bfb53ef3a0d2ab Mon Sep 17 00:00:00 2001 From: Navanit Nandakumar Date: Sat, 12 Aug 2023 07:46:23 +0530 Subject: [PATCH 05/12] Update octal_to_binary.py Added type hints for the octal_to_binary function --- conversions/octal_to_binary.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 92709f2acfb3..b6996164764a 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -1,4 +1,4 @@ -def octal_to_binary(octal): +def octal_to_binary(octal)->int: # Converting Octal to Decimal decimal = 0 power = 0 @@ -14,8 +14,6 @@ def octal_to_binary(octal): decimal //= 2 digit_place *= 10 return binary - - octal_number = int(input("Enter octal number: ")) binary_number = octal_to_binary(octal_number) print(f"The binary equivalent of {octal_number} is {binary_number}") From d14d5fd8f539d8b2508731f241998a88654a8942 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Aug 2023 02:17:21 +0000 Subject: [PATCH 06/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/octal_to_binary.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index b6996164764a..68606c6da813 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -1,4 +1,4 @@ -def octal_to_binary(octal)->int: +def octal_to_binary(octal) -> int: # Converting Octal to Decimal decimal = 0 power = 0 @@ -14,6 +14,8 @@ def octal_to_binary(octal)->int: decimal //= 2 digit_place *= 10 return binary + + octal_number = int(input("Enter octal number: ")) binary_number = octal_to_binary(octal_number) print(f"The binary equivalent of {octal_number} is {binary_number}") From f1c6a6559316ad62cfc9af16bee0a042e04ba128 Mon Sep 17 00:00:00 2001 From: Navanit Nandakumar Date: Sat, 12 Aug 2023 08:56:38 +0530 Subject: [PATCH 07/12] Update octal_to_binary.py Added all required type hints Added doctest --- conversions/octal_to_binary.py | 111 +++++++++++++++++++++++++++------ 1 file changed, 93 insertions(+), 18 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 68606c6da813..0e9f037ac3a3 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -1,21 +1,96 @@ -def octal_to_binary(octal) -> int: - # Converting Octal to Decimal - decimal = 0 - power = 0 - while octal != 0: - decimal += (octal % 10) * pow(8, power) - octal //= 10 - power += 1 +def octal_to_binary(octal:str) -> str: + """ + Convert an octal value to its binary equivalent + + >>> octal_to_binary("") + Traceback (most recent call last): + ... + ValueError: Empty string 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("e") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary(8) + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("-e") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("-8") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("1") + '0b1' + >>> octal_to_binary("-1") + '-0b1' + >>> octal_to_binary("12") + '0b1010' + >>> octal_to_binary(" 12 ") + '0b1010' + >>> octal_to_binary("-45") + '-0b100101' + >>> octal_to_binary("-") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("0") + '0b0' + >>> octal_to_binary("-4055") + '-0b100000101101' + >>> octal_to_binary("2-0Fm") + 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 + >>> octal_to_binary("19") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + """ + + oct_string = str(octal).strip() + if not oct_string: + raise ValueError("Empty string was passed to the function") + is_negative = oct_string[0] == "-" + if is_negative: + oct_string = oct_string[1:] + if not oct_string.isdigit() or not all(0 <= int(char) <= 7 for char in oct_string): + raise ValueError("Non-octal value was passed to the function") + decimal_number = 0 + for char in oct_string: + decimal_number = 8 * decimal_number + int(char) + if is_negative: + decimal_number = -decimal_number + # Converting Decimal to Binary - binary = 0 - digit_place = 1 - while decimal != 0: - binary += (decimal % 2) * digit_place - decimal //= 2 - digit_place *= 10 - return binary + if decimal_number == 0: + return "0b0" + + negative = False + if decimal_number < 0: + negative = True + decimal_number = -decimal_number + + binary: list[int] = [] + while decimal_number > 0: + binary.insert(0, decimal_number % 2) + decimal_number >>= 1 + + if negative: + return "-0b" + "".join(str(e) for e in binary) + return "0b" + "".join(str(e) for e in binary) -octal_number = int(input("Enter octal number: ")) -binary_number = octal_to_binary(octal_number) -print(f"The binary equivalent of {octal_number} is {binary_number}") +if __name__ == "__main__": + from doctest import testmod + testmod() From c9e7bf6e45b6a164e7204ae1b5841b60c2c8bd61 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Aug 2023 03:28:12 +0000 Subject: [PATCH 08/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/octal_to_binary.py | 120 +++++++++++++++++---------------- 1 file changed, 61 insertions(+), 59 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 0e9f037ac3a3..64f205d371aa 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -1,63 +1,63 @@ -def octal_to_binary(octal:str) -> str: +def octal_to_binary(octal: str) -> str: + """ + Convert an octal value to its binary equivalent + + >>> octal_to_binary("") + Traceback (most recent call last): + ... + ValueError: Empty string 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("e") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary(8) + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("-e") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("-8") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("1") + '0b1' + >>> octal_to_binary("-1") + '-0b1' + >>> octal_to_binary("12") + '0b1010' + >>> octal_to_binary(" 12 ") + '0b1010' + >>> octal_to_binary("-45") + '-0b100101' + >>> octal_to_binary("-") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("0") + '0b0' + >>> octal_to_binary("-4055") + '-0b100000101101' + >>> octal_to_binary("2-0Fm") + 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 + >>> octal_to_binary("19") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function """ - Convert an octal value to its binary equivalent - >>> octal_to_binary("") - Traceback (most recent call last): - ... - ValueError: Empty string 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("e") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary(8) - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary("-e") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary("-8") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary("1") - '0b1' - >>> octal_to_binary("-1") - '-0b1' - >>> octal_to_binary("12") - '0b1010' - >>> octal_to_binary(" 12 ") - '0b1010' - >>> octal_to_binary("-45") - '-0b100101' - >>> octal_to_binary("-") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary("0") - '0b0' - >>> octal_to_binary("-4055") - '-0b100000101101' - >>> octal_to_binary("2-0Fm") - 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 - >>> octal_to_binary("19") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - """ - oct_string = str(octal).strip() if not oct_string: raise ValueError("Empty string was passed to the function") @@ -75,7 +75,7 @@ def octal_to_binary(octal:str) -> str: # Converting Decimal to Binary if decimal_number == 0: return "0b0" - + negative = False if decimal_number < 0: negative = True @@ -91,6 +91,8 @@ def octal_to_binary(octal:str) -> str: return "0b" + "".join(str(e) for e in binary) + if __name__ == "__main__": from doctest import testmod + testmod() From b944eec224a31934d055ae47f79bb0c7bd823358 Mon Sep 17 00:00:00 2001 From: Navanit Nandakumar Date: Sat, 12 Aug 2023 09:08:14 +0530 Subject: [PATCH 09/12] Update octal_to_binary.py Fixed white spaces --- conversions/octal_to_binary.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 64f205d371aa..02dc911a7f7f 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -1,7 +1,6 @@ def octal_to_binary(octal: str) -> str: """ Convert an octal value to its binary equivalent - >>> octal_to_binary("") Traceback (most recent call last): ... @@ -57,7 +56,6 @@ def octal_to_binary(octal: str) -> str: ... ValueError: Non-octal value was passed to the function """ - oct_string = str(octal).strip() if not oct_string: raise ValueError("Empty string was passed to the function") @@ -71,28 +69,20 @@ def octal_to_binary(octal: str) -> str: decimal_number = 8 * decimal_number + int(char) if is_negative: decimal_number = -decimal_number - # Converting Decimal to Binary if decimal_number == 0: return "0b0" - negative = False if decimal_number < 0: negative = True decimal_number = -decimal_number - binary: list[int] = [] while decimal_number > 0: binary.insert(0, decimal_number % 2) decimal_number >>= 1 - if negative: return "-0b" + "".join(str(e) for e in binary) - return "0b" + "".join(str(e) for e in binary) - - if __name__ == "__main__": from doctest import testmod - testmod() From 6e742ab167c489df801c34a006bd0db276597881 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Aug 2023 03:39:32 +0000 Subject: [PATCH 10/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/octal_to_binary.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 02dc911a7f7f..06856aa788ce 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -83,6 +83,9 @@ def octal_to_binary(octal: str) -> str: if negative: return "-0b" + "".join(str(e) for e in binary) return "0b" + "".join(str(e) for e in binary) + + if __name__ == "__main__": from doctest import testmod + testmod() From b8b3b8c5937932bb380806f994e0ea24e7b520b4 Mon Sep 17 00:00:00 2001 From: Navanit Nandakumar Date: Sun, 13 Aug 2023 11:22:31 +0530 Subject: [PATCH 11/12] Update octal_to_binary.py Made the code more efficient --- conversions/octal_to_binary.py | 143 +++++++++++++++------------------ 1 file changed, 63 insertions(+), 80 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 06856aa788ce..3953ed795cb3 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -1,91 +1,74 @@ -def octal_to_binary(octal: str) -> str: - """ - Convert an octal value to its binary equivalent - >>> octal_to_binary("") - Traceback (most recent call last): - ... - ValueError: Empty string 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("e") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary(8) - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary("-e") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary("-8") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary("1") - '0b1' - >>> octal_to_binary("-1") - '-0b1' - >>> octal_to_binary("12") - '0b1010' - >>> octal_to_binary(" 12 ") - '0b1010' - >>> octal_to_binary("-45") - '-0b100101' - >>> octal_to_binary("-") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary("0") - '0b0' - >>> octal_to_binary("-4055") - '-0b100000101101' - >>> octal_to_binary("2-0Fm") - 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 - >>> octal_to_binary("19") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function +def octal_to_binary(octal:str) -> str: """ + Convert an octal value to its binary equivalent + >>> octal_to_binary("") + Traceback (most recent call last): + ... + ValueError: Empty string 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("e") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary(8) + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("-e") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("-8") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("1") + '0b1' + >>> octal_to_binary("-1") + '-0b1' + >>> octal_to_binary("12") + '0b1010' + >>> octal_to_binary(" 12 ") + '0b1010' + >>> octal_to_binary("-45") + '-0b100101' + >>> octal_to_binary("-") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("0") + '0b0' + >>> octal_to_binary("-4055") + '-0b100000101101' + >>> octal_to_binary("2-0Fm") + 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 + >>> octal_to_binary("19") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + """ oct_string = str(octal).strip() if not oct_string: raise ValueError("Empty string was passed to the function") - is_negative = oct_string[0] == "-" + is_negative = oct_string.startswith('-') if is_negative: oct_string = oct_string[1:] + binary_num = '-0b' + else: + binary_num = '0b' if not oct_string.isdigit() or not all(0 <= int(char) <= 7 for char in oct_string): raise ValueError("Non-octal value was passed to the function") - decimal_number = 0 - for char in oct_string: - decimal_number = 8 * decimal_number + int(char) - if is_negative: - decimal_number = -decimal_number - # Converting Decimal to Binary - if decimal_number == 0: - return "0b0" - negative = False - if decimal_number < 0: - negative = True - decimal_number = -decimal_number - binary: list[int] = [] - while decimal_number > 0: - binary.insert(0, decimal_number % 2) - decimal_number >>= 1 - if negative: - return "-0b" + "".join(str(e) for e in binary) - return "0b" + "".join(str(e) for e in binary) - - + binary_num += str(bin(int(oct_string, 8)))[2:] + return binary_num if __name__ == "__main__": from doctest import testmod - testmod() From eedc5935ad008cd38310fac47977c6985393a7b6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 13 Aug 2023 05:53:55 +0000 Subject: [PATCH 12/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/octal_to_binary.py | 123 +++++++++++++++++---------------- 1 file changed, 63 insertions(+), 60 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 3953ed795cb3..40bf612075a0 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -1,74 +1,77 @@ -def octal_to_binary(octal:str) -> str: +def octal_to_binary(octal: str) -> str: + """ + Convert an octal value to its binary equivalent + >>> octal_to_binary("") + Traceback (most recent call last): + ... + ValueError: Empty string 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("e") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary(8) + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("-e") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("-8") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("1") + '0b1' + >>> octal_to_binary("-1") + '-0b1' + >>> octal_to_binary("12") + '0b1010' + >>> octal_to_binary(" 12 ") + '0b1010' + >>> octal_to_binary("-45") + '-0b100101' + >>> octal_to_binary("-") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("0") + '0b0' + >>> octal_to_binary("-4055") + '-0b100000101101' + >>> octal_to_binary("2-0Fm") + 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 + >>> octal_to_binary("19") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function """ - Convert an octal value to its binary equivalent - >>> octal_to_binary("") - Traceback (most recent call last): - ... - ValueError: Empty string 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("e") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary(8) - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary("-e") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary("-8") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary("1") - '0b1' - >>> octal_to_binary("-1") - '-0b1' - >>> octal_to_binary("12") - '0b1010' - >>> octal_to_binary(" 12 ") - '0b1010' - >>> octal_to_binary("-45") - '-0b100101' - >>> octal_to_binary("-") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - >>> octal_to_binary("0") - '0b0' - >>> octal_to_binary("-4055") - '-0b100000101101' - >>> octal_to_binary("2-0Fm") - 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 - >>> octal_to_binary("19") - Traceback (most recent call last): - ... - ValueError: Non-octal value was passed to the function - """ oct_string = str(octal).strip() if not oct_string: raise ValueError("Empty string was passed to the function") - is_negative = oct_string.startswith('-') + is_negative = oct_string.startswith("-") if is_negative: oct_string = oct_string[1:] - binary_num = '-0b' + binary_num = "-0b" else: - binary_num = '0b' + binary_num = "0b" if not oct_string.isdigit() or not all(0 <= int(char) <= 7 for char in oct_string): raise ValueError("Non-octal value was passed to the function") binary_num += str(bin(int(oct_string, 8)))[2:] return binary_num + + if __name__ == "__main__": from doctest import testmod + testmod()