From d9f04d6cf5c117c7d2462fa058983d84ea0f7622 Mon Sep 17 00:00:00 2001 From: PhilipCarlsson Date: Wed, 16 Nov 2022 13:46:33 +0100 Subject: [PATCH] Added Square Function --- printNumbers/functions/square.py | 28 ++++++++++++++++++++++++++++ printNumbers/printNumbers.py | 6 +++++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100755 printNumbers/functions/square.py diff --git a/printNumbers/functions/square.py b/printNumbers/functions/square.py new file mode 100755 index 0000000..2f4930f --- /dev/null +++ b/printNumbers/functions/square.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# +# square.py +# +# This file is part of printNumbers. +# +# Copyright (C) 2022 P. Carlsson, IEK-8, 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 square(n): + ''' + :param n: Operand + :return: n*n + ''' + + return (n*n) diff --git a/printNumbers/printNumbers.py b/printNumbers/printNumbers.py index 9f9ef0e..231ed4d 100755 --- a/printNumbers/printNumbers.py +++ b/printNumbers/printNumbers.py @@ -27,12 +27,13 @@ """ Usage: printNumbers.py -h --help - printNumbers.py [--fibonacci|--factorial] + printNumbers.py [--fibonacci|--factorial|--square] Options: -h --help Print usage. --fibonacci Print the fibonacci sequence. --factorial Print the factorial. + --square Print the square of the operand. """ from docopt import docopt @@ -45,6 +46,7 @@ # functionTable = { CONST_FUNC_CODE_FIBONACCI : FibonacciSequence, CONST_FUNC_CODE_FACTORIAL : Factorial, + CONST_FUNC_CODE_SQUARE : Square, } # @@ -67,3 +69,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_SQUARE: + print(str(params.operand) + ' squared is ', str(result))