From b22596cd965ef53b0faa4e2d32044a21c1458aab Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Sat, 19 Sep 2020 06:36:56 +0100 Subject: [PATCH] bin_to_octal (#2431) * bin_to_octal Converts binary values to the octal equivalent. * Update bin_to_octal * Update conversions/bin_to_octal Co-authored-by: Christian Clauss * Update conversions/bin_to_octal Co-authored-by: Du Yuanchao * Update conversions/bin_to_octal Co-authored-by: Du Yuanchao * Update conversions/bin_to_octal Co-authored-by: Du Yuanchao * Rename bin_to_octal to bin_to_octal.py Co-authored-by: Christian Clauss Co-authored-by: Du Yuanchao --- conversions/bin_to_octal.py | 45 +++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 conversions/bin_to_octal.py diff --git a/conversions/bin_to_octal.py b/conversions/bin_to_octal.py new file mode 100644 index 000000000..39aa4646e --- /dev/null +++ b/conversions/bin_to_octal.py @@ -0,0 +1,45 @@ +""" +The function below will convert any binary string to the octal equivalent. + +>>> bin_to_octal("1111") +'17' + +>>> bin_to_octal("101010101010011") +'52523' + +>>> bin_to_octal("") +Traceback (most recent call last): +... +ValueError: Empty string was passed to the function +>>> bin_to_octal("a-1") +Traceback (most recent call last): +... +ValueError: Non-binary value was passed to the function +""" + + +def bin_to_octal(bin_string: str) -> str: + if not all(char in "01" for char in bin_string): + raise ValueError("Non-binary value was passed to the function") + if not bin_string: + raise ValueError("Empty string was passed to the function") + oct_string = "" + while len(bin_string) % 3 != 0: + bin_string = "0" + bin_string + bin_string_in_3_list = [ + bin_string[index: index + 3] + for index, value in enumerate(bin_string) + if index % 3 == 0 + ] + for bin_group in bin_string_in_3_list: + oct_val = 0 + for index, val in enumerate(bin_group): + oct_val += int(2 ** (2 - index) * int(val)) + oct_string += str(oct_val) + return oct_string + + +if __name__ == "__main__": + from doctest import testmod + + testmod()