Problem 3: Scale (100pts)
Implement the generator function scale(it, multiplier)
, which yields elements of the given iterable it
, scaled by multiplier
.
As an extra challenge, try writing this function using a yield from
statement!
def scale(it, multiplier):
"""Yield elements of the iterable it scaled by a number multiplier.
>>> m = scale(iter([1, 5, 2]), 5)
>>> type(m)
<class 'generator'>
>>> list(m)
[5, 25, 10]
>>> # generators allow us to represent infinite sequences!!!
>>> def naturals():
... i = 0
... while True:
... yield i
... i += 1
>>> m = scale(naturals(), 2)
>>> [next(m) for _ in range(5)]
[0, 2, 4, 6, 8]
"""
"*** YOUR CODE HERE ***"