From 9b1dc8d5f1f3113deae76a8910bf7b8c1ae2ada5 Mon Sep 17 00:00:00 2001 From: cybov Date: Mon, 12 Oct 2020 22:31:59 +0100 Subject: [PATCH 1/2] Added comments with some code midifications --- conversions/octal_to_decimal | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/conversions/octal_to_decimal b/conversions/octal_to_decimal index a5b027e3ae8d..d281bd5b42b0 100644 --- a/conversions/octal_to_decimal +++ b/conversions/octal_to_decimal @@ -9,26 +9,40 @@ def oct_to_decimal(oct_string: str) -> int: >>> oct_to_decimal("-45") -37 >>> oct_to_decimal("2-0Fm") + Traceback (most recent call last): + ... ValueError: Non-octal value was passed to the function >>> oct_to_decimal("") + Traceback (most recent call last): + ... ValueError: Empty string value was passed to the function >>> oct_to_decimal("19") + Traceback (most recent call last): + ... ValueError: Non-octal value was passed to the function """ + # Strip oct_string of whitespaces oct_string = str(oct_string).strip() if not oct_string: - raise ValueError("Empty string was passed to the function") + raise ValueError("Empty string value was passed to the function") + + # Check if oct_string is a negative value is_negative = oct_string[0] == "-" if is_negative: + # Remove (-) from oct_string oct_string = oct_string[1:] - if not all(0 <= int(char) <= 7 for char in oct_string): + + # check if oct_string is an octal value and convert + if oct_string.isdecimal() and all(0 <= int(char) <= 7 for char in oct_string): + decimal_number = 0 + for char in oct_string: + decimal_number = 8 * decimal_number + int(char) + if is_negative: + decimal_number = -decimal_number + return decimal_number + else: + # else raise exception 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 - return decimal_number if __name__ == "__main__": From f01fc05c134bf78ebb344eaf45dd001a52e36958 Mon Sep 17 00:00:00 2001 From: cybov Date: Mon, 12 Oct 2020 22:41:20 +0100 Subject: [PATCH 2/2] Renamed octal_to_decimal to include (.py) --- conversions/{octal_to_decimal => octal_to_decimal.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename conversions/{octal_to_decimal => octal_to_decimal.py} (100%) diff --git a/conversions/octal_to_decimal b/conversions/octal_to_decimal.py similarity index 100% rename from conversions/octal_to_decimal rename to conversions/octal_to_decimal.py