From 3f7750ca44bf5cb7e932bfa9089ea39689dba47d Mon Sep 17 00:00:00 2001 From: cybov Date: Tue, 13 Oct 2020 20:43:35 +0100 Subject: [PATCH 1/8] Added decimal_to_binary_recursion.py --- conversions/decimal_to_binary_recursion.py | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 conversions/decimal_to_binary_recursion.py diff --git a/conversions/decimal_to_binary_recursion.py b/conversions/decimal_to_binary_recursion.py new file mode 100644 index 000000000000..218a0cd73b99 --- /dev/null +++ b/conversions/decimal_to_binary_recursion.py @@ -0,0 +1,67 @@ +""" Convert decimal values to binary using recursion method """ + + +def bin_recursive(decimal: int) -> str: + """ + The funtion takes in a positive integer value + and returns its binary equivalent. + >>> bin_recursive(1000) + '1111101000' + >>> bin_recursive("72") + Traceback (most recent call last): + ... + TypeError: unsupported operand type(s) for //: 'str' and 'int' + >>> bin_recursive("number") + Traceback (most recent call last): + ... + TypeError: unsupported operand type(s) for //: 'str' and 'int' + """ + # Initialize exit base of the recursion function + if decimal == 1 or decimal == 0: + return str(decimal) + return bin_recursive(decimal // 2) + str(decimal % 2) + + +def main(number: str) -> str: + """ + This function takes a parameter "number", + raises a ValueError for wrong inputs, + calls the function above and returns the output + with prefix "0b" & "-0b" for positive + and negative integers respectively. + >>> main(0) + '0b0' + >>> main(40) + '0b101000' + >>> main(-40) + '-0b101000' + >>> main(40.8) + Traceback (most recent call last): + ... + ValueError: Input value is not an integer + >>> main("forty") + Traceback (most recent call last): + ... + ValueError: Input value is not an integer + """ + number = str(number).strip() + negative = False + + if number.startswith("-"): + negative = True + number = number[1:] + + if number.isnumeric(): + if negative: + binary = "-0b" + bin_recursive(int(number)) + else: + binary = "0b" + bin_recursive(int(number)) + return binary + else: + raise ValueError("Input value is not an integer") + + +if __name__ == "__main__": + from doctest import testmod + + testmod() From c4fc5c88c5b80dee0f985ccd7e4dd11af1418b49 Mon Sep 17 00:00:00 2001 From: cybov Date: Tue, 13 Oct 2020 21:54:42 +0100 Subject: [PATCH 2/8] Added decimal_to_binary_recursion.py --- conversions/decimal_to_binary_recursion.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/conversions/decimal_to_binary_recursion.py b/conversions/decimal_to_binary_recursion.py index 218a0cd73b99..4735ab812a94 100644 --- a/conversions/decimal_to_binary_recursion.py +++ b/conversions/decimal_to_binary_recursion.py @@ -1,17 +1,14 @@ -""" Convert decimal values to binary using recursion method """ - - -def bin_recursive(decimal: int) -> str: +def binary_recursive(decimal: int) -> str: """ - The funtion takes in a positive integer value + This takes in a positive integer value and returns its binary equivalent. - >>> bin_recursive(1000) + >>> binary_recursive(1000) '1111101000' - >>> bin_recursive("72") + >>> binary_recursive("72") Traceback (most recent call last): ... TypeError: unsupported operand type(s) for //: 'str' and 'int' - >>> bin_recursive("number") + >>> binary_recursive("number") Traceback (most recent call last): ... TypeError: unsupported operand type(s) for //: 'str' and 'int' @@ -19,7 +16,8 @@ def bin_recursive(decimal: int) -> str: # Initialize exit base of the recursion function if decimal == 1 or decimal == 0: return str(decimal) - return bin_recursive(decimal // 2) + str(decimal % 2) + result = binary_recursive(decimal // 2) + str(decimal % 2) + return str(result) def main(number: str) -> str: @@ -53,9 +51,9 @@ def main(number: str) -> str: if number.isnumeric(): if negative: - binary = "-0b" + bin_recursive(int(number)) + binary = "-0b" + binary_recursive(int(number)) else: - binary = "0b" + bin_recursive(int(number)) + binary = "0b" + binary_recursive(int(number)) return binary else: raise ValueError("Input value is not an integer") From 9d05f7f75ec62520312486a5846f81511a8ed247 Mon Sep 17 00:00:00 2001 From: cybov Date: Wed, 14 Oct 2020 09:22:56 +0100 Subject: [PATCH 3/8] Made changes to docstring --- conversions/decimal_to_binary_recursion.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/conversions/decimal_to_binary_recursion.py b/conversions/decimal_to_binary_recursion.py index 4735ab812a94..57b51deae7d2 100644 --- a/conversions/decimal_to_binary_recursion.py +++ b/conversions/decimal_to_binary_recursion.py @@ -1,7 +1,6 @@ def binary_recursive(decimal: int) -> str: """ - This takes in a positive integer value - and returns its binary equivalent. + Take a positive integer value and return its binary equivalent. >>> binary_recursive(1000) '1111101000' >>> binary_recursive("72") @@ -16,17 +15,14 @@ def binary_recursive(decimal: int) -> str: # Initialize exit base of the recursion function if decimal == 1 or decimal == 0: return str(decimal) - result = binary_recursive(decimal // 2) + str(decimal % 2) - return str(result) + return binary_recursive(decimal // 2) + str(decimal % 2) def main(number: str) -> str: """ - This function takes a parameter "number", - raises a ValueError for wrong inputs, - calls the function above and returns the output - with prefix "0b" & "-0b" for positive - and negative integers respectively. + Take an integer value and raise ValueError for wrong inputs, + call the function above and return the output with prefix "0b" & "-0b" + for positive and negative integers respectively. >>> main(0) '0b0' >>> main(40) From 31b23185122adfddaf4b16eff766115e08053acb Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 14 Oct 2020 11:50:44 +0200 Subject: [PATCH 4/8] Use divmod() --- conversions/decimal_to_binary_recursion.py | 26 +++++++++------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/conversions/decimal_to_binary_recursion.py b/conversions/decimal_to_binary_recursion.py index 57b51deae7d2..0051d6e704c7 100644 --- a/conversions/decimal_to_binary_recursion.py +++ b/conversions/decimal_to_binary_recursion.py @@ -12,10 +12,11 @@ def binary_recursive(decimal: int) -> str: ... TypeError: unsupported operand type(s) for //: 'str' and 'int' """ - # Initialize exit base of the recursion function - if decimal == 1 or decimal == 0: + # Set the exit cases for the recursion + if decimal in (0, 1): return str(decimal) - return binary_recursive(decimal // 2) + str(decimal % 2) + div, mod = divmod(decimal, 2) + return binary_recursive(div) + mod def main(number: str) -> str: @@ -39,20 +40,13 @@ def main(number: str) -> str: ValueError: Input value is not an integer """ number = str(number).strip() - negative = False - - if number.startswith("-"): - negative = True - number = number[1:] - - if number.isnumeric(): - if negative: - binary = "-0b" + binary_recursive(int(number)) - else: - binary = "0b" + binary_recursive(int(number)) - return binary - else: + if not number: + raise ValueError("No input value was provided") + negative = "-" if number.startswith("-") else "" + number = number.lstrip("-") + if not number.isnumeric(): raise ValueError("Input value is not an integer") + return f"{negative}0b{binary_recursive(int(number))}" if __name__ == "__main__": From cba9e641db655f24c014d9b34c21b15fab508695 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 14 Oct 2020 11:51:20 +0200 Subject: [PATCH 5/8] binary_recursive(div) + str(mod) --- conversions/decimal_to_binary_recursion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/decimal_to_binary_recursion.py b/conversions/decimal_to_binary_recursion.py index 0051d6e704c7..1bb410aa9b3d 100644 --- a/conversions/decimal_to_binary_recursion.py +++ b/conversions/decimal_to_binary_recursion.py @@ -16,7 +16,7 @@ def binary_recursive(decimal: int) -> str: if decimal in (0, 1): return str(decimal) div, mod = divmod(decimal, 2) - return binary_recursive(div) + mod + return binary_recursive(div) + str(mod) def main(number: str) -> str: From c995c41dd448ece667eef88376786f14999c2244 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 14 Oct 2020 12:06:59 +0200 Subject: [PATCH 6/8] Be kind with user input if possible --- conversions/decimal_to_binary_recursion.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/conversions/decimal_to_binary_recursion.py b/conversions/decimal_to_binary_recursion.py index 1bb410aa9b3d..147433cf8f3f 100644 --- a/conversions/decimal_to_binary_recursion.py +++ b/conversions/decimal_to_binary_recursion.py @@ -6,13 +6,14 @@ def binary_recursive(decimal: int) -> str: >>> binary_recursive("72") Traceback (most recent call last): ... - TypeError: unsupported operand type(s) for //: 'str' and 'int' + TypeError: unsupported operand type(s) for divmod(): 'str' and 'int' >>> binary_recursive("number") Traceback (most recent call last): ... - TypeError: unsupported operand type(s) for //: 'str' and 'int' + TypeError: unsupported operand type(s) for divmod(): 'str' and 'int' """ # Set the exit cases for the recursion + decimal = int(decimal) if decimal in (0, 1): return str(decimal) div, mod = divmod(decimal, 2) From 997ba917994f3c5656f70f99e293b65194fcd1ca Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 14 Oct 2020 12:12:46 +0200 Subject: [PATCH 7/8] Update decimal_to_binary_recursion.py --- conversions/decimal_to_binary_recursion.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/conversions/decimal_to_binary_recursion.py b/conversions/decimal_to_binary_recursion.py index 147433cf8f3f..84d4a6e3b3eb 100644 --- a/conversions/decimal_to_binary_recursion.py +++ b/conversions/decimal_to_binary_recursion.py @@ -4,17 +4,14 @@ def binary_recursive(decimal: int) -> str: >>> binary_recursive(1000) '1111101000' >>> binary_recursive("72") - Traceback (most recent call last): - ... - TypeError: unsupported operand type(s) for divmod(): 'str' and 'int' + '1001000' >>> binary_recursive("number") Traceback (most recent call last): ... TypeError: unsupported operand type(s) for divmod(): 'str' and 'int' """ - # Set the exit cases for the recursion decimal = int(decimal) - if decimal in (0, 1): + if decimal in (0, 1): # Exit cases for the recursion return str(decimal) div, mod = divmod(decimal, 2) return binary_recursive(div) + str(mod) From 0812320a0985006242634331f69def0bdc196550 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 14 Oct 2020 12:18:38 +0200 Subject: [PATCH 8/8] ValueError: invalid literal for int() with base 10: 'number' --- conversions/decimal_to_binary_recursion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/decimal_to_binary_recursion.py b/conversions/decimal_to_binary_recursion.py index 84d4a6e3b3eb..c149ea86592f 100644 --- a/conversions/decimal_to_binary_recursion.py +++ b/conversions/decimal_to_binary_recursion.py @@ -8,7 +8,7 @@ def binary_recursive(decimal: int) -> str: >>> binary_recursive("number") Traceback (most recent call last): ... - TypeError: unsupported operand type(s) for divmod(): 'str' and 'int' + ValueError: invalid literal for int() with base 10: 'number' """ decimal = int(decimal) if decimal in (0, 1): # Exit cases for the recursion