Skip to content

Commit 74826f0

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent dda8aab commit 74826f0

1 file changed

Lines changed: 33 additions & 33 deletions

File tree

dynamic_programming/fibonacci.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,32 @@
1616
class Fibonacci:
1717
"""
1818
Dynamic Programming implementation of Fibonacci sequence generator.
19-
19+
2020
This class maintains a memoized sequence of Fibonacci numbers and can efficiently
2121
generate new numbers by building on previously calculated values.
22-
22+
2323
Attributes:
2424
sequence (list[int]): Memoized Fibonacci sequence starting with [0, 1]
2525
"""
26-
26+
2727
def __init__(self) -> None:
2828
"""Initialize the Fibonacci sequence with the first two numbers."""
2929
self.sequence = [0, 1]
30-
30+
3131
def get(self, index: int) -> list[int]:
3232
"""
3333
Get the first `index` Fibonacci numbers. If numbers don't exist in the sequence,
3434
calculate all missing numbers leading up to the required index.
35-
35+
3636
Args:
3737
index (int): Number of Fibonacci numbers to return (must be non-negative)
38-
38+
3939
Returns:
4040
list[int]: List containing the first `index` Fibonacci numbers
41-
41+
4242
Raises:
4343
ValueError: If index is negative
44-
44+
4545
Examples:
4646
>>> fib = Fibonacci()
4747
>>> fib.get(0)
@@ -58,7 +58,7 @@ def get(self, index: int) -> list[int]:
5858
[0]
5959
>>> fib.get(15) # Test extending existing sequence
6060
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
61-
61+
6262
# Test edge cases
6363
>>> fib_new = Fibonacci()
6464
>>> fib_new.get(0)
@@ -67,7 +67,7 @@ def get(self, index: int) -> list[int]:
6767
[0]
6868
>>> fib_new.get(2)
6969
[0, 1]
70-
70+
7171
# Test error handling
7272
>>> fib_error = Fibonacci()
7373
>>> fib_error.get(-1)
@@ -78,7 +78,7 @@ def get(self, index: int) -> list[int]:
7878
Traceback (most recent call last):
7979
...
8080
ValueError: Index must be non-negative, got -5
81-
81+
8282
# Test large numbers
8383
>>> fib_large = Fibonacci()
8484
>>> result = fib_large.get(20)
@@ -88,7 +88,7 @@ def get(self, index: int) -> list[int]:
8888
4181
8989
>>> result[0], result[1], result[2]
9090
(0, 1, 1)
91-
91+
9292
# Test sequence correctness
9393
>>> fib_test = Fibonacci()
9494
>>> seq = fib_test.get(8)
@@ -97,30 +97,30 @@ def get(self, index: int) -> list[int]:
9797
"""
9898
if index < 0:
9999
raise ValueError(f"Index must be non-negative, got {index}")
100-
100+
101101
if index == 0:
102102
return []
103-
103+
104104
# Calculate missing numbers if needed
105105
if (difference := index - len(self.sequence)) > 0:
106106
for _ in range(difference):
107107
self.sequence.append(self.sequence[-1] + self.sequence[-2])
108-
108+
109109
return self.sequence[:index]
110-
110+
111111
def get_nth(self, n: int) -> int:
112112
"""
113113
Get the nth Fibonacci number (0-indexed).
114-
114+
115115
Args:
116116
n (int): The index of the Fibonacci number to retrieve
117-
117+
118118
Returns:
119119
int: The nth Fibonacci number
120-
120+
121121
Raises:
122122
ValueError: If n is negative
123-
123+
124124
Examples:
125125
>>> fib = Fibonacci()
126126
>>> fib.get_nth(0)
@@ -140,19 +140,19 @@ def get_nth(self, n: int) -> int:
140140
"""
141141
if n < 0:
142142
raise ValueError(f"Index must be non-negative, got {n}")
143-
143+
144144
# Extend sequence if needed
145145
if n >= len(self.sequence):
146146
difference = n - len(self.sequence) + 1
147147
for _ in range(difference):
148148
self.sequence.append(self.sequence[-1] + self.sequence[-2])
149-
149+
150150
return self.sequence[n]
151-
151+
152152
def reset(self) -> None:
153153
"""
154154
Reset the sequence to its initial state.
155-
155+
156156
Examples:
157157
>>> fib = Fibonacci()
158158
>>> fib.get(10)
@@ -169,7 +169,7 @@ def reset(self) -> None:
169169
def main() -> None:
170170
"""
171171
Interactive CLI for generating Fibonacci numbers.
172-
172+
173173
Allows users to input indices and get the corresponding Fibonacci sequence.
174174
Supports commands: 'exit', 'quit', 'reset', 'help'
175175
"""
@@ -178,13 +178,13 @@ def main() -> None:
178178
"Enter the index of the Fibonacci number you want to calculate\n"
179179
"Commands: 'exit'/'quit' to exit, 'reset' to clear cache, 'help' for help\n"
180180
)
181-
181+
182182
fibonacci = Fibonacci()
183-
183+
184184
while True:
185185
try:
186186
prompt: str = input(">> ").strip().lower()
187-
187+
188188
if prompt in {"exit", "quit"}:
189189
print("Goodbye!")
190190
break
@@ -203,24 +203,24 @@ def main() -> None:
203203
continue
204204
elif prompt == "":
205205
continue
206-
206+
207207
try:
208208
index: int = int(prompt)
209209
if index < 0:
210210
print("Please enter a non-negative number.")
211211
continue
212-
212+
213213
result = fibonacci.get(index)
214214
if not result:
215215
print("[]")
216216
else:
217217
print(f"First {index} Fibonacci numbers: {result}")
218218
if index > 0:
219219
print(f"The {index}th Fibonacci number is: {result[-1]}")
220-
220+
221221
except ValueError:
222222
print("Invalid input. Enter a number or use 'help' for commands.")
223-
223+
224224
except KeyboardInterrupt:
225225
print("\nGoodbye!")
226226
break
@@ -230,4 +230,4 @@ def main() -> None:
230230

231231

232232
if __name__ == "__main__":
233-
main()
233+
main()

0 commit comments

Comments
 (0)