From 4c470ffe55aa0b2af2bb462fe3e45388ece6955f Mon Sep 17 00:00:00 2001 From: KPAhola Date: Fri, 25 Sep 2020 13:25:21 +0300 Subject: [PATCH 1/9] added the required function --- mathtools.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mathtools.py b/mathtools.py index 58081fa..7658003 100644 --- a/mathtools.py +++ b/mathtools.py @@ -10,3 +10,10 @@ def isPrime(n): if n % i == 0: return False return True + +def factorial(n): + '''Returns the factorial of a number''' + if n == 0: + return 1 + else: + return n * factorial(n-1) From eb66e0e43ab64d9547b0002ba05b52f80b4d12ac Mon Sep 17 00:00:00 2001 From: KPAhola Date: Fri, 25 Sep 2020 13:33:07 +0300 Subject: [PATCH 2/9] removed test.py --- test.py | 62 --------------------------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 test.py diff --git a/test.py b/test.py deleted file mode 100644 index ff90060..0000000 --- a/test.py +++ /dev/null @@ -1,62 +0,0 @@ -import mathtools - -def test_isPrime(): - fails = 0 - print "Testing isPrime" - primes = [2, 3, 5, 7, 983, 991, 997] - not_primes = [4, 6, 8, 9, 993, 994, 995, 999] - for prime in primes: - if mathtools.isPrime(prime): - print '+ ', # Pass the test - else: - fails += 1 - print '- ', - for not_prime in not_primes: - if mathtools.isPrime(not_prime): - fails += 1 - print '- ', - else: - print '+ ', # Pass the test - - if not fails: - print "TEST OK" - else: - print ("FOUND ", fails, " ERRORS") - - -def test_factorial(): - print "Testing factorial " - fails = 0 - factorials = [(5, 120), (10, 3628800), (7, 5040), (12, 479001600)] - for f in factorials: - if mathtools.factorial(f[0]) == f[1]: - print '+ ', # Pass the test - else: - fails += 1 - print '- ', - if not fails: - print "TEST OK" - else: - print "FOUND ", fails, " ERRORS" - -def test_fib(): - print "Testing fib " - fails = 0 - fib = [(1, 1), (0, 0), (3, 2), (4, 3), (6, 8), (7, 13), (20, 6765)] - for f in fib: - if mathtools.fib(f[0]) == f[1]: - print '+ ', # Pass the test - else: - fails += 1 - print '- ', - if not fails: - print "TEST OK" - else: - print "FOUND ", fails, " ERRORS" - -functions = ('isPrime', 'factorial', 'fib') -for f in functions: - if hasattr(mathtools, f): - globals()['test_'+f]() - else: - print "*", f, "IS NOT DEFINED" \ No newline at end of file From e5e547552d78234d6d852062663b7896ee294f0b Mon Sep 17 00:00:00 2001 From: KPAhola Date: Fri, 25 Sep 2020 14:03:13 +0300 Subject: [PATCH 3/9] Added the required piece of code --- mathtools.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mathtools.py b/mathtools.py index 7658003..cfdffd3 100644 --- a/mathtools.py +++ b/mathtools.py @@ -17,3 +17,12 @@ def factorial(n): return 1 else: return n * factorial(n-1) + +def fib(n): + ''' Calculates the n value of the fibonacci sequence''' + if n == 0: + return 0 + elif n == 1: + return 1 + else: + return fib(n-1)+fib(n-2) From c7594317ee77e73594243e6417b102028ec999e3 Mon Sep 17 00:00:00 2001 From: KPAhola Date: Fri, 25 Sep 2020 14:12:04 +0300 Subject: [PATCH 4/9] added the required code --- mathtools.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mathtools.py b/mathtools.py index 7658003..b8c00fc 100644 --- a/mathtools.py +++ b/mathtools.py @@ -17,3 +17,12 @@ def factorial(n): return 1 else: return n * factorial(n-1) + +def arithmetic(a, difference, n): + '''Calculates the sum of a arithmetic serie of n elements. + An arithmetic sequence is of the form: a, a+d, a+2d, a+3d,... + n is the number of elements in the sequence.''' + #Get the arithmetic sequence + sequence = [a+difference*x for x in range(n)] + #Calculates its sum + return sum(sequence) From 3012d57bf257b8886e4ce030cf19d2237b7ffb41 Mon Sep 17 00:00:00 2001 From: Jaakko Astikainen Date: Fri, 25 Sep 2020 14:27:52 +0300 Subject: [PATCH 5/9] Added a function called geometric --- mathtools.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/mathtools.py b/mathtools.py index 6bc53a6..b03259b 100644 --- a/mathtools.py +++ b/mathtools.py @@ -35,3 +35,11 @@ def fib(n): return 1 else: return fib(n-1)+fib(n-2) + + +def geometric(a, ratio, n): + '''Calculates the sum of a geometric serie of n elements. + A geometric sequence is of the form: a, a*r, a*r*r, a*r*r*r,... + n is the number of elements in the sequence.''' + #Use the sum formula: + return a*(1-ratio**n)/(1-ratio) From 3d8ceaf1996cd756f446ae1a4506d9ca46e1c006 Mon Sep 17 00:00:00 2001 From: KPAhola Date: Fri, 25 Sep 2020 14:29:59 +0300 Subject: [PATCH 6/9] added a new function --- mathtools.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mathtools.py b/mathtools.py index 6bc53a6..dd57940 100644 --- a/mathtools.py +++ b/mathtools.py @@ -35,3 +35,12 @@ def fib(n): return 1 else: return fib(n-1)+fib(n-2) + +def geometric(a, ratio, n): + '''Calculates the sum of a geometric serie of n elements. + A geometric sequence is of the form: a, a*r, a*r*r, a*r*r*r,... + n is the number of elements in the sequence.''' + #Get the geometric sequence + sequence = [a*(ratio**x) for x in range(n)] + #Calculates its sum + return sum(sequence) From 4f6f54c3ae87a1d6107192a2abe1305844e45b33 Mon Sep 17 00:00:00 2001 From: KPAhola Date: Fri, 25 Sep 2020 14:32:51 +0300 Subject: [PATCH 7/9] removed a conflicting function --- mathtools.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/mathtools.py b/mathtools.py index dd57940..6bc53a6 100644 --- a/mathtools.py +++ b/mathtools.py @@ -35,12 +35,3 @@ def fib(n): return 1 else: return fib(n-1)+fib(n-2) - -def geometric(a, ratio, n): - '''Calculates the sum of a geometric serie of n elements. - A geometric sequence is of the form: a, a*r, a*r*r, a*r*r*r,... - n is the number of elements in the sequence.''' - #Get the geometric sequence - sequence = [a*(ratio**x) for x in range(n)] - #Calculates its sum - return sum(sequence) From 918d8ef207e72b8b2de09d3ac3857686f3ee251d Mon Sep 17 00:00:00 2001 From: KPAhola Date: Fri, 25 Sep 2020 14:35:29 +0300 Subject: [PATCH 8/9] fixed mathtools.py --- mathtools.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mathtools.py b/mathtools.py index 6bc53a6..4acb878 100644 --- a/mathtools.py +++ b/mathtools.py @@ -35,3 +35,5 @@ def fib(n): return 1 else: return fib(n-1)+fib(n-2) + + From c7d9df0cd6606c8464f5ec75fb7cf8882bf083b1 Mon Sep 17 00:00:00 2001 From: Jaakko Astikainen Date: Fri, 25 Sep 2020 14:45:59 +0300 Subject: [PATCH 9/9] Recovered the file test.py --- test.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 0000000..ff90060 --- /dev/null +++ b/test.py @@ -0,0 +1,62 @@ +import mathtools + +def test_isPrime(): + fails = 0 + print "Testing isPrime" + primes = [2, 3, 5, 7, 983, 991, 997] + not_primes = [4, 6, 8, 9, 993, 994, 995, 999] + for prime in primes: + if mathtools.isPrime(prime): + print '+ ', # Pass the test + else: + fails += 1 + print '- ', + for not_prime in not_primes: + if mathtools.isPrime(not_prime): + fails += 1 + print '- ', + else: + print '+ ', # Pass the test + + if not fails: + print "TEST OK" + else: + print ("FOUND ", fails, " ERRORS") + + +def test_factorial(): + print "Testing factorial " + fails = 0 + factorials = [(5, 120), (10, 3628800), (7, 5040), (12, 479001600)] + for f in factorials: + if mathtools.factorial(f[0]) == f[1]: + print '+ ', # Pass the test + else: + fails += 1 + print '- ', + if not fails: + print "TEST OK" + else: + print "FOUND ", fails, " ERRORS" + +def test_fib(): + print "Testing fib " + fails = 0 + fib = [(1, 1), (0, 0), (3, 2), (4, 3), (6, 8), (7, 13), (20, 6765)] + for f in fib: + if mathtools.fib(f[0]) == f[1]: + print '+ ', # Pass the test + else: + fails += 1 + print '- ', + if not fails: + print "TEST OK" + else: + print "FOUND ", fails, " ERRORS" + +functions = ('isPrime', 'factorial', 'fib') +for f in functions: + if hasattr(mathtools, f): + globals()['test_'+f]() + else: + print "*", f, "IS NOT DEFINED" \ No newline at end of file