Alexander Blinne <n...@blinne.net> writes:
>> def gen_s():
>>   s = [1]
>>   m = skipdups(heapq.merge(*[(lambda j: (k*j for k in s))(n) for n in 
>> [2,3,5]]))
>>   yield s[0]
>>   while True:
>>       k = m.next()
>>       s.append(k)
>>       yield k

Nice.  I wouldn't have been sure that "for k in s" worked properly when
s was changing like that.

There is a space complexity problem compared to the Scheme or Haskell
version: all the s[i]'s are saved in the s array, instead of being
discarded once they are yielded.  That means generating n elements needs
O(n) space instead of O(n**0.7) or something like that.  I guess you can
get around it with collections.deque instead of a list.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to