-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFibbonacciSequence.py
More file actions
76 lines (61 loc) · 2.14 KB
/
FibbonacciSequence.py
File metadata and controls
76 lines (61 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
File: FibbonacciSequence.py
Authors:
- Amey Thakur (https://github.com/Amey-Thakur)
- Mega Satish (https://github.com/msatmod)
Repository: https://github.com/Amey-Thakur/PYTHON-SHORTS
Release Date: January 9, 2022
License: MIT License
Description:
A memory-efficient generator for the Fibonacci sequence. This implementation
leverages the stateful nature of Python generators to produce sequences of
arbitrary length with O(1) space complexity.
Mathematical Logic:
The Fibonacci sequence F_n is defined by the recurrence relation:
F_n = F_{n-1} + F_{n-2}
with seed values F_0 = 0 and F_1 = 1.
"""
from typing import Generator, List
def fibonacci_generator() -> Generator[int, None, None]:
"""
Stateful generator for the infinite Fibonacci sequence.
Yields:
int: The next number in the Fibonacci sequence.
"""
a, b = 0, 1
while True:
yield a
a, b = b, a + b
def generate_fibonacci_sequence(limit: int) -> List[int]:
"""
Produces a finite slice of the Fibonacci sequence.
Args:
limit (int): The number of terms to generate.
Returns:
List[int]: A list containing the first 'limit' terms of the sequence.
Raises:
ValueError: If the limit is negative.
"""
if limit < 0:
raise ValueError("Sequence limit must be non-negative.")
gen = fibonacci_generator()
return [next(gen) for _ in range(limit)]
def run_fibonacci_demo():
"""Execution demo with standard and boundary test vectors."""
print("--- Python Shorts: Linear Recurrence Relations (Fibonacci) ---")
test_limits = [0, 1, 5, 10, 15]
for limit in test_limits:
print(f"\n[Sequence]: First {limit} terms")
try:
results = generate_fibonacci_sequence(limit)
print(f"[Output]: {results}")
except ValueError as e:
print(f"[Error]: {e}")
# Robustness Check
print("\n[Robustness Check]: Attempting negative limit (-3)")
try:
generate_fibonacci_sequence(-3)
except ValueError as e:
print(f"[Captured Error]: {e}")
if __name__ == "__main__":
run_fibonacci_demo()