From 4bf73aa3054dbbaf1b83c5b12998fdebab0652cb Mon Sep 17 00:00:00 2001 From: ken437 Date: Fri, 15 May 2020 00:31:46 -0400 Subject: [PATCH 01/10] Added file aliquot_sum.py containing a function to find theo --- maths/aliquot_sum.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 maths/aliquot_sum.py diff --git a/maths/aliquot_sum.py b/maths/aliquot_sum.py new file mode 100644 index 000000000000..c292270bcf7f --- /dev/null +++ b/maths/aliquot_sum.py @@ -0,0 +1,35 @@ +def aliquot_sum(input_num): + """ + Finds the aliquot sum of an input integer, where the + aliquot sum of a number n is defined as the sum of all + natural numbers less than n that divide n evenly. For + example, the aliquot sum of 15 is 1 + 3 + 5 = 9. This is + a simple O(n) implementation. + @param input_num: a positive integer whose aliquot sum is to be found + @return: the aliquot sum of input_num, if input_num is positive. + Otherwise, return the string "Please enter a positive integer" + + >>> aliquot_sum(15) + 9 + >>> aliquot_sum(6) + 6 + >>> aliquot_sum(-1) + 'Please enter a positive integer' + >>> aliquot_sum(12) + 16 + >>> aliquot_sum(19) + 1 + """ + if input_num <= 0: + return "Please enter a positive integer" + sum = 0 + for divisor in range(1, input_num): + if input_num % divisor == 0: + sum += divisor + return sum + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 245369f5526bc1d5bcf272db7d42f42e3b006301 Mon Sep 17 00:00:00 2001 From: ken437 Date: Fri, 15 May 2020 00:42:18 -0400 Subject: [PATCH 02/10] Added parameter type info to aliquot_sum.py --- maths/aliquot_sum.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/maths/aliquot_sum.py b/maths/aliquot_sum.py index c292270bcf7f..76e24c043250 100644 --- a/maths/aliquot_sum.py +++ b/maths/aliquot_sum.py @@ -1,4 +1,7 @@ -def aliquot_sum(input_num): +from typing import Union + + +def aliquot_sum(input_num: int) -> Union[int, str]: """ Finds the aliquot sum of an input integer, where the aliquot sum of a number n is defined as the sum of all @@ -8,6 +11,7 @@ def aliquot_sum(input_num): @param input_num: a positive integer whose aliquot sum is to be found @return: the aliquot sum of input_num, if input_num is positive. Otherwise, return the string "Please enter a positive integer" + Wikipedia Explanation: https://en.wikipedia.org/wiki/Aliquot_sum >>> aliquot_sum(15) 9 From 05b344f2cb6746ee46693684d20d3b0a262d1eab Mon Sep 17 00:00:00 2001 From: Kenneth P <41343159+ken437@users.noreply.github.com> Date: Fri, 15 May 2020 01:29:20 -0400 Subject: [PATCH 03/10] Update maths/aliquot_sum.py Co-authored-by: Christian Clauss --- maths/aliquot_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/aliquot_sum.py b/maths/aliquot_sum.py index 76e24c043250..843886a76387 100644 --- a/maths/aliquot_sum.py +++ b/maths/aliquot_sum.py @@ -1,7 +1,7 @@ from typing import Union -def aliquot_sum(input_num: int) -> Union[int, str]: +def aliquot_sum(input_num: int) -> int: """ Finds the aliquot sum of an input integer, where the aliquot sum of a number n is defined as the sum of all From e014506b4762f4bffd98284486b011e977a5678b Mon Sep 17 00:00:00 2001 From: Kenneth P <41343159+ken437@users.noreply.github.com> Date: Fri, 15 May 2020 01:29:31 -0400 Subject: [PATCH 04/10] Update maths/aliquot_sum.py Co-authored-by: Christian Clauss --- maths/aliquot_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/aliquot_sum.py b/maths/aliquot_sum.py index 843886a76387..36d100070df8 100644 --- a/maths/aliquot_sum.py +++ b/maths/aliquot_sum.py @@ -30,7 +30,7 @@ def aliquot_sum(input_num: int) -> int: for divisor in range(1, input_num): if input_num % divisor == 0: sum += divisor - return sum + return sum(divisor for divisor in range(1, input_num) if input_num % divisor == 0) if __name__ == "__main__": From a8a04dff2bcb98bdeecd2b0c9f1aeb82ae86f668 Mon Sep 17 00:00:00 2001 From: Kenneth P <41343159+ken437@users.noreply.github.com> Date: Fri, 15 May 2020 01:39:51 -0400 Subject: [PATCH 05/10] Updated code to raise ValueErrors if input is bad --- maths/aliquot_sum.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/maths/aliquot_sum.py b/maths/aliquot_sum.py index 36d100070df8..8868ec545090 100644 --- a/maths/aliquot_sum.py +++ b/maths/aliquot_sum.py @@ -18,14 +18,18 @@ def aliquot_sum(input_num: int) -> int: >>> aliquot_sum(6) 6 >>> aliquot_sum(-1) - 'Please enter a positive integer' + ValueError + >>> aliquot_sum(0) + ValueError + >>> aliquot_sum(1.6) + ValueError >>> aliquot_sum(12) 16 >>> aliquot_sum(19) 1 """ - if input_num <= 0: - return "Please enter a positive integer" + if !isinstance(input_num, int) && input_num <= 0: + raise ValueError sum = 0 for divisor in range(1, input_num): if input_num % divisor == 0: From 357695c269820aff966849bf67f1677f961e7c51 Mon Sep 17 00:00:00 2001 From: Kenneth P <41343159+ken437@users.noreply.github.com> Date: Fri, 15 May 2020 01:41:46 -0400 Subject: [PATCH 06/10] Fixed logical operators --- maths/aliquot_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/aliquot_sum.py b/maths/aliquot_sum.py index 8868ec545090..d37c0f74dc77 100644 --- a/maths/aliquot_sum.py +++ b/maths/aliquot_sum.py @@ -28,7 +28,7 @@ def aliquot_sum(input_num: int) -> int: >>> aliquot_sum(19) 1 """ - if !isinstance(input_num, int) && input_num <= 0: + if (not isinstance(input_num, int)) or (input_num <= 0): raise ValueError sum = 0 for divisor in range(1, input_num): From 1e2ba33456df7092ef46986066ae29211095ca2a Mon Sep 17 00:00:00 2001 From: Kenneth P <41343159+ken437@users.noreply.github.com> Date: Fri, 15 May 2020 01:43:04 -0400 Subject: [PATCH 07/10] Removed unnecessary import --- maths/aliquot_sum.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/maths/aliquot_sum.py b/maths/aliquot_sum.py index d37c0f74dc77..91cf2b8ca96e 100644 --- a/maths/aliquot_sum.py +++ b/maths/aliquot_sum.py @@ -1,6 +1,3 @@ -from typing import Union - - def aliquot_sum(input_num: int) -> int: """ Finds the aliquot sum of an input integer, where the From 2a1116723baf7166007dae0e7675cbe4cbffa876 Mon Sep 17 00:00:00 2001 From: ken437 Date: Fri, 15 May 2020 01:52:43 -0400 Subject: [PATCH 08/10] Updated aliquot_sum.py --- maths/aliquot_sum.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/maths/aliquot_sum.py b/maths/aliquot_sum.py index 91cf2b8ca96e..cefde1fab9fb 100644 --- a/maths/aliquot_sum.py +++ b/maths/aliquot_sum.py @@ -15,22 +15,28 @@ def aliquot_sum(input_num: int) -> int: >>> aliquot_sum(6) 6 >>> aliquot_sum(-1) - ValueError + Traceback (most recent call last): + ... + ValueError: Input must be positive >>> aliquot_sum(0) - ValueError + Traceback (most recent call last): + ... + ValueError: Input must be positive >>> aliquot_sum(1.6) - ValueError + Traceback (most recent call last): + ... + ValueError: Input must be an integer >>> aliquot_sum(12) 16 + >>> aliquot_sum(1) + 0 >>> aliquot_sum(19) 1 """ - if (not isinstance(input_num, int)) or (input_num <= 0): - raise ValueError - sum = 0 - for divisor in range(1, input_num): - if input_num % divisor == 0: - sum += divisor + if not isinstance(input_num, int): + raise ValueError("Input must be an integer") + if input_num <= 0: + raise ValueError("Input must be positive") return sum(divisor for divisor in range(1, input_num) if input_num % divisor == 0) From eb79bddaf3bdd1b8f0133f4b85a96f91750aebeb Mon Sep 17 00:00:00 2001 From: ken437 Date: Fri, 15 May 2020 01:54:32 -0400 Subject: [PATCH 09/10] fixed formatting --- maths/aliquot_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/aliquot_sum.py b/maths/aliquot_sum.py index cefde1fab9fb..7b52dffa5863 100644 --- a/maths/aliquot_sum.py +++ b/maths/aliquot_sum.py @@ -33,7 +33,7 @@ def aliquot_sum(input_num: int) -> int: >>> aliquot_sum(19) 1 """ - if not isinstance(input_num, int): + if not isinstance(input_num, int): raise ValueError("Input must be an integer") if input_num <= 0: raise ValueError("Input must be positive") From 366b5e89a30cb91fed7baaf101c6d3535ea455e5 Mon Sep 17 00:00:00 2001 From: ken437 Date: Fri, 15 May 2020 02:04:18 -0400 Subject: [PATCH 10/10] fixed documentation --- maths/aliquot_sum.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/aliquot_sum.py b/maths/aliquot_sum.py index 7b52dffa5863..ac5fa58f41cf 100644 --- a/maths/aliquot_sum.py +++ b/maths/aliquot_sum.py @@ -7,7 +7,7 @@ def aliquot_sum(input_num: int) -> int: a simple O(n) implementation. @param input_num: a positive integer whose aliquot sum is to be found @return: the aliquot sum of input_num, if input_num is positive. - Otherwise, return the string "Please enter a positive integer" + Otherwise, raise a ValueError Wikipedia Explanation: https://en.wikipedia.org/wiki/Aliquot_sum >>> aliquot_sum(15)