Quiz of the week: Where in your system you have this code?

TrustyTony 0 Tallied Votes 225 Views Share

Next quiz is posted as code snippet. It is quite interesting code. What it does? And where in your system you already have this code? (in little different format) (If you have not follow tip in page http://python.org/download/)

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))]
TrustyTony 888 ex-Moderator Team Colleague Featured Poster

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.

TrustyTony 888 ex-Moderator Team Colleague Featured Poster

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.