From c1159007e2d9e6b1597d4358edd390e5ed6940f6 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Fri, 11 Aug 2023 17:11:58 +0530 Subject: [PATCH 01/19] Octal to Binary Convert --- conversions/octal_to_binary.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 conversions/octal_to_binary.py diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py new file mode 100644 index 000000000000..e57860b1b48f --- /dev/null +++ b/conversions/octal_to_binary.py @@ -0,0 +1,28 @@ +def octal_to_binary(octal_number): + """ + /** + * Converts any Octal Number to a Binary Number + * + * @author Bama Charan Chhandogi + */ + Convert an octal number to binary. + + Args: + octal_number (str): The octal number as a string. + + Returns: + str: The binary representation of the octal number. + + Examples: + >>> octal_to_binary("34") + '0b11100' + >>> octal_to_binary("777") + '0b111111111' + """ + decimal_number = int(octal_number, 8) + binary_number = bin(decimal_number) + return binary_number + +if __name__ == "__main__": + import doctest + doctest.testmod() From 8277f5b23fd53b128071f2e42010c0b0f63d30cf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 11 Aug 2023 11:44:02 +0000 Subject: [PATCH 02/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/octal_to_binary.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index e57860b1b48f..7b1b1aca9629 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -23,6 +23,8 @@ def octal_to_binary(octal_number): binary_number = bin(decimal_number) return binary_number + if __name__ == "__main__": import doctest + doctest.testmod() From 69991555fc83083250623c178e5ee8edbe0577d6 Mon Sep 17 00:00:00 2001 From: Bama Charan Chhandogi Date: Fri, 11 Aug 2023 17:31:43 +0530 Subject: [PATCH 03/19] mention return type --- conversions/octal_to_binary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 7b1b1aca9629..c24fed4e2225 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -1,4 +1,4 @@ -def octal_to_binary(octal_number): +def octal_to_binary(octal_number: str) -> str: """ /** * Converts any Octal Number to a Binary Number From 7ad4dd07a2ac020972691e3e7d8c607e81eda1b8 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sun, 13 Aug 2023 14:20:02 +0530 Subject: [PATCH 04/19] code scratch --- conversions/octal_to_binary.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index c24fed4e2225..48075b8df8f7 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -1,10 +1,5 @@ -def octal_to_binary(octal_number: str) -> str: +def octal_to_binary(octal_number): """ - /** - * Converts any Octal Number to a Binary Number - * - * @author Bama Charan Chhandogi - */ Convert an octal number to binary. Args: @@ -12,19 +7,23 @@ def octal_to_binary(octal_number: str) -> str: Returns: str: The binary representation of the octal number. - - Examples: - >>> octal_to_binary("34") - '0b11100' - >>> octal_to_binary("777") - '0b111111111' """ - decimal_number = int(octal_number, 8) - binary_number = bin(decimal_number) - return binary_number + binary_number = "" + octal_digits = "01234567" + for digit in octal_number: + if digit not in octal_digits: + raise ValueError("Invalid octal digit") + + binary_digit = "" + value = int(digit) + for _ in range(3): + binary_digit = str(value % 2) + binary_digit + value //= 2 + binary_number += binary_digit + + return binary_number if __name__ == "__main__": import doctest - - doctest.testmod() + doctest.testmod() \ No newline at end of file From 2d73603083e6913f8b820c08aec393571b697c58 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 08:50:58 +0000 Subject: [PATCH 05/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/octal_to_binary.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 48075b8df8f7..385704175481 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -14,7 +14,7 @@ def octal_to_binary(octal_number): for digit in octal_number: if digit not in octal_digits: raise ValueError("Invalid octal digit") - + binary_digit = "" value = int(digit) for _ in range(3): @@ -24,6 +24,8 @@ def octal_to_binary(octal_number): return binary_number + if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() From 7b0ac718b04bdf15180aa71dc4f44ba0ca802452 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sun, 13 Aug 2023 15:12:42 +0530 Subject: [PATCH 06/19] mentioned return type --- conversions/octal_to_binary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 48075b8df8f7..e7e841f12121 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -1,4 +1,4 @@ -def octal_to_binary(octal_number): +def octal_to_binary(octal_number: str) -> str: """ Convert an octal number to binary. From 3040f9970757ce8fae91ba1c791c1909c9f17df5 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Thu, 17 Aug 2023 09:59:25 +0530 Subject: [PATCH 07/19] remove comment --- conversions/octal_to_binary.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index c62eeabff339..701a55811d8c 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -1,13 +1,4 @@ def octal_to_binary(octal_number: str) -> str: - """ - Convert an octal number to binary. - - Args: - octal_number (str): The octal number as a string. - - Returns: - str: The binary representation of the octal number. - """ binary_number = "" octal_digits = "01234567" From fa33d5cc6379890e1b363763f3a97c0769b2aa0a Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Fri, 18 Aug 2023 11:45:30 +0530 Subject: [PATCH 08/19] added documention and some test cases --- conversions/octal_to_binary.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 701a55811d8c..2c2a87dc4212 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -1,7 +1,31 @@ +""" +* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) +* Description: Convert a Octal number to Binary. + +reference for better understand + +URL: https://en.wikipedia.org/wiki/Binary_number +URL: https://en.wikipedia.org/wiki/Octal +""" def octal_to_binary(octal_number: str) -> str: binary_number = "" octal_digits = "01234567" + """ + ValueError: String to the function + >>> oct_to_decimal("Av") + Traceback (most recent call last): + ... + ValueError: Non-octal value was passed to the function + >>> oct_to_decimal("90") + Traceback (most recent call last): + ... + ValueError: octal value was passed to the function + >>> oct_to_decimal("17") + 001111 + >>> oct_to_decimal("7") + 111 + """ for digit in octal_number: if digit not in octal_digits: raise ValueError("Invalid octal digit") From ee728eef00763ba4281afd0a7520247ba670045c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 18 Aug 2023 06:17:15 +0000 Subject: [PATCH 09/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/octal_to_binary.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 2c2a87dc4212..b441e85d0375 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -7,6 +7,8 @@ URL: https://en.wikipedia.org/wiki/Binary_number URL: https://en.wikipedia.org/wiki/Octal """ + + def octal_to_binary(octal_number: str) -> str: binary_number = "" octal_digits = "01234567" From 9e75dd770e527d9ed72a7c5677556199831016aa Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Fri, 18 Aug 2023 12:47:09 +0530 Subject: [PATCH 10/19] add another test case --- conversions/octal_to_binary.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index b441e85d0375..c00c87ffc328 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -22,6 +22,13 @@ def octal_to_binary(octal_number: str) -> str: >>> oct_to_decimal("90") Traceback (most recent call last): ... + ValueError: Special Character was passed to the function + >>> oct_to_decimal("#$") + Traceback (most recent call last): + ... + ValueError: Empty String was passed to the function + >>> oct_to_decimal("") + ... ValueError: octal value was passed to the function >>> oct_to_decimal("17") 001111 From 8906c9d676a9aabb9cc54baf69e71f5f3c5feb82 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sat, 19 Aug 2023 01:06:44 +0530 Subject: [PATCH 11/19] fixes documention --- conversions/octal_to_binary.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index c00c87ffc328..7facfb7fc73d 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -2,16 +2,13 @@ * Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi) * Description: Convert a Octal number to Binary. -reference for better understand +References for better understanding: +https://en.wikipedia.org/wiki/Binary_number +https://en.wikipedia.org/wiki/Octal -URL: https://en.wikipedia.org/wiki/Binary_number -URL: https://en.wikipedia.org/wiki/Octal """ - def octal_to_binary(octal_number: str) -> str: - binary_number = "" - octal_digits = "01234567" """ ValueError: String to the function @@ -35,6 +32,8 @@ def octal_to_binary(octal_number: str) -> str: >>> oct_to_decimal("7") 111 """ + binary_number = "" + octal_digits = "01234567" for digit in octal_number: if digit not in octal_digits: raise ValueError("Invalid octal digit") @@ -52,4 +51,4 @@ def octal_to_binary(octal_number: str) -> str: if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod() \ No newline at end of file From 920fcc455962d3cb53bcd05eb1b17074b9341d5e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 18 Aug 2023 19:40:21 +0000 Subject: [PATCH 12/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/octal_to_binary.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 7facfb7fc73d..e11f41c4f7c6 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -8,8 +8,8 @@ """ -def octal_to_binary(octal_number: str) -> str: +def octal_to_binary(octal_number: str) -> str: """ ValueError: String to the function >>> oct_to_decimal("Av") @@ -51,4 +51,4 @@ def octal_to_binary(octal_number: str) -> str: if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + doctest.testmod() From d5eacd9cbbb46bee238d9f8cee95720626f72825 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sat, 19 Aug 2023 20:17:49 +0530 Subject: [PATCH 13/19] Documention and test cases added --- conversions/octal_to_binary.py | 46 ++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index e11f41c4f7c6..959b80d04d46 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -5,38 +5,53 @@ References for better understanding: https://en.wikipedia.org/wiki/Binary_number https://en.wikipedia.org/wiki/Octal - """ - def octal_to_binary(octal_number: str) -> str: """ - ValueError: String to the function - >>> oct_to_decimal("Av") + Convert an Octal number to Binary. + + ValueError: Non-octal value was passed to the function + >>> octal_to_binary("Av") Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function - >>> oct_to_decimal("90") + + ValueError: Special Character was passed to the function + >>> octal_to_binary("@#") Traceback (most recent call last): ... ValueError: Special Character was passed to the function - >>> oct_to_decimal("#$") + + ValueError: Empty String was passed to the function + >>> octal_to_binary("") Traceback (most recent call last): ... ValueError: Empty String was passed to the function - >>> oct_to_decimal("") - ... - ValueError: octal value was passed to the function - >>> oct_to_decimal("17") - 001111 - >>> oct_to_decimal("7") - 111 + + ValueError: Octal value was passed to the function + >>> octal_to_binary("17") + '001111' + + ValueError: Octal value was passed to the function + >>> octal_to_binary("7") + '111' """ + if not octal_number: + raise ValueError("Empty String was passed to the function") + + for digit in octal_number: + if not digit.isdigit(): + raise ValueError("Special Character was passed to the function") + + if digit < '0' or digit > '7': + raise ValueError("Non-octal value was passed to the function") + binary_number = "" octal_digits = "01234567" for digit in octal_number: if digit not in octal_digits: - raise ValueError("Invalid octal digit") + raise ValueError("Octal value was passed to the function") binary_digit = "" value = int(digit) @@ -50,5 +65,4 @@ def octal_to_binary(octal_number: str) -> str: if __name__ == "__main__": import doctest - - doctest.testmod() + doctest.testmod() \ No newline at end of file From eb4d80ee21f61943a96d6d89b87ab2a670645b7b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 19 Aug 2023 14:48:33 +0000 Subject: [PATCH 14/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/octal_to_binary.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 959b80d04d46..7d103a4b47fd 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -7,6 +7,7 @@ https://en.wikipedia.org/wiki/Octal """ + def octal_to_binary(octal_number: str) -> str: """ Convert an Octal number to Binary. @@ -43,8 +44,8 @@ def octal_to_binary(octal_number: str) -> str: for digit in octal_number: if not digit.isdigit(): raise ValueError("Special Character was passed to the function") - - if digit < '0' or digit > '7': + + if digit < "0" or digit > "7": raise ValueError("Non-octal value was passed to the function") binary_number = "" @@ -65,4 +66,5 @@ def octal_to_binary(octal_number: str) -> str: if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() From 37e33918e81df01d300c2cf37b35771c7c788439 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sat, 19 Aug 2023 23:53:55 +0530 Subject: [PATCH 15/19] documention problem solved --- conversions/octal_to_binary.py | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 7d103a4b47fd..0cbef890655d 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -7,52 +7,35 @@ https://en.wikipedia.org/wiki/Octal """ - def octal_to_binary(octal_number: str) -> str: """ Convert an Octal number to Binary. - - ValueError: Non-octal value was passed to the function + >>> octal_to_binary("17") + '001111' + >>> octal_to_binary("7") + '111' >>> octal_to_binary("Av") Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function - - ValueError: Special Character was passed to the function + >>> octal_to_binary("@#") Traceback (most recent call last): ... - ValueError: Special Character was passed to the function - - ValueError: Empty String was passed to the function + 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 - - ValueError: Octal value was passed to the function - >>> octal_to_binary("17") - '001111' - - ValueError: Octal value was passed to the function - >>> octal_to_binary("7") - '111' + ValueError: Empty string was passed to the function """ if not octal_number: raise ValueError("Empty String was passed to the function") - for digit in octal_number: - if not digit.isdigit(): - raise ValueError("Special Character was passed to the function") - - if digit < "0" or digit > "7": - raise ValueError("Non-octal value was passed to the function") - binary_number = "" octal_digits = "01234567" for digit in octal_number: if digit not in octal_digits: - raise ValueError("Octal value was passed to the function") + raise ValueError("Non-octal value was passed to the function") binary_digit = "" value = int(digit) @@ -66,5 +49,4 @@ def octal_to_binary(octal_number: str) -> str: if __name__ == "__main__": import doctest - doctest.testmod() From 25921bd37cdc829e1fbaff39dab546e667323a17 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 19 Aug 2023 18:25:31 +0000 Subject: [PATCH 16/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/octal_to_binary.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 0cbef890655d..18929beb6cb6 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -7,6 +7,7 @@ https://en.wikipedia.org/wiki/Octal """ + def octal_to_binary(octal_number: str) -> str: """ Convert an Octal number to Binary. @@ -18,7 +19,7 @@ def octal_to_binary(octal_number: str) -> str: Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function - + >>> octal_to_binary("@#") Traceback (most recent call last): ... @@ -49,4 +50,5 @@ def octal_to_binary(octal_number: str) -> str: if __name__ == "__main__": import doctest + doctest.testmod() From 7e9f2afa8e7e0a78e7d909f96434ff5d6bcf0bb4 Mon Sep 17 00:00:00 2001 From: BamaCharanChhandogi Date: Sun, 20 Aug 2023 15:28:16 +0530 Subject: [PATCH 17/19] error in exit 1 --- conversions/octal_to_binary.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 18929beb6cb6..202abafaf248 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -19,7 +19,7 @@ def octal_to_binary(octal_number: str) -> str: Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function - + >>> octal_to_binary("@#") Traceback (most recent call last): ... @@ -30,7 +30,7 @@ def octal_to_binary(octal_number: str) -> str: ValueError: Empty string was passed to the function """ if not octal_number: - raise ValueError("Empty String was passed to the function") + raise ValueError("Empty string was passed to the function") binary_number = "" octal_digits = "01234567" @@ -50,5 +50,4 @@ def octal_to_binary(octal_number: str) -> str: if __name__ == "__main__": import doctest - doctest.testmod() From 761e1f7404797b76acc0d91f38f8b63f9c2585e7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 20 Aug 2023 10:00:29 +0000 Subject: [PATCH 18/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- conversions/octal_to_binary.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index 202abafaf248..b70642856b73 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -19,7 +19,7 @@ def octal_to_binary(octal_number: str) -> str: Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function - + >>> octal_to_binary("@#") Traceback (most recent call last): ... @@ -50,4 +50,5 @@ def octal_to_binary(octal_number: str) -> str: if __name__ == "__main__": import doctest + doctest.testmod() From 2e91bcf5bd9b3a46e17bbb2a525a1f8220f58313 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sun, 20 Aug 2023 05:36:17 -0700 Subject: [PATCH 19/19] Apply suggestions from code review --- conversions/octal_to_binary.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/octal_to_binary.py b/conversions/octal_to_binary.py index b70642856b73..84e1e85f33ca 100644 --- a/conversions/octal_to_binary.py +++ b/conversions/octal_to_binary.py @@ -11,6 +11,7 @@ def octal_to_binary(octal_number: str) -> str: """ Convert an Octal number to Binary. + >>> octal_to_binary("17") '001111' >>> octal_to_binary("7") @@ -19,7 +20,6 @@ def octal_to_binary(octal_number: str) -> str: Traceback (most recent call last): ... ValueError: Non-octal value was passed to the function - >>> octal_to_binary("@#") Traceback (most recent call last): ...