From 7cf647f499f2392c949c137df39424a878abce08 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Tue, 15 Sep 2020 13:19:37 +0100 Subject: [PATCH 1/7] bin_to_octal Converts binary values to the octal equivalent. --- conversions/bin_to_octal | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 conversions/bin_to_octal diff --git a/conversions/bin_to_octal b/conversions/bin_to_octal new file mode 100644 index 000000000000..be09073852ad --- /dev/null +++ b/conversions/bin_to_octal @@ -0,0 +1,43 @@ +""" +The function below will convert any binary string to the octal equivalent. + +>>> binToOctal("1111") +'17' + +>>> binToOctal("101010101010011") +'52523' + +>>> binToOctal("") +ValueError("Empty string was passed to the function") + +>>> binToOctal("a-1") +ValueError("Non-binary value was passed to the function") + +""" + + +def binToOctal(binString: str) -> str: + if not all("0" <= char <= "1" for char in binString): + raise ValueError("Non-binary value was passed to the function") + if not binString: + raise ValueError("Empty string was passed to the function") + octString = "" + while len(binString) % 3 != 0: + binString = "0" + binString + binStringIn3List = [ + binString[index : index + 3] + for index, value in enumerate(binString) + if index % 3 == 0 + ] + for binGroup in binStringIn3List: + octVal = 0 + for index, val in enumerate(binGroup): + octVal += int(2 ** (2 - index) * int(val)) + octString += str(octVal) + return octString + + +if __name__ == "__main__": + from doctest import testmod + + testmod() From 66830adda844e8ed12dfbe068211450cc948d9cf Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Tue, 15 Sep 2020 13:22:52 +0100 Subject: [PATCH 2/7] Update bin_to_octal --- conversions/bin_to_octal | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/conversions/bin_to_octal b/conversions/bin_to_octal index be09073852ad..93e6a2390cb0 100644 --- a/conversions/bin_to_octal +++ b/conversions/bin_to_octal @@ -1,40 +1,40 @@ """ The function below will convert any binary string to the octal equivalent. ->>> binToOctal("1111") +>>> bin_to_octal("1111") '17' ->>> binToOctal("101010101010011") +>>> bin_to_octal("101010101010011") '52523' ->>> binToOctal("") +>>> bin_to_octal("") ValueError("Empty string was passed to the function") ->>> binToOctal("a-1") +>>> bin_to_octal("a-1") ValueError("Non-binary value was passed to the function") """ -def binToOctal(binString: str) -> str: - if not all("0" <= char <= "1" for char in binString): +def bin_to_octal(bin_string: str) -> str: + if not all("0" <= char <= "1" for char in bin_string): raise ValueError("Non-binary value was passed to the function") - if not binString: + if not bin_string: raise ValueError("Empty string was passed to the function") - octString = "" - while len(binString) % 3 != 0: - binString = "0" + binString - binStringIn3List = [ - binString[index : index + 3] - for index, value in enumerate(binString) + 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 binGroup in binStringIn3List: - octVal = 0 - for index, val in enumerate(binGroup): - octVal += int(2 ** (2 - index) * int(val)) - octString += str(octVal) - return octString + 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 cf48d729d5c6ec01c419b512b7e2c1bdffb01d47 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Tue, 15 Sep 2020 22:52:11 +0100 Subject: [PATCH 3/7] Update conversions/bin_to_octal Co-authored-by: Christian Clauss --- conversions/bin_to_octal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/bin_to_octal b/conversions/bin_to_octal index 93e6a2390cb0..47daebe53519 100644 --- a/conversions/bin_to_octal +++ b/conversions/bin_to_octal @@ -17,7 +17,7 @@ ValueError("Non-binary value was passed to the function") def bin_to_octal(bin_string: str) -> str: - if not all("0" <= char <= "1" for char in bin_string): + 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") From 7e810261653400e826ec4f906d48f9b7f7375c5f Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Wed, 16 Sep 2020 16:46:02 +0100 Subject: [PATCH 4/7] Update conversions/bin_to_octal Co-authored-by: Du Yuanchao --- conversions/bin_to_octal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/bin_to_octal b/conversions/bin_to_octal index 47daebe53519..06943204e18d 100644 --- a/conversions/bin_to_octal +++ b/conversions/bin_to_octal @@ -25,7 +25,7 @@ def bin_to_octal(bin_string: str) -> str: while len(bin_string) % 3 != 0: bin_string = "0" + bin_string bin_string_in_3_list = [ - bin_string[index : index + 3] + bin_string[index: index + 3] for index, value in enumerate(bin_string) if index % 3 == 0 ] From 0e42dbbec49e2c0a40ebe7b0471b5fda900b9909 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Wed, 16 Sep 2020 16:47:26 +0100 Subject: [PATCH 5/7] Update conversions/bin_to_octal Co-authored-by: Du Yuanchao --- conversions/bin_to_octal | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/conversions/bin_to_octal b/conversions/bin_to_octal index 06943204e18d..663993203fda 100644 --- a/conversions/bin_to_octal +++ b/conversions/bin_to_octal @@ -11,8 +11,9 @@ The function below will convert any binary string to the octal equivalent. ValueError("Empty string was passed to the function") >>> bin_to_octal("a-1") -ValueError("Non-binary value was passed to the function") - +Traceback (most recent call last): +... +ValueError: Non-binary value was passed to the function """ From 39c00ea5a1c42da63c680e6f8667397edbe2a46a Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Wed, 16 Sep 2020 16:48:13 +0100 Subject: [PATCH 6/7] Update conversions/bin_to_octal Co-authored-by: Du Yuanchao --- conversions/bin_to_octal | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/conversions/bin_to_octal b/conversions/bin_to_octal index 663993203fda..39aa4646e7a5 100644 --- a/conversions/bin_to_octal +++ b/conversions/bin_to_octal @@ -8,8 +8,9 @@ The function below will convert any binary string to the octal equivalent. '52523' >>> bin_to_octal("") -ValueError("Empty string was passed to the function") - +Traceback (most recent call last): +... +ValueError: Empty string was passed to the function >>> bin_to_octal("a-1") Traceback (most recent call last): ... From 3f60a8c337c216866ed68862a09c4f719426afc8 Mon Sep 17 00:00:00 2001 From: mohammadreza490 <47437328+mohammadreza490@users.noreply.github.com> Date: Wed, 16 Sep 2020 16:50:12 +0100 Subject: [PATCH 7/7] Rename bin_to_octal to bin_to_octal.py --- conversions/{bin_to_octal => bin_to_octal.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename conversions/{bin_to_octal => bin_to_octal.py} (100%) diff --git a/conversions/bin_to_octal b/conversions/bin_to_octal.py similarity index 100% rename from conversions/bin_to_octal rename to conversions/bin_to_octal.py