which one do you prefer? python with C# or java?

Paul Rubin no.email at nospam.invalid
Fri Jun 15 03:00:16 EDT 2012


Alexander Blinne <news at 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.



More information about the Python-list mailing list