From 8006dc41a075ffd142747865122037105c70319f Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Sun, 6 Sep 2020 01:44:03 +0100 Subject: [PATCH 1/3] Create octal_to_decimal octal to decimal converter --- conversions/octal_to_decimal | 37 ++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 conversions/octal_to_decimal diff --git a/conversions/octal_to_decimal b/conversions/octal_to_decimal new file mode 100644 index 000000000000..42b610ba21fb --- /dev/null +++ b/conversions/octal_to_decimal @@ -0,0 +1,37 @@ +def oct_to_decimal(oct_string: str) -> int: + """ + Convert a octal value to its decimal equivalent + + >>> oct_to_decimal("12") + 10 + >>> oct_to_decimal(" 12 ") + 10 + >>> oct_to_decimal("-45") + -37 + >>> oct_to_decimal("2-0Fm") + ValueError: Non-octal value was passed to the function + >>> oct_to_decimal("") + ValueError: Empty string value was passed to the function + >>> oct_to_decimal("19") + ValueError: Non-octal value was passed to the function + """ + oct_string = oct_string.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 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 + return decimal_number + + +if __name__ == "__main__": + from doctest import testmod + + testmod() From abc56504f5844e27ae7a158135fb071e3f455def Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Sun, 6 Sep 2020 01:45:40 +0100 Subject: [PATCH 2/3] Update octal_to_decimal --- conversions/octal_to_decimal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/octal_to_decimal b/conversions/octal_to_decimal index 42b610ba21fb..76ebd3109608 100644 --- a/conversions/octal_to_decimal +++ b/conversions/octal_to_decimal @@ -21,7 +21,7 @@ def oct_to_decimal(oct_string: str) -> int: is_negative = oct_string[0] == "-" if is_negative: oct_string = oct_string[1:] - if not all(0 < int(char) <= 7 for char in oct_string): + if 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: From bf0327e679713a7c75733b5c22892d57db5e7b77 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Sun, 6 Sep 2020 10:57:52 +0100 Subject: [PATCH 3/3] Update conversions/octal_to_decimal Co-authored-by: Christian Clauss --- conversions/octal_to_decimal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/octal_to_decimal b/conversions/octal_to_decimal index 76ebd3109608..a5b027e3ae8d 100644 --- a/conversions/octal_to_decimal +++ b/conversions/octal_to_decimal @@ -15,7 +15,7 @@ def oct_to_decimal(oct_string: str) -> int: >>> oct_to_decimal("19") ValueError: Non-octal value was passed to the function """ - oct_string = oct_string.strip() + oct_string = str(oct_string).strip() if not oct_string: raise ValueError("Empty string was passed to the function") is_negative = oct_string[0] == "-"