Fixing some terms number issue, and improving result for first terms (0 and 1)

This commit is contained in:
Mehdi ALAOUI 2017-04-11 23:10:52 +01:00
parent 08cbd113a4
commit 9a756d1940

View File

@ -6,25 +6,24 @@ This is a pure Python implementation of Dynamic Programming solution to the fibo
class Fibonacci: class Fibonacci:
def __init__(self, N=None): def __init__(self, N=None):
self.fib_array = []
if N: if N:
N = int(N) N = int(N)
self.fib_array = [0] * (N + 1) self.fib_array.append(0)
self.fib_array[0] = 0 self.fib_array.append(1)
self.fib_array[1] = 1
for i in range(2, N + 1): for i in range(2, N + 1):
self.fib_array[i] = self.fib_array[ self.fib_array.append(self.fib_array[i - 1] + self.fib_array[i - 2])
i - 1] + self.fib_array[i - 2] elif N==0:
else: self.fib_array.append(0)
self.fib_array = [None] * (N + 1)
def get(self, sequence_no=None): def get(self, sequence_no=None):
if sequence_no: if sequence_no!=None:
if sequence_no < len(self.fib_array): if sequence_no < len(self.fib_array):
return print(self.fib_array[:sequence_no]) return print(self.fib_array[:sequence_no+1])
else: else:
print("Out of bound.") print("Out of bound.")
else: else:
print("Please specify the a value") print("Please specify a value")
if __name__ == '__main__': if __name__ == '__main__':