Infinite Generator
Completed
Write an infinite generator fibonacci(). Read n and print the first n Fibonacci numbers using itertools.islice.
Input
One integer.
Output
One line.
Example
Input
10
Output
0 1 1 2 3 5 8 13 21 34
Editor
Python
Hints
while True with yield inside never ends - the caller decides how many values to take.
islice(gen, n) takes the first n.
Solution explanation
Infinite generators are safe because values are only computed when requested.
Show solution
from itertools import islice
def fibonacci():
a, b = 0, 1
while True:
yield a
a, b = b, a + b
print(*islice(fibonacci(), int(input())))