Skip to content

Commit e268bf1

Browse files
Merge pull request TheAlgorithms#75 from alaouimehdi1995/master
Fixing some terms number issue, and improving result for first terms (0 and 1)
2 parents 7814b28 + ce8b025 commit e268bf1

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

dynamic_programming/fibonacci.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,24 @@
66
class Fibonacci:
77

88
def __init__(self, N=None):
9+
self.fib_array = []
910
if N:
1011
N = int(N)
11-
self.fib_array = [0] * (N + 1)
12-
self.fib_array[0] = 0
13-
self.fib_array[1] = 1
12+
self.fib_array.append(0)
13+
self.fib_array.append(1)
1414
for i in range(2, N + 1):
15-
self.fib_array[i] = self.fib_array[
16-
i - 1] + self.fib_array[i - 2]
17-
else:
18-
self.fib_array = [None] * (N + 1)
15+
self.fib_array.append(self.fib_array[i - 1] + self.fib_array[i - 2])
16+
elif N == 0:
17+
self.fib_array.append(0)
1918

2019
def get(self, sequence_no=None):
21-
if sequence_no:
20+
if sequence_no != None:
2221
if sequence_no < len(self.fib_array):
23-
return print(self.fib_array[:sequence_no])
22+
return print(self.fib_array[:sequence_no + 1])
2423
else:
2524
print("Out of bound.")
2625
else:
27-
print("Please specify the a value")
26+
print("Please specify a value")
2827

2928

3029
if __name__ == '__main__':

0 commit comments

Comments
 (0)