f | import itertools | f | import itertools |
| | | |
| def seesaw(sequence): | | def seesaw(sequence): |
t | even, odd = itertools.tee(sequence) | t | even_iter, odd_iter = itertools.tee(sequence) |
| even = filter(lambda x: x % 2 == 0, even) | | even_iter = filter(lambda x: x % 2 == 0, even_iter) |
| odd = filter(lambda x: x % 2 == 1, odd) | | odd_iter = filter(lambda x: x % 2 != 0, odd_iter) |
| for i1, i2 in itertools.zip_longest(even, odd): | | for even, odd in itertools.zip_longest(even_iter, odd_iter): |
| if i1 != None: | | if even is not None: |
| | | yield even |
| | | if odd is not None: |
| yield i1 | | yield odd |
| if i2 != None: | | |
| yield i2 | | |