473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pythonic Infinite Lists

391 Expert 256MB
Hi All

I came across a Perl implementation of Infinite Lists the other day, and was wondering about implementing it in python.

The specific example was to generate the first 3000 Hamming numbers, that is numbers of the form 2**i*3**j*5**k for i,j,k>=0.

I implemented something similar to their approach as follows:
Expand|Select|Wrap|Line Numbers
  1. from time import time
  2. startTime=time()
  3.  
  4. def getHeads(Hamming,prods,inds):
  5.     "Returns heads of three streams"
  6.     heads=[]
  7.     for p,i in zip(prods,inds):
  8.         heads.append(p*Hamming[i])
  9.     return heads
  10.  
  11. def merge(heads,inds):
  12.     "returns value to be added to hamming, and next set of inds"
  13.     val=min(heads)
  14.     for i in range(len(heads)):
  15.         if heads[i]==val:
  16.             inds[i]+=1
  17.     return val,inds
  18.  
  19. Hamming=[1]
  20.  
  21. prods=[2,3,5]
  22. inds=[0,0,0]
  23. for k in range(1,3000):
  24.     heads=getHeads(Hamming,prods,inds)
  25.     val,inds=merge(heads,inds)
  26.     Hamming.append(val)
  27.  
  28. print 3000,Hamming[3000-1]
  29. print "time: ",time()-startTime
This works rather well, generating the 3000 hamming numbers in about 14ms. But I was wondering about a more general setup to solve this problem generally. Like a class structure for these streams.

I started with this:
Expand|Select|Wrap|Line Numbers
  1. class Stream(object):
  2.     """Stream objects have a head (which is the front element), a
  3.     tail (which can be another stream, or a function(n,head)"""
  4.     def __init__(self,head,tail,n=0,tailIsStream=False):
  5.         self.n=0
  6.         self.tailStream=tailIsStream
  7.         self.head=head
  8.         self.tail=tail
  9.         self.values=[head]
  10.  
  11.     def call(self):
  12.         ans=self.head
  13.         self.n+=1
  14.         if self.tailStream:
  15.             self.head=self.tail.call()
  16.         else:
  17.             self.head=self.tail(self.n,self.head)
  18.         self.values.append(self.head)
  19.         return ans
  20.  
  21.     def __mul__(self,other):
  22.         return Stream(self.head*other,lambda n,head: head*other)
  23.  
  24.  
  25.  
  26. s=Stream(1,lambda n,head:n**2)
  27. for i in range(10):
  28.     print i, s.call()
which sort of works, but it's not obvious (at least to me) how to approach the coding of the hamming problem in this framework. And I wondered if there was a neater way anyway.

Thoughts?
Jun 10 '10 #1
0 1082

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
10
by: Bulba! | last post by:
Hello everyone, I'm reading the rows from a CSV file. csv.DictReader puts those rows into dictionaries. The actual files contain old and new translations of software strings. The dictionary...
7
by: jelle | last post by:
I now some hostility to functional programming is flaming up now and then; still could someone suggest me a pythonic equivalent for Mathematica's FixedPoint function? For those not familiar with...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
33
by: Gregory Petrosyan | last post by:
Buenos dias, amigos! I have to write _simple_ gui library, for embedding into game. My first attempt was to use XML: isn't it cute to describe ui in such a way: <window> <title>Hello...
9
by: SMB | last post by:
I have two lists of data like the following: LIST1 , ] LIST2 , 'label': 'First Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 1L}, {'code': 14L, 'name': 'Last...
5
by: akameswaran | last post by:
Disclaimer - I recognize this is not a practical exercise. There are many implementations around that would do the job better, more efficiently (Meaning in C) or whatever. I caught some thread...
1
by: Simon Forman | last post by:
I've got a function that I'd like to improve. It takes a list of lists and a "target" element, and it returns the set of the items in the lists that appear either before or after the target...
26
by: Frank Samuelson | last post by:
I love Python, and it is one of my 2 favorite languages. I would suggest that Python steal some aspects of the S language. ------------------------------------------------------- 1. Currently...
11
by: breal | last post by:
I have three lists... for instance a = ; b = ; c = ; I want to take those and end up with all of the combinations they create like the following lists
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.