Write a Python generator function that yields the first `n` Fibonacci numbers. Generators are memory-efficient because they yield values one at a time on demand using the `yield` keyword, rather than building a full list in memory.
Examples
Example 1 Input:n = 5
Output:List of yielded values: [0, 1, 1, 2, 3]
Example 2 Input:n = 2
Output:List of yielded values: [0, 1]
Constraints
▪0 <= n <= 50
Hints
Hint 1Show
Use the `yield` keyword inside a loop to output a value and pause execution.
Hint 2Show
Initialize `a = 0` and `b = 1`, and update them on each step as `a, b = b, a + b`.
Starter Code
Solution.py
1def fib_generator(n: int):
2 # Your generator code here
3 pass
Solution
Solve the problem first before reviewing the solution!