From 1aab152ea4900b0c81e026f91626b6c1023b7600 Mon Sep 17 00:00:00 2001 From: citharus Date: Fri, 29 Oct 2021 18:50:10 +0200 Subject: [PATCH 01/14] Removed doctest call --- dynamic_programming/fibonacci.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index cab1358ddea1..ce8025588634 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -59,7 +59,3 @@ def get(self, sequence_no=None): print("\nInvalid input, please try again.") except NameError: print("\n********* Invalid input, good bye!! ************\n") - - import doctest - - doctest.testmod() From adad44bf4fcb06280701c307cc11d2270fd624d5 Mon Sep 17 00:00:00 2001 From: citharus Date: Fri, 29 Oct 2021 19:01:27 +0200 Subject: [PATCH 02/14] Removed 0 and 1 append to `fib_array` --- dynamic_programming/fibonacci.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index ce8025588634..60f43a3b42a1 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -6,11 +6,9 @@ class Fibonacci: def __init__(self, N=None): - self.fib_array = [] + self.fib_array = [0, 1] if N: N = int(N) - self.fib_array.append(0) - self.fib_array.append(1) for i in range(2, N + 1): self.fib_array.append(self.fib_array[i - 1] + self.fib_array[i - 2]) elif N == 0: From 02e22a2efc7ede0e46d51666c2211f3b48cdc4aa Mon Sep 17 00:00:00 2001 From: citharus Date: Fri, 29 Oct 2021 19:09:04 +0200 Subject: [PATCH 03/14] Moved fibonacci sequence logic into `calculate` --- dynamic_programming/fibonacci.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index 60f43a3b42a1..62de9fa0d223 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -5,15 +5,12 @@ class Fibonacci: - def __init__(self, N=None): + def __init__(self): self.fib_array = [0, 1] - if N: - N = int(N) - for i in range(2, N + 1): - self.fib_array.append(self.fib_array[i - 1] + self.fib_array[i - 2]) - elif N == 0: - self.fib_array.append(0) - print(self.fib_array) + + def calculate(self, number: int) -> None: + for _ in range(number): + self.fib_array.append(self.fib_array[-1] + self.fib_array[-2]) def get(self, sequence_no=None): """ From e5e3ff2f7d56870a37e59abd615eb64a84a4e0cc Mon Sep 17 00:00:00 2001 From: citharus Date: Fri, 29 Oct 2021 19:20:21 +0200 Subject: [PATCH 04/14] Refactored `get` to generate missing numbers --- dynamic_programming/fibonacci.py | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index 62de9fa0d223..cbff0f31364b 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -12,25 +12,11 @@ def calculate(self, number: int) -> None: for _ in range(number): self.fib_array.append(self.fib_array[-1] + self.fib_array[-2]) - def get(self, sequence_no=None): - """ - >>> Fibonacci(5).get(3) - [0, 1, 1, 2, 3, 5] - [0, 1, 1, 2] - >>> Fibonacci(5).get(6) - [0, 1, 1, 2, 3, 5] - Out of bound. - >>> Fibonacci(5).get(-1) - [0, 1, 1, 2, 3, 5] - [] - """ - if sequence_no is not None: - if sequence_no < len(self.fib_array): - return print(self.fib_array[: sequence_no + 1]) - else: - print("Out of bound.") - else: - print("Please specify a value") + def get(self, number: int) -> list: + difference = number - (len(self.fib_array) - 2) + if difference >= 1: + self.calculate(difference) + return self.fib_array if __name__ == "__main__": @@ -38,7 +24,8 @@ def get(self, sequence_no=None): print("\n Enter the upper limit for the fibonacci sequence: ", end="") try: N = int(input().strip()) - fib = Fibonacci(N) + fib = Fibonacci() + fib.calculate(N) print( "\n********* Enter different values to get the corresponding fibonacci " "sequence, enter any negative number to exit. ************\n" @@ -49,7 +36,7 @@ def get(self, sequence_no=None): if i < 0: print("\n********* Good Bye!! ************\n") break - fib.get(i) + print(fib.get(i)) except NameError: print("\nInvalid input, please try again.") except NameError: From a2c0020ee893868c064a75451ef064fed7182d7c Mon Sep 17 00:00:00 2001 From: citharus Date: Fri, 29 Oct 2021 19:20:55 +0200 Subject: [PATCH 05/14] Renamed `fib_array` to `sequence` --- dynamic_programming/fibonacci.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index cbff0f31364b..96e15c293f23 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -6,17 +6,17 @@ class Fibonacci: def __init__(self): - self.fib_array = [0, 1] + self.sequence = [0, 1] def calculate(self, number: int) -> None: for _ in range(number): - self.fib_array.append(self.fib_array[-1] + self.fib_array[-2]) + self.sequence.append(self.sequence[-1] + self.sequence[-2]) def get(self, number: int) -> list: - difference = number - (len(self.fib_array) - 2) + difference = number - (len(self.sequence) - 2) if difference >= 1: self.calculate(difference) - return self.fib_array + return self.sequence if __name__ == "__main__": From c4e59f29e3fd78fe53015695f36196f71067329d Mon Sep 17 00:00:00 2001 From: citharus Date: Fri, 29 Oct 2021 19:21:36 +0200 Subject: [PATCH 06/14] Renamed `number` to `index` --- dynamic_programming/fibonacci.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index 96e15c293f23..e0e06f92446d 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -8,12 +8,12 @@ class Fibonacci: def __init__(self): self.sequence = [0, 1] - def calculate(self, number: int) -> None: - for _ in range(number): + def calculate(self, index: int) -> None: + for _ in range(index): self.sequence.append(self.sequence[-1] + self.sequence[-2]) - def get(self, number: int) -> list: - difference = number - (len(self.sequence) - 2) + def get(self, index: int) -> list: + difference = index - (len(self.sequence) - 2) if difference >= 1: self.calculate(difference) return self.sequence From b2579d5e93a290990d35af8b84f0c35f92bbebc7 Mon Sep 17 00:00:00 2001 From: citharus Date: Fri, 29 Oct 2021 20:13:35 +0200 Subject: [PATCH 07/14] Refactored `get` to only return sequence to `index` --- dynamic_programming/fibonacci.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index e0e06f92446d..d82f915ea1e8 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -16,7 +16,7 @@ def get(self, index: int) -> list: difference = index - (len(self.sequence) - 2) if difference >= 1: self.calculate(difference) - return self.sequence + return self.sequence[:index] if __name__ == "__main__": From 3aebf0f457b64ae9203108af9b1c635bd90c035d Mon Sep 17 00:00:00 2001 From: citharus Date: Fri, 29 Oct 2021 20:15:24 +0200 Subject: [PATCH 08/14] Moved main block into function --- dynamic_programming/fibonacci.py | 45 +++++++++++++++++--------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index d82f915ea1e8..d3a166ca94e4 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -19,25 +19,28 @@ def get(self, index: int) -> list: return self.sequence[:index] +def main(): + print( + 'Fibonacci Series Using Dynamic Programming\n', + 'Enter the index of the Fibonacci number you want to calculate ', + 'in the prompt below. (To exit enter exit or Ctrl-C)\n', + sep="", + ) + fibonacci = Fibonacci() + + while True: + prompt = input('>> ') + if prompt in ['exit', 'quit']: + break + + try: + index = int(prompt) + except ValueError: + print('Enter a number or "exit"') + continue + + print(fibonacci.get(index)) + + if __name__ == "__main__": - print("\n********* Fibonacci Series Using Dynamic Programming ************\n") - print("\n Enter the upper limit for the fibonacci sequence: ", end="") - try: - N = int(input().strip()) - fib = Fibonacci() - fib.calculate(N) - print( - "\n********* Enter different values to get the corresponding fibonacci " - "sequence, enter any negative number to exit. ************\n" - ) - while True: - try: - i = int(input("Enter value: ").strip()) - if i < 0: - print("\n********* Good Bye!! ************\n") - break - print(fib.get(i)) - except NameError: - print("\nInvalid input, please try again.") - except NameError: - print("\n********* Invalid input, good bye!! ************\n") + main() From 38fddf8d9765a3e3deaf39a268888b25dcc47ba4 Mon Sep 17 00:00:00 2001 From: citharus Date: Fri, 29 Oct 2021 20:24:38 +0200 Subject: [PATCH 09/14] Added documentation to `get` --- dynamic_programming/fibonacci.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index d3a166ca94e4..18b9ded32cb5 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -13,6 +13,15 @@ def calculate(self, index: int) -> None: self.sequence.append(self.sequence[-1] + self.sequence[-2]) def get(self, index: int) -> list: + """ + Get the Fibonacci number of `index`. If the number does not exist, + calculate all missing numbers leading up to the number of `index`. + + >>>Fibonacci().get(10) + [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] + >>>Fibonacci().get(5) + [0, 1, 1, 2, 3] + """ difference = index - (len(self.sequence) - 2) if difference >= 1: self.calculate(difference) From ed0adcbb0740d873dfbdd956d314834bd2bf9cb6 Mon Sep 17 00:00:00 2001 From: citharus Date: Fri, 29 Oct 2021 20:27:01 +0200 Subject: [PATCH 10/14] Added missing type hints --- dynamic_programming/fibonacci.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index 18b9ded32cb5..c069f8a27144 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -5,7 +5,7 @@ class Fibonacci: - def __init__(self): + def __init__(self) -> None: self.sequence = [0, 1] def calculate(self, index: int) -> None: @@ -38,12 +38,12 @@ def main(): fibonacci = Fibonacci() while True: - prompt = input('>> ') - if prompt in ['exit', 'quit']: + prompt: str = input('>> ') + if prompt in {'exit', 'quit'}: break try: - index = int(prompt) + index: int = int(prompt) except ValueError: print('Enter a number or "exit"') continue From ed2234c00818ec018007f026c3c2f4e5c391f4ee Mon Sep 17 00:00:00 2001 From: Maarten Date: Fri, 29 Oct 2021 20:40:45 +0200 Subject: [PATCH 11/14] Fixed doctest error in `get` docstring --- dynamic_programming/fibonacci.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index c069f8a27144..8d3ddb6c2d0b 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -17,9 +17,9 @@ def get(self, index: int) -> list: Get the Fibonacci number of `index`. If the number does not exist, calculate all missing numbers leading up to the number of `index`. - >>>Fibonacci().get(10) + >>> Fibonacci().get(10) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] - >>>Fibonacci().get(5) + >>> Fibonacci().get(5) [0, 1, 1, 2, 3] """ difference = index - (len(self.sequence) - 2) From 620d8792343d76548b5dc378221793e187072764 Mon Sep 17 00:00:00 2001 From: citharus Date: Sat, 30 Oct 2021 15:05:29 +0200 Subject: [PATCH 12/14] Moved calculate logic into get --- dynamic_programming/fibonacci.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index 8d3ddb6c2d0b..394f96b0ee58 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -8,10 +8,6 @@ class Fibonacci: def __init__(self) -> None: self.sequence = [0, 1] - def calculate(self, index: int) -> None: - for _ in range(index): - self.sequence.append(self.sequence[-1] + self.sequence[-2]) - def get(self, index: int) -> list: """ Get the Fibonacci number of `index`. If the number does not exist, @@ -24,7 +20,8 @@ def get(self, index: int) -> list: """ difference = index - (len(self.sequence) - 2) if difference >= 1: - self.calculate(difference) + for _ in range(index): + self.sequence.append(self.sequence[-1] + self.sequence[-2]) return self.sequence[:index] From 6b1bfec94e28ef9959e6ac53d6d05f208e3ece43 Mon Sep 17 00:00:00 2001 From: citharus Date: Sat, 30 Oct 2021 15:06:38 +0200 Subject: [PATCH 13/14] Reformatted with black --- dynamic_programming/fibonacci.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index 394f96b0ee58..742eb6b91e25 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -27,22 +27,22 @@ def get(self, index: int) -> list: def main(): print( - 'Fibonacci Series Using Dynamic Programming\n', - 'Enter the index of the Fibonacci number you want to calculate ', - 'in the prompt below. (To exit enter exit or Ctrl-C)\n', + "Fibonacci Series Using Dynamic Programming\n", + "Enter the index of the Fibonacci number you want to calculate ", + "in the prompt below. (To exit enter exit or Ctrl-C)\n", sep="", ) fibonacci = Fibonacci() while True: - prompt: str = input('>> ') - if prompt in {'exit', 'quit'}: + prompt: str = input(">> ") + if prompt in {"exit", "quit"}: break try: index: int = int(prompt) except ValueError: - print('Enter a number or "exit"') + print("Enter a number or 'exit'") continue print(fibonacci.get(index)) From 1d4b8dcfcc43ee1878e41c1f15e7e3071e29599a Mon Sep 17 00:00:00 2001 From: citharus Date: Sat, 30 Oct 2021 21:50:04 +0200 Subject: [PATCH 14/14] Fixed wrong generation range --- dynamic_programming/fibonacci.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index 742eb6b91e25..4abc60d4f3cc 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -20,7 +20,7 @@ def get(self, index: int) -> list: """ difference = index - (len(self.sequence) - 2) if difference >= 1: - for _ in range(index): + for _ in range(difference): self.sequence.append(self.sequence[-1] + self.sequence[-2]) return self.sequence[:index]