easy +8 pts

Fibonacci Generator

Implement a generator that yields Fibonacci numbers up to a given limit.

Write a generator function named `fibonacci(limit)` that yields Fibonacci numbers in increasing order, starting from 0, 1, 1, 2, 3, 5, ... and stops as soon as the next number would exceed `limit`. The generator should yield numbers that are <= `limit`. The function must be a generator, so calling it returns a generator object. You can assume `limit` is a non-negative integer. Do not use recursion; use a loop with yield.

Constraints

0 <= limit <= 10^6. Generator yields at most about 100 values.

Example

>>> list(fibonacci(0))
[0]
>>> list(fibonacci(1))
[0, 1, 1]
>>> list(fibonacci(10))
[0, 1, 1, 2, 3, 5, 8]
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Start with a = 0, b = 1.
Yield while a <= limit, then update to the next pair.
Be careful: for limit=1, both 1s are included because 1 <= 1.
The generator should be lazy; no need to collect a list internally.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.