class LazyList:
        def __init__(self, g):
            self.sofar = []
            self.fetch = g.next
   
        def __getitem__(self, i):
            sofar, fetch = self.sofar, self.fetch
            while i >= len(sofar):
                sofar.append(fetch())
            return sofar[i]

def times(n, g):
    for i in g:
        yield n * i

def merge(g, h):
     ng = g.next()
     nh = h.next()
     while 1:
         if ng < nh:
             yield ng
             ng = g.next()
         elif ng > nh:
             yield nh
             nh = h.next()
         else:
             yield ng
             ng = g.next()
             nh = h.next()

def m235():
    yield 1
    # Gack:  m235 below actually refers to a LazyList.
    me_times2 = times(2, m235)
    me_times3 = times(3, m235)
    me_times5 = times(5, m235)
    for i in merge(merge(me_times2,
                         me_times3),
                   me_times5):
        yield i

#Print as many of these as you like -- *this* implementation is memory-
#efficient.
if __name__ == '__main__':
    m235 = LazyList(m235())
    for i in range(5):
        print [m235[j] for j in range(15*i, 15*(i+1))]

Specialties:
IT/Science/Contracts/Religious translation/interpreting FIN-ENG-FIN
Python programming

1
Contributor
2
Replies
3
Views
6 Years
Discussion Span
Last Post by pyTony
0

Interestingly you also got it if you have OpenOffice.org 3 or pypy installed, even Qt 2010.05 and QtSDK seems to have it in pythongdb.

Edited by pyTony: n/a

0

My version of code (would like to change to __iter__ but was little complicated to change):

""" Generating Hamming sequence having only factors 2,3 or 5
    version with for in LazyList and using heapq.merge
    http://oeis.org/A051037
    m235 is indexed from zero Python style
    original code from: <Python installation directory>/Lib/test/test_generators.py

"""

import heapq

class LazyList:
    def __init__(self, g):
        self.sofar, self.fetch = [], g.next
   
    def __getitem__(self, index):
        for c in range(len(self.sofar), index + 1):
            self.sofar.append(self.fetch())
        return self.sofar[index]

def times(n, g):
    for i in g:
        yield n * i

def m235():
    yield 1
    previous = 1
    for val in heapq.merge(times(2, m235), times(3, m235), times(5, m235)):
        if val != previous:
            previous = val
            yield val
m235 = LazyList(m235())
            
if __name__ == '__main__':
    perline, max_length, linecount = 6, 10, 150
    format = "%%%ii" % max_length
    for count in range(linecount):
        print(', '.join(format % m235[index]
                        for index in range(perline * count, perline * (count + 1))) +
              (',' if count != linecount - 1 else '.'))

Worthy to check the test files otherwise as this same file has for example code for Knigth's Tour also.

Edited by pyTony: n/a

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.