fork download
  1. from itertools import islice
  2.  
  3. def scale(s, m):
  4. return (x*m for x in s)
  5.  
  6. def merge(s1, s2):
  7. x1, x2 = next(s1), next(s2)
  8. if x1 < x2:
  9. x = x1
  10. it = iter(merge(s1, s2))
  11. elif x1 > x2:
  12. x = x2
  13. it = iter(merge(s1, s2))
  14. else:
  15. x = x1
  16. it = iter(merge(s1, s2))
  17. yield x
  18. while True: yield next(it)
  19.  
  20. def integers():
  21. n = 0
  22. while True:
  23. n += 1
  24. yield n
  25.  
  26. m2 = scale(integers(), 2)
  27. m3 = scale(integers(), 3)
  28. m5 = scale(integers(), 5)
  29.  
  30. m23 = merge(m2, m3)
  31.  
  32. hamming_numbers = merge(m23, m5)
  33.  
  34. print( list( islice(m23,0,10)))
  35.  
  36. print( list( islice(m2,0,10)))
  37. print( list( islice(m3,0,10)))
  38. print( list( islice(m3,0,10)))
  39. print( list( islice(m5,0,10)))
  40. print( list( islice(m5,0,10)))
  41.  
  42.  
  43. print( list( islice(hamming_numbers,0,10)))
  44. print( list( islice(hamming_numbers,0,10)))
  45.  
Success #stdin #stdout 0.06s 9568KB
stdin
Standard input is empty
stdout
[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]