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. it1, it2 = iter(s1), iter(s2)
  8. x1, x2 = next(it1), next(it2)
  9. if x1 < x2:
  10. x = x1
  11. it = iter(merge(it1, s2))
  12. elif x1 > x2:
  13. x = x2
  14. it = iter(merge(s1, it2))
  15. else:
  16. x = x1
  17. it = iter(merge(it1, it2))
  18. yield x
  19. while True: yield next(it)
  20.  
  21. def integers():
  22. n = 0
  23. while True:
  24. n += 1
  25. yield n
  26.  
  27. m2 = scale(integers(), 2)
  28. m3 = scale(integers(), 3)
  29. m5 = scale(integers(), 5)
  30.  
  31. m23 = merge(m2, m3)
  32.  
  33. hamming_numbers = merge(m23, m5)
  34.  
  35. print( list( islice(m23,0,10)))
  36.  
  37. print( list( islice(m2,0,10)))
  38. print( list( islice(m3,0,10)))
  39. print( list( islice(m3,0,10)))
  40. print( list( islice(m5,0,10)))
  41. print( list( islice(m5,0,10)))
  42.  
  43.  
  44. print( list( islice(hamming_numbers,0,10)))
  45. print( list( islice(hamming_numbers,0,10)))
  46.  
Success #stdin #stdout 0.16s 10264KB
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]