nums = [n async for n in fibonacci(10)] # [0, 1, 1, 2, 3, 5, 8]
File: ~/fibonacci_async.py
from typing import AsyncGenerator
async def fibonacci(max_value: int) -> AsyncGenerator[int, None]:
"""Async generator that yields Fibonacci numbers up to a configurable max value.
Args:
max_value: The maximum value to yield (inclusive). Only Fibonacci
numbers <= max_value are yielded.
Yields:
Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, ...
Example:
async for num in fibonacci(10):
print(num)
# Output: 0, 1, 1, 2, 3, 5, 8
"""
a, b = 0, 1
while a <= max_value:
yield a
a, b = b, a + b
Usage:
import asyncio
from fibonacci_async import fibonacci
async def main():
async for num in fibonacci(20):
print(num) # 0, 1, 1, 2, 3, 5, 8, 13
# Or collect into a list
nums = [n async for n in fibonacci(10)] # [0, 1, 1, 2, 3, 5, 8]
asyncio.run(main())
How it works:
- The function is declared with async def and the return type is AsyncGenerator[int, None], making it an async generator.
- It accepts a single parameter max_value — the inclusive upper bound.
- It maintains two variables a and b starting at 0 and 1.
- On each iteration, it yields a (the current Fibonacci number), then advances both variables.
- The loop stops when a exceeds max_value.
Test results with 7 edge cases: | Test | Input | Expected Output | Result | |------|-------|----------------|--------| | Basic | `fibonacci(10)` | `[0, 1, 1, 2, 3, 5, 8]` | ✅ | | Zero | `fibonacci(0)` | `[0]` | ✅ | | One | `fibonacci(1)` | `[0, 1, 1]` | ✅ | | Negative max | `fibonacci(-1)` | `[]` | ✅ | | Larger value | `fibonacci(100)` | `[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]` | ✅ | | Async iteration | `fibonacci(5)` via `async for` | `[0, 1, 1, 2, 3, 5]` | ✅ | | Large cap | `fibonacci(1000)` | Last=987, count=17 | ✅ | **Edge cases covered:** 1. **`max_value = 0`** — yields only `[0]` (the first Fibonacci number equals the bound) 2. **`max_value = 1`** — yields `[0, 1, 1]` (duplicate 1s, since both `a=1` and the next `a=1` are ≤ 1) 3. **Negative max** — yields `[]` (empty, since 0 > -1) 4. **Large bounds** — works correctly with arbitrary integer bounds, no overflow 5. **`async for` protocol** — properly supports async iteration ---
{"model": "gpt-4", "problem_class": "python-async-generator", "result": "passed", "tests": 7}File: ~/fibonacci_async.py
from typing import AsyncGenerator
async def fibonacci(max_value: int) -> AsyncGenerator[int, None]:
"""Async generator that yields Fibonacci numbers up to a configurable max value.
Args:
max_value: The maximum value to yield (inclusive). Only Fibonacci
numbers <= max_value are yielded.
Yields:
Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, ...
Example:
async for num in fibonacci(10):
print(num)
# Output: 0, 1, 1, 2, 3, 5, 8
"""
a, b = 0, 1
while a <= max_value:
yield a
a, b = b, a + b
Usage:
import asyncio
from fibonacci_async import fibonacci
async def main():
async for num in fibonacci(20):
print(num) # 0, 1, 1, 2, 3, 5, 8, 13
# Or collect into a list
nums = [n async for n in fibonacci(10)] # [0, 1, 1, 2, 3, 5, 8]
asyncio.run(main())
How it works:
- The function is declared with async def and the return type is AsyncGenerator[int, None], making it an async generator.
- It accepts a single parameter max_value — the inclusive upper bound.
- It maintains two variables a and b starting at 0 and 1.
- On each iteration, it yields a (the current Fibonacci number), then advances both variables.
- The loop stops when a exceeds max_value.
Test results with 7 edge cases: | Test | Input | Expected Output | Result | |------|-------|----------------|--------| | Basic | `fibonacci(10)` | `[0, 1, 1, 2, 3, 5, 8]` | ✅ | | Zero | `fibonacci(0)` | `[0]` | ✅ | | One | `fibonacci(1)` | `[0, 1, 1]` | ✅ | | Negative max | `fibonacci(-1)` | `[]` | ✅ | | Larger value | `fibonacci(100)` | `[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]` | ✅ | | Async iteration | `fibonacci(5)` via `async for` | `[0, 1, 1, 2, 3, 5]` | ✅ | | Large cap | `fibonacci(1000)` | Last=987, count=17 | ✅ | **Edge cases covered:** 1. **`max_value = 0`** — yields only `[0]` (the first Fibonacci number equals the bound) 2. **`max_value = 1`** — yields `[0, 1, 1]` (duplicate 1s, since both `a=1` and the next `a=1` are ≤ 1) 3. **Negative max** — yields `[]` (empty, since 0 > -1) 4. **Large bounds** — works correctly with arbitrary integer bounds, no overflow 5. **`async for` protocol** — properly supports async iteration ---
{"model": "gpt-4", "problem_class": "python-async-generator", "result": "passed", "tests": 7}