diff --git a/participants/scipy_002/pr_tutorial/simple_functions.py b/participants/scipy_002/pr_tutorial/simple_functions.py index c3dcf8f..f9caaa6 100644 --- a/participants/scipy_002/pr_tutorial/simple_functions.py +++ b/participants/scipy_002/pr_tutorial/simple_functions.py @@ -10,3 +10,17 @@ def factorial(value): return 1 else: return value * factorial(value - 1) + +def is_prime(value): + if value < 2: + raise ValueError(f"Don't pass any numbers smaller than 2. Received {value}.") + elif value == 2: + return True + + current_val = 2 + while current_val < value: + if value % current_val == 0: + return False + + current_val += 1 + return True \ No newline at end of file diff --git a/participants/scipy_002/pr_tutorial/tests/test_simple_function.py b/participants/scipy_002/pr_tutorial/tests/test_simple_function.py index 1982130..662bcc8 100644 --- a/participants/scipy_002/pr_tutorial/tests/test_simple_function.py +++ b/participants/scipy_002/pr_tutorial/tests/test_simple_function.py @@ -1,7 +1,29 @@ -from pr_tutorial.simple_functions import factorial - +from pr_tutorial.simple_functions import factorial, is_prime def test_factorial_3(): """Simplest test for one crete case""" assert factorial(3) == 6 + +def test_is_prime(): + """Simple test for naive is_prime() function.""" + primes_below_100 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, + 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, + 89, 97] + + for num in range(1, 100): + if num == 1: + try: + is_prime(num) + except ValueError as e: + continue + except Exception as e: # wrong exception raised + assert False + assert False + else: + primeOrNot = is_prime(num) + + if num in primes_below_100: + assert primeOrNot is True + else: + assert primeOrNot is False \ No newline at end of file