f | import itertools | f | import itertools |
| | | |
| def seesaw(sequence): | | def seesaw(sequence): |
t | evens, odds = itertools.tee(sequence, 2) | t | seq1, seq2 = itertools.tee(sequence, 2) |
| evens = (x for x in evens if x % 2 == 0) | | evens = (x for x in seq1 if x % 2 == 0) |
| odds = (x for x in odds if x % 2 != 0) | | odds = (x for x in seq2 if x % 2 != 0) |
| for even, odd in itertools.zip_longest(evens, odds): | | for even, odd in itertools.zip_longest(evens, odds): |
| if even is not None: | | if even is not None: |
| yield even | | yield even |
| if odd is not None: | | if odd is not None: |
| yield odd | | yield odd |