Problem 4: Merge (100pts)
Write a generator function merge
that takes in two infinite generators a
and b
that are in increasing order without duplicates and returns a generator that has all the elements of both generators, in increasing order, without duplicates.
Note: You may assume that both
a
andb
contains unlimited elements. As a result, callingnext(a)
andnext(b)
will never throwStopIteration
exception.
def merge(a, b):
"""Merge two generators that are in increasing order and without duplicates.
Return a generator that has all elements of both generators in increasing
order and without duplicates.
>>> def sequence(start, step):
... while True:
... yield start
... start += step
>>> a = sequence(2, 3) # 2, 5, 8, 11, 14, ...
>>> b = sequence(3, 2) # 3, 5, 7, 9, 11, 13, 15, ...
>>> result = merge(a, b) # 2, 3, 5, 7, 8, 9, 11, 13, 14, 15
>>> [next(result) for _ in range(10)]
[2, 3, 5, 7, 8, 9, 11, 13, 14, 15]
"""
"*** YOUR CODE HERE ***"