From 64b7654fcf0f8bd30157ea1bdcf6e54c54b49ba2 Mon Sep 17 00:00:00 2001 From: Grigoriy Hanin <43445998+haningrisha@users.noreply.github.com> Date: Sat, 5 Jun 2021 20:46:04 +0300 Subject: [PATCH 1/3] Prime factor input check --- maths/basic_maths.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/maths/basic_maths.py b/maths/basic_maths.py index 07ee3b3df296..83ac147f19a6 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -6,7 +6,17 @@ def prime_factors(n: int) -> list: """Find Prime Factors. >>> prime_factors(100) [2, 2, 5, 5] + >>> prime_factors(0) + Traceback (most recent call last): + ... + ValueError: Only positive integers have prime factors + >>> prime_factors(-10) + Traceback (most recent call last): + ... + ValueError: Only positive integers have prime factors """ + if n <= 0: + raise ValueError("Only positive integers have prime factors") pf = [] while n % 2 == 0: pf.append(2) @@ -74,7 +84,6 @@ def euler_phi(n: int) -> int: if __name__ == "__main__": - print(prime_factors(100)) - print(number_of_divisors(100)) - print(sum_of_divisors(100)) - print(euler_phi(100)) + import doctest + + doctest.testmod() From bc5cb3348cf59120fbd4dbb3b91c7ebd7c781a37 Mon Sep 17 00:00:00 2001 From: Grigoriy Hanin <43445998+haningrisha@users.noreply.github.com> Date: Sat, 5 Jun 2021 20:52:46 +0300 Subject: [PATCH 2/3] number_of_divisors input check --- maths/basic_maths.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/maths/basic_maths.py b/maths/basic_maths.py index 83ac147f19a6..f84ba596da8b 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -34,7 +34,17 @@ def number_of_divisors(n: int) -> int: """Calculate Number of Divisors of an Integer. >>> number_of_divisors(100) 9 + >>> number_of_divisors(0) + Traceback (most recent call last): + ... + ValueError: Only positive numbers are accepted + >>> number_of_divisors(-10) + Traceback (most recent call last): + ... + ValueError: Only positive numbers are accepted """ + if n <= 0: + raise ValueError("Only positive numbers are accepted") div = 1 temp = 1 while n % 2 == 0: From 8c5ba779367b5d2fcb6119f7bb008e93bea00803 Mon Sep 17 00:00:00 2001 From: Grigoriy Hanin <43445998+haningrisha@users.noreply.github.com> Date: Sat, 5 Jun 2021 20:55:23 +0300 Subject: [PATCH 3/3] sum_of_divisors input check --- maths/basic_maths.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/maths/basic_maths.py b/maths/basic_maths.py index f84ba596da8b..47d3d91b397d 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -64,7 +64,17 @@ def sum_of_divisors(n: int) -> int: """Calculate Sum of Divisors. >>> sum_of_divisors(100) 217 + >>> sum_of_divisors(0) + Traceback (most recent call last): + ... + ValueError: Only positive numbers are accepted + >>> sum_of_divisors(-10) + Traceback (most recent call last): + ... + ValueError: Only positive numbers are accepted """ + if n <= 0: + raise ValueError("Only positive numbers are accepted") s = 1 temp = 1 while n % 2 == 0: