I want to change the behavior of the generator below so that it only yields even numbers. How can I do this?
I'm aware that there simpler, clever ways to do this. This is a contrived HR challenge, where the
The change_generator
function that I wrote does not yield the desired output. I can only change change_generator
.
I cannot change positive_integers_generator()
nor the for loop below.
Can I solve this with a decorator?
#can't change the body of this function
def positive_integers_generator():
n = 1
while True:
x = yield n
if x is not None:
n = x
else:
n += 1
# can only change this function
def change_generator(generator, n):
for i in generator:
if i%2 == 0:
yield(i)
# can't change this code either
# should print 1, 2, 4, 6, 8
g = positive_integers_generator()
for _ in range(5):
n = next(g)
print(n)
change_generator(g, n)
You can use the built in function
filter
Or a generator expression.
Or
itertools.count
from the standard library:But if you only can change the
change_generator
function, the "correct answer" to the challenge probably involves using generator.send()You don't need the parens on generator in your loop, and you don't seem to be printing the output of the right generator. Updated version that works for me:
In your very specific problem, if you can't change the
print(n)
part then you are pretty cornered because you can't change the instance of generatorg
that was created forpositive_integers_generator()
.In what may be a frowned upon answer, in this particular case you might want to update the
global g
to be reassigned to a newgenerator
after that: