Consider the following code snippet.
from typing import Iterable
def geometric_progression(
start: float, multiplier: float, num_elements: int
) -> Iterable[float]:
assert num_elements >= 0
if num_elements > 0:
yield start
yield from geometric_progression(
start * multiplier, multiplier, num_elements - 1
)
This function returns the first num_elements
of the geometric progression starting with start
and multipliying by multiplier
each time. It's easy to see that the last element will be passed through one yield-statement and num_elements-1
yield-from-statements. Does this function have O(num_elements)
time complexity, or does it have O(num_elements**2)
time complexity due to a "ladder" of nested yield-from-statements of depths 0, 1, 2, ..., num_elements-2
, num_elements-1
?
EDIT: I've come up with a simpler code snippet to demonstrate what I am asking.
from typing import Iterable, Any
def identity_with_nested_yield_from(depth: int, iterable: Iterable[Any]) -> Iterable[Any]:
assert depth >= 1
if depth == 1:
yield from iterable
else:
yield from identity_with_nested_yield_from(depth-1, iterable)
Is this function O(depth + length of iterable)
, or is it O(depth * length of iterable)
?