Skip to content
29 changes: 29 additions & 0 deletions printNumbers/functions/gaussian_summation.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

def GaussianSummation(n):
'''
Helper function.
'''
if n == 0:
return n
else:
return GaussianSummation(n - 1) + n
3 changes: 3 additions & 0 deletions printNumbers/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand All @@ -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):
Expand Down
15 changes: 10 additions & 5 deletions printNumbers/printNumbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,27 @@
"""
Usage:
printNumbers.py -h --help
printNumbers.py [--fibonacci|--factorial] <operand>
printNumbers.py [--fibonacci|--factorial|--gaussiansummation] <operand>

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 : FibonacciSequence,
functionTable = { CONST_FUNC_CODE_FIBONACCI : FibonacciRecursion,
CONST_FUNC_CODE_FACTORIAL : Factorial,
CONST_FUNC_CODE_GAUSSIAN_SUMMATION : GaussianSummation,
}

#
Expand All @@ -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)
2 changes: 2 additions & 0 deletions printNumbers/unittests/runTestSuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@

import test_factorial
import test_fibonacci
import test_gaussian_summation


def suite():
suite = unittest.TestSuite()

suite.addTest(test_factorial.suite())
suite.addTest(test_fibonacci.suite())
suite.addTest(test_gaussian_summation.suite())

return suite

Expand Down
56 changes: 56 additions & 0 deletions printNumbers/unittests/test_gaussian_summation.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

#
# Unit tests: 'gaussian_summation'.
#

import sys, os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import unittest
from functions.gaussian_summation import *

class TestGaussianSummation(unittest.TestCase):

def test_value_0(self):
self.assertEqual(GaussianSummation(0), 0)

def test_value_1(self):
self.assertEqual(GaussianSummation(1), 1)

def test_value_2(self):
self.assertEqual(GaussianSummation(2), 3)

def test_value_20(self):
self.assertEqual(GaussianSummation(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()