Light Mode
Dark Mode
yield does not work?

The <Best Clear Solution> offered to me is "yield" by Phil15 (Sep 13, 2022):

def duplicate_zeros(donuts):
    for o in donuts:
        yield o
        if not o:
            yield o

I really like the way, how a generator avoids building a local copy of the new list. It would be great to learn more about this topic. But when I copy-paste the code and hit RUN, all I get is:

Example:
<generator object duplicate_zeros at 0x7fbac8c74350>
AssertionError:     
 <module>, 16

It seems that wrapping the function in a list(..) would make the example work:

print("Example:")
print(list(duplicate_zeros([1, 0, 2, 3, 0, 4, 5, 0])))

But how did the solution passed the tests?

Created: Sept. 14, 2022, 5:31 p.m.
Updated: Sept. 14, 2022, 7:39 p.m.
0
25
User avatar
mu_py