User Tools

Site Tools


python:intvectorrange

This is an old revision of the document!


# gives all possible integer vectors between two endpoints, inclusively.  For example:
# IntVectorRange([0,0,2], [1,1,3]) yields:
#   [0, 0, 2]
#   [1, 0, 2]
#   [0, 1, 2]
#   [1, 1, 2]
#   [0, 0, 3]
#   [1, 0, 3]
#   [0, 1, 3]
#   [1, 1, 3]
class IntVectorRange:
	def __init__(self,start,end=None):
		if end is None:
			end=start
			start=[0] * len(end)
 
		self.start=start
		self.end=end
		self.delta = [end[i]-start[i]+1  for  i in xrange(len(start))]
		self.len = reduce(lambda x,y: x*y, self.delta, 1)
		self.size = len(start)
 
	def __getitem__(self,index): 
		if index >= self.len: raise IndexError
		r = [0] * self.size
		for i in xrange(len(self.start)):
			r[i] = index % self.delta[i] + self.start[i]
			index /= self.delta[i]
		return r
 
	def __len__(self): return self.len
python/intvectorrange.1274974896.txt.gz · Last modified: 2010/05/27 08:41 by tkbletsc

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki