mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-18 16:27:02 +00:00
* Removed doctest call * Removed 0 and 1 append to `fib_array` * Moved fibonacci sequence logic into `calculate` * Refactored `get` to generate missing numbers * Renamed `fib_array` to `sequence` * Renamed `number` to `index` * Refactored `get` to only return sequence to `index` * Moved main block into function * Added documentation to `get` * Added missing type hints * Fixed doctest error in `get` docstring * Moved calculate logic into get * Reformatted with black * Fixed wrong generation range
This commit is contained in:
parent
a64c9f1e7c
commit
868c2fa0a8
|
@ -5,61 +5,48 @@ sequence problem.
|
||||||
|
|
||||||
|
|
||||||
class Fibonacci:
|
class Fibonacci:
|
||||||
def __init__(self, N=None):
|
def __init__(self) -> None:
|
||||||
self.fib_array = []
|
self.sequence = [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:
|
|
||||||
self.fib_array.append(0)
|
|
||||||
print(self.fib_array)
|
|
||||||
|
|
||||||
def get(self, sequence_no=None):
|
def get(self, index: int) -> list:
|
||||||
"""
|
"""
|
||||||
>>> Fibonacci(5).get(3)
|
Get the Fibonacci number of `index`. If the number does not exist,
|
||||||
[0, 1, 1, 2, 3, 5]
|
calculate all missing numbers leading up to the number of `index`.
|
||||||
[0, 1, 1, 2]
|
|
||||||
>>> Fibonacci(5).get(6)
|
>>> Fibonacci().get(10)
|
||||||
[0, 1, 1, 2, 3, 5]
|
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
|
||||||
Out of bound.
|
>>> Fibonacci().get(5)
|
||||||
>>> Fibonacci(5).get(-1)
|
[0, 1, 1, 2, 3]
|
||||||
[0, 1, 1, 2, 3, 5]
|
|
||||||
[]
|
|
||||||
"""
|
"""
|
||||||
if sequence_no is not None:
|
difference = index - (len(self.sequence) - 2)
|
||||||
if sequence_no < len(self.fib_array):
|
if difference >= 1:
|
||||||
return print(self.fib_array[: sequence_no + 1])
|
for _ in range(difference):
|
||||||
else:
|
self.sequence.append(self.sequence[-1] + self.sequence[-2])
|
||||||
print("Out of bound.")
|
return self.sequence[:index]
|
||||||
else:
|
|
||||||
print("Please specify a value")
|
|
||||||
|
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
|
||||||
|
|
||||||
|
try:
|
||||||
|
index: int = int(prompt)
|
||||||
|
except ValueError:
|
||||||
|
print("Enter a number or 'exit'")
|
||||||
|
continue
|
||||||
|
|
||||||
|
print(fibonacci.get(index))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print("\n********* Fibonacci Series Using Dynamic Programming ************\n")
|
main()
|
||||||
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")
|
|
||||||
|
|
||||||
import doctest
|
|
||||||
|
|
||||||
doctest.testmod()
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user