From 61eae9d4d0315ccbc3de98f0124c7024687b3a2d Mon Sep 17 00:00:00 2001 From: schererc Date: Wed, 16 Nov 2022 13:42:56 +0100 Subject: [PATCH 1/4] printNumbers.py: changed FibonacciSequence to FibonacciRecursion in functionTable --- printNumbers/printNumbers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/printNumbers/printNumbers.py b/printNumbers/printNumbers.py index 9f9ef0e..e396870 100755 --- a/printNumbers/printNumbers.py +++ b/printNumbers/printNumbers.py @@ -43,7 +43,7 @@ # # FUNCTION TABLE # -functionTable = { CONST_FUNC_CODE_FIBONACCI : FibonacciSequence, +functionTable = { CONST_FUNC_CODE_FIBONACCI : FibonacciRecursion, CONST_FUNC_CODE_FACTORIAL : Factorial, } From cd9d0d5cb791af75ce105a3603e4142ab7296119 Mon Sep 17 00:00:00 2001 From: schererc Date: Thu, 17 Nov 2022 12:11:13 +0100 Subject: [PATCH 2/4] created test for new function gaussian_summation --- printNumbers/unittests/runTestSuite.py | 1 + .../unittests/test_gaussian_summation.py | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 printNumbers/unittests/test_gaussian_summation.py diff --git a/printNumbers/unittests/runTestSuite.py b/printNumbers/unittests/runTestSuite.py index f3dca23..bc3d436 100755 --- a/printNumbers/unittests/runTestSuite.py +++ b/printNumbers/unittests/runTestSuite.py @@ -34,6 +34,7 @@ def suite(): suite.addTest(test_factorial.suite()) suite.addTest(test_fibonacci.suite()) + suite.addTest(test_gaussian_summation.suite()) return suite diff --git a/printNumbers/unittests/test_gaussian_summation.py b/printNumbers/unittests/test_gaussian_summation.py new file mode 100644 index 0000000..8a538d1 --- /dev/null +++ b/printNumbers/unittests/test_gaussian_summation.py @@ -0,0 +1,56 @@ +# -*- coding: utf-8 -*- +# +# test_factorial.py +# +# This file is part of PrintNumbers. +# +# Copyright (C) 2022 C. Scherer, Simulation & Datalab Neuroscience, JSC, FZ Juelich +# +# PrintNumbers is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# PrintNumbers is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with PrintNumbers. If not, see . + +# +# Unit tests: 'gaussian_summation'. +# + +import sys, os +sys.path.append(os.path.join(os.path.dirname(__file__), '..')) + +import unittest +from functions.factorial import * + +class TestGaussianSummation(unittest.TestCase): + + def test_value_0(self): + self.assertEqual(Factorial(0), 0) + + def test_value_1(self): + self.assertEqual(Factorial(1), 1) + + def test_value_2(self): + self.assertEqual(Factorial(2), 3) + + def test_value_20(self): + self.assertEqual(Factorial(20), 210) + + +def suite(): + suite = unittest.makeSuite(TestGaussianSummation, 'test') + return suite + +def run(): + runner = unittest.TextTestRunner(verbosity = 2) + runner.run(suite()) + +if __name__ == "__main__": + run() \ No newline at end of file From a4f8a6e06f489eb8ff08c85b2969ab2b689d6977 Mon Sep 17 00:00:00 2001 From: schererc Date: Thu, 17 Nov 2022 13:17:00 +0100 Subject: [PATCH 3/4] bug fix for test for new function gaussian_summation --- printNumbers/unittests/runTestSuite.py | 1 + printNumbers/unittests/test_gaussian_summation.py | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/printNumbers/unittests/runTestSuite.py b/printNumbers/unittests/runTestSuite.py index bc3d436..c79504e 100755 --- a/printNumbers/unittests/runTestSuite.py +++ b/printNumbers/unittests/runTestSuite.py @@ -27,6 +27,7 @@ import test_factorial import test_fibonacci +import test_gaussian_summation def suite(): diff --git a/printNumbers/unittests/test_gaussian_summation.py b/printNumbers/unittests/test_gaussian_summation.py index 8a538d1..eab69c2 100644 --- a/printNumbers/unittests/test_gaussian_summation.py +++ b/printNumbers/unittests/test_gaussian_summation.py @@ -27,21 +27,21 @@ sys.path.append(os.path.join(os.path.dirname(__file__), '..')) import unittest -from functions.factorial import * +from functions.gaussian_summation import * class TestGaussianSummation(unittest.TestCase): def test_value_0(self): - self.assertEqual(Factorial(0), 0) + self.assertEqual(GaussianSummation(0), 0) def test_value_1(self): - self.assertEqual(Factorial(1), 1) + self.assertEqual(GaussianSummation(1), 1) def test_value_2(self): - self.assertEqual(Factorial(2), 3) + self.assertEqual(GaussianSummation(2), 3) def test_value_20(self): - self.assertEqual(Factorial(20), 210) + self.assertEqual(GaussianSummation(20), 210) def suite(): From cb4888b57bcfadd6cae71b8fcac9ad9bb5682a27 Mon Sep 17 00:00:00 2001 From: schererc Date: Thu, 17 Nov 2022 13:17:12 +0100 Subject: [PATCH 4/4] implementation of new function gaussian_summation --- printNumbers/functions/gaussian_summation.py | 29 ++++++++++++++++++++ printNumbers/parameters.py | 3 ++ printNumbers/printNumbers.py | 13 ++++++--- 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 printNumbers/functions/gaussian_summation.py diff --git a/printNumbers/functions/gaussian_summation.py b/printNumbers/functions/gaussian_summation.py new file mode 100644 index 0000000..7b67fde --- /dev/null +++ b/printNumbers/functions/gaussian_summation.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# +# fibonacci.py +# +# This file is part of printNumbers. +# +# Copyright (C) 2022 C. Scherer, Simulation & Datalab Neuroscience, JSC, FZ Juelich +# +# printNumbers is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. +# +# printNumbers is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with printNumbers. If not, see . + +def GaussianSummation(n): + ''' + Helper function. + ''' + if n == 0: + return n + else: + return GaussianSummation(n - 1) + n diff --git a/printNumbers/parameters.py b/printNumbers/parameters.py index aebe0c3..0c57734 100755 --- a/printNumbers/parameters.py +++ b/printNumbers/parameters.py @@ -29,6 +29,7 @@ CONST_MAX_OPERAND_VAL = 20 CONST_FUNC_CODE_FIBONACCI = 0 CONST_FUNC_CODE_FACTORIAL = 1 +CONST_FUNC_CODE_GAUSSIAN_SUMMATION = 2 class Parameters(object): @@ -43,6 +44,8 @@ def __setParameters(self, cmdLineArgs): self.functionIndex = CONST_FUNC_CODE_FIBONACCI elif cmdLineArgs['--factorial']: self.functionIndex = CONST_FUNC_CODE_FACTORIAL + elif cmdLineArgs['--gaussiansummation']: + self.functionIndex = CONST_FUNC_CODE_GAUSSIAN_SUMMATION @property def operand(self): diff --git a/printNumbers/printNumbers.py b/printNumbers/printNumbers.py index e396870..37dbd2a 100755 --- a/printNumbers/printNumbers.py +++ b/printNumbers/printNumbers.py @@ -27,24 +27,27 @@ """ Usage: printNumbers.py -h --help - printNumbers.py [--fibonacci|--factorial] + printNumbers.py [--fibonacci|--factorial|--gaussiansummation] Options: - -h --help Print usage. - --fibonacci Print the fibonacci sequence. - --factorial Print the factorial. + -h --help Print usage. + --fibonacci Print the fibonacci sequence. + --factorial Print the factorial. + --gaussiansummation Print the sum of the first n integers """ from docopt import docopt from parameters import * from functions.fibonacci import * from functions.factorial import * +from functions.gaussian_summation import * # # FUNCTION TABLE # functionTable = { CONST_FUNC_CODE_FIBONACCI : FibonacciRecursion, CONST_FUNC_CODE_FACTORIAL : Factorial, + CONST_FUNC_CODE_GAUSSIAN_SUMMATION : GaussianSummation, } # @@ -67,3 +70,5 @@ print('fib(' + str(params.operand) + ') =', result) elif params.functionIndex == CONST_FUNC_CODE_FACTORIAL: print(str(params.operand) + '! =', str(result)) + elif params.functionIndex == CONST_FUNC_CODE_GAUSSIAN_SUMMATION: + print('gaussian_summation(' + str(params.operand) + ') =', result)