From e03426b8fd696b8794e21ef52c76a0a5140e1463 Mon Sep 17 00:00:00 2001 From: rafagarciac Date: Fri, 12 Oct 2018 00:41:57 +0200 Subject: [PATCH 1/3] Improve and Refactor the fibonnaciSeries.py (Recursion) --- Maths/fibonacciSeries.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/Maths/fibonacciSeries.py b/Maths/fibonacciSeries.py index 5badc6a82..a54fb89db 100644 --- a/Maths/fibonacciSeries.py +++ b/Maths/fibonacciSeries.py @@ -1,16 +1,18 @@ # Fibonacci Sequence Using Recursion def recur_fibo(n): - if n <= 1: - return n - else: - return(recur_fibo(n-1) + recur_fibo(n-2)) + return n if n <= 1 else (recur_fibo(n-1) + recur_fibo(n-2)) -limit = int(input("How many terms to include in fibonacci series: ")) +def isPositiveInteger(limit): + return limit >= 0 -if limit <= 0: - print("Please enter a positive integer: ") -else: - print(f"The first {limit} terms of the fibonacci series are as follows") - for i in range(limit): - print(recur_fibo(i)) +def main(): + limit = int(input("How many terms to include in fibonacci series: ")) + if isPositiveInteger: + print(f"The first {limit} terms of the fibonacci series are as follows:") + print([recur_fibo(n) for n in range(limit)]) + else: + print("Please enter a positive integer: ") + +if __name__ == '__main__': + main() From 1a4962a589c486e7e9fd989db333a83cf3a8e123 Mon Sep 17 00:00:00 2001 From: rafagarciac Date: Sat, 13 Oct 2018 22:17:57 +0200 Subject: [PATCH 2/3] Refactor gdc and rename two files GreaterCommonDivisor and FibonnaciSequenceRecursive --- ...cciSeries.py => FibonacciSequenceRecursion.py} | 0 Maths/GreaterCommonDivisor.py | 15 +++++++++++++++ Maths/gcd.py | 12 ------------ 3 files changed, 15 insertions(+), 12 deletions(-) rename Maths/{fibonacciSeries.py => FibonacciSequenceRecursion.py} (100%) create mode 100644 Maths/GreaterCommonDivisor.py delete mode 100644 Maths/gcd.py diff --git a/Maths/fibonacciSeries.py b/Maths/FibonacciSequenceRecursion.py similarity index 100% rename from Maths/fibonacciSeries.py rename to Maths/FibonacciSequenceRecursion.py diff --git a/Maths/GreaterCommonDivisor.py b/Maths/GreaterCommonDivisor.py new file mode 100644 index 000000000..15adaca1f --- /dev/null +++ b/Maths/GreaterCommonDivisor.py @@ -0,0 +1,15 @@ +# Greater Common Divisor - https://en.wikipedia.org/wiki/Greatest_common_divisor +def gcd(a, b): + return b if a == 0 else gcd(b % a, a) + +def main(): + try: + nums = input("Enter two Integers separated by comma (,): ").split(',') + num1 = int(nums[0]); num2 = int(nums[1]) + except (IndexError, UnboundLocalError, ValueError): + print("Wrong Input") + print(f"gcd({num1}, {num2}) = {gcd(num1, num2)}") + +if __name__ == '__main__': + main() + diff --git a/Maths/gcd.py b/Maths/gcd.py deleted file mode 100644 index 0f0ba7dce..000000000 --- a/Maths/gcd.py +++ /dev/null @@ -1,12 +0,0 @@ -def gcd(a, b): - if a == 0 : - return b - - return gcd(b%a, a) - -def main(): - print(gcd(3, 6)) - - -if __name__ == '__main__': - main() From a811a0e8499f291428b1d972d43f7b26e65a6d28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Garc=C3=ADa=20Cu=C3=A9llar?= Date: Sat, 13 Oct 2018 22:22:32 +0200 Subject: [PATCH 3/3] Update FibonacciSequenceRecursion.py --- Maths/FibonacciSequenceRecursion.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/FibonacciSequenceRecursion.py b/Maths/FibonacciSequenceRecursion.py index a54fb89db..a97bb8b3f 100644 --- a/Maths/FibonacciSequenceRecursion.py +++ b/Maths/FibonacciSequenceRecursion.py @@ -8,7 +8,7 @@ def isPositiveInteger(limit): def main(): limit = int(input("How many terms to include in fibonacci series: ")) - if isPositiveInteger: + if isPositiveInteger(limit): print(f"The first {limit} terms of the fibonacci series are as follows:") print([recur_fibo(n) for n in range(limit)]) else: