Skip to content

Rewrote fibonacci.py (#5665) #5677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Oct 31, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 38 additions & 51 deletions dynamic_programming/fibonacci.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,48 @@


class Fibonacci:
def __init__(self, N=None):
self.fib_array = []
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:
self.fib_array.append(0)
print(self.fib_array)
def __init__(self) -> None:
self.sequence = [0, 1]

def get(self, sequence_no=None):
def get(self, index: int) -> list:
"""
>>> 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]
[]
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]
"""
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")
difference = index - (len(self.sequence) - 2)
if difference >= 1:
for _ in range(difference):
self.sequence.append(self.sequence[-1] + self.sequence[-2])
return self.sequence[: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(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
fib.get(i)
except NameError:
print("\nInvalid input, please try again.")
except NameError:
print("\n********* Invalid input, good bye!! ************\n")
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: str = input(">> ")
if prompt in {"exit", "quit"}:
break

import doctest
try:
index: int = int(prompt)
except ValueError:
print("Enter a number or 'exit'")
continue

doctest.testmod()
print(fibonacci.get(index))


if __name__ == "__main__":
main()