From 8db2c960b8b06d20d87f02265e34b2050d78249a Mon Sep 17 00:00:00 2001 From: Twiss Date: Wed, 27 Oct 2021 13:50:04 +0100 Subject: [PATCH 1/2] Commit to trigger workflows. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1ec0bba..48e9648 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ # CI MPM -Toy repo for CI lecture. +Toy repo for CI lecture and some simple changes From 93b691ecc76844f1e900047c28bbd5af4a603baa Mon Sep 17 00:00:00 2001 From: Twiss Date: Wed, 27 Oct 2021 14:27:55 +0100 Subject: [PATCH 2/2] Add factorial function. --- simple_functions/functions1.py | 10 +++++++++- tests/test_simple_functions.py | 16 +++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/simple_functions/functions1.py b/simple_functions/functions1.py index 8c63a4d..c9327bd 100644 --- a/simple_functions/functions1.py +++ b/simple_functions/functions1.py @@ -1,5 +1,7 @@ -__all__ = ['my_sum'] +from functools import lru_cache + +__all__ = ['my_sum','factorial'] def my_sum(iterable): @@ -7,3 +9,9 @@ def my_sum(iterable): for i in iterable: tot += i return tot + +@lru_cache # Note: -> @cache in python >= 3.9 +def factorial(n): + return n * factorial(n-1) if n else 1 + + diff --git a/tests/test_simple_functions.py b/tests/test_simple_functions.py index 5a03b52..b731df5 100644 --- a/tests/test_simple_functions.py +++ b/tests/test_simple_functions.py @@ -1,7 +1,7 @@ import pytest - -from simple_functions import my_sum - +import sys +sys.path.append("../") +from simple_functions import my_sum, factorial class TestSimpleFunctions(object): '''Class to test our simple functions are working correctly''' @@ -14,3 +14,13 @@ def test_my_add(self, iterable, expected): '''Test our add function''' isum = my_sum(iterable) assert isum == expected + + @pytest.mark.parametrize('number, expected', [ + (5, 120), + (3, 6), + (1, 1) + ]) + def test_factorial(self, number, expected): + '''Test our factorial function''' + answer = factorial(number) + assert answer == expected