f | import itertools | f | import itertools |
| | | |
| def seesaw(sequence): | | def seesaw(sequence): |
t | first_iter, second_iter = itertools.tee(sequence, 2) | t | even_iter, odd_iter = itertools.tee(sequence) |
| even_filtered = filter(lambda x: x % 2 == 0, first_iter) | | even_iter = filter(lambda x: x % 2 == 0, even_iter) |
| odd_filtered = filter(lambda x: x % 2 != 0, second_iter) | | odd_iter = filter(lambda x: x % 2 != 0, odd_iter) |
| for even, odd in itertools.zip_longest(even_filtered, odd_filtered): | | for even, odd in itertools.zip_longest(even_iter, odd_iter): |
| 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 |