I want to write a fibonacci
function that behaves as a coroutine
in python.
Here is basically what I want to accomplish:
def fibonacci(limit: int) -> int:
...
fib = fibonacci(limit=100_000_000)
next(fib)
fib.send(10) # -> 55
next(fib) # -> 89
fib.send(15) # -> 610
...
I tried to write some logic based on code snipped below, but unfortunately it's not what I'm looking for:
def fibonacci(limit: int) -> int:
a, b = 0, 1
while limit:
c: int = (yield b)
if c is not None:
a, b = b, a + b + c
else:
a, b = b, a + b
limit -= 1
Could anyone please help me to figure out the right logic for python fibonacci coroutine, I'm kind of confused on how to make it properly, thanks in advance!