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] [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 3953ed795..40bf61207 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()