From 9a756d1940b1368d7db94b2cce123de5b369bc66 Mon Sep 17 00:00:00 2001 From: Mehdi ALAOUI Date: Tue, 11 Apr 2017 23:10:52 +0100 Subject: [PATCH 1/2] Fixing some terms number issue, and improving result for first terms (0 and 1) --- dynamic_programming/fibonacci.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index 5733f693a..cc4174e14 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -6,25 +6,24 @@ This is a pure Python implementation of Dynamic Programming solution to the fibo class Fibonacci: def __init__(self, N=None): + self.fib_array = [] if N: N = int(N) - self.fib_array = [0] * (N + 1) - self.fib_array[0] = 0 - self.fib_array[1] = 1 + self.fib_array.append(0) + self.fib_array.append(1) for i in range(2, N + 1): - self.fib_array[i] = self.fib_array[ - i - 1] + self.fib_array[i - 2] - else: - self.fib_array = [None] * (N + 1) + self.fib_array.append(self.fib_array[i - 1] + self.fib_array[i - 2]) + elif N==0: + self.fib_array.append(0) def get(self, sequence_no=None): - if sequence_no: + if sequence_no!=None: if sequence_no < len(self.fib_array): - return print(self.fib_array[:sequence_no]) + return print(self.fib_array[:sequence_no+1]) else: print("Out of bound.") else: - print("Please specify the a value") + print("Please specify a value") if __name__ == '__main__': From ce8b025320a7da89f3422a178d8f5518e9172e0b Mon Sep 17 00:00:00 2001 From: Mehdi ALAOUI Date: Wed, 12 Apr 2017 14:45:56 +0100 Subject: [PATCH 2/2] Adding spaces between operators --- dynamic_programming/fibonacci.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dynamic_programming/fibonacci.py b/dynamic_programming/fibonacci.py index cc4174e14..692cb756a 100644 --- a/dynamic_programming/fibonacci.py +++ b/dynamic_programming/fibonacci.py @@ -13,13 +13,13 @@ class Fibonacci: 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: + elif N == 0: self.fib_array.append(0) def get(self, sequence_no=None): - if sequence_no!=None: + if sequence_no != None: if sequence_no < len(self.fib_array): - return print(self.fib_array[:sequence_no+1]) + return print(self.fib_array[:sequence_no + 1]) else: print("Out of bound.") else: