from itertools import islice def scale(s, m): return (x*m for x in s) def merge(s1, s2): x1, x2 = next(s1), next(s2) if x1 < x2: x = x1 it = iter(merge(s1, s2)) elif x1 > x2: x = x2 it = iter(merge(s1, s2)) else: x = x1 it = iter(merge(s1, s2)) yield x while True: yield next(it) def integers(): n = 0 while True: n += 1 yield n m2 = scale(integers(), 2) m3 = scale(integers(), 3) m5 = scale(integers(), 5) m23 = merge(m2, m3) hamming_numbers = merge(m23, m5) print( list( islice(m23,0,10))) print( list( islice(m2,0,10))) print( list( islice(m3,0,10))) print( list( islice(m3,0,10))) print( list( islice(m5,0,10))) print( list( islice(m5,0,10))) print( list( islice(hamming_numbers,0,10))) print( list( islice(hamming_numbers,0,10)))
Standard input is empty
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20] [22, 24, 26, 28, 30, 32, 34, 36, 38, 40] [33, 36, 39, 42, 45, 48, 51, 54, 57, 60] [63, 66, 69, 72, 75, 78, 81, 84, 87, 90] [5, 10, 15, 20, 25, 30, 35, 40, 45, 50] [55, 60, 65, 70, 75, 80, 85, 90, 95, 100] [42, 44, 46, 48, 50, 52, 54, 56, 58, 60] [62, 64, 66, 68, 70, 72, 74, 76, 78, 80]