def testLgiOf(): jogo = [10, 8, 7, 5, 3, 2] print 'jogo', jogo lgiObj = lgicomb.LgiCombiner(ic.comb(60, 6)-1,-1,jogo) lgi = lgiObj.getLgi() print 'lgi', lgi jogo = filterMinusOne(jogo) print 'jogo', jogo lgiObj = lgicomb.LgiCombiner(ic.comb(60, 6)-1,-1,jogo) lgi = lgiObj.getLgi() print 'lgi', lgi
def getLgi(self): lgi = 0 for i in range(self.size): value = self.iArray[i] posInv = self.size - i lgi += comb.comb(value, posInv) return lgi
def checkUpAmountInArrayCarried(carriedArray, lgi): size = len(carriedArray); soma = 0 for i in range(size): value = carriedArray[i] posInv = size - i soma += comb.comb(value, posInv) if soma <> lgi: msg = 'checkUpAmountInCarriedArray() ==>> soma (=%d) não igual a lgi (=%d) %s' %(soma, lgi, str(carriedArray)) raise ValueError, msg
def __init__(self, upLimit=0, size=1, iArrayIn=[0]): if size < 1: size = 1 if upLimit < 0: upLimit = 0 self.upLimit = upLimit # yet to be checked True self.stillFirst = False if iArrayIn == [0] and size > 1: self.iArray = range(size-1, -1, -1) self.stillFirst = True else: self.iArray = list(iArrayIn) if self.iArray == range(size-1, -1, -1): self.stillFirst = True self.size = len(self.iArray) self.checkArrayConsistency() self.nOfCombines = comb.comb(self.upLimit+1, self.size) self.iArrayGiven = list(self.iArray)
def approach(self, lgi, pointInf, pointSup, pos, arrayCarried, amount=0): global counter if pos == self.size: msg = 'Well, failed to get the LG Index. Reason: pos surpasses all available slots. (pos=%d, size=%d, pointInf=%d, pointSup=%d)' %(pos, self.size, pointInf, pointSup) print msg sys.exit(0) #raise ValueError, msg pointMid = pointInf + (pointSup - pointInf) / 2 posInv = self.size - pos parcel = comb.comb(pointMid, posInv) amountCompare = amount + parcel parcelSup = comb.comb(pointSup, posInv) amountCompareSup = amount + parcelSup parcelInf = comb.comb(pointInf, posInv) amountCompareInf = amount + parcelInf #msg = 'i=%d inf=%d/%d mid=%d/%d sup=%d/%d pos=%d %s press [ENTER]' %(lgi, pointInf, amountCompareInf, pointMid, amountCompare, pointSup, amountCompareSup, pos, str(arrayCarried)) #ans=raw_input(msg) #counter+=1 # global #print counter, msg if amountCompareSup < lgi: amount += parcelSup arrayCarried[pos] = pointSup pos += 1 # renew pointInf and pointSup pointInf = self.size - 1 - pos pointSup = pointSup - 1 # limiteSup return self.approach(lgi, pointInf, pointSup, pos, arrayCarried, amount) if amountCompareSup == lgi: # ok, game over arrayCarried[pos] = pointSup # check for consistency checkUpAmountInArrayCarried(arrayCarried, lgi) return arrayCarried if amountCompare > lgi: # the following check avoids an infinite recursion and logically complements the desired functionality if pointMid >= pointSup-1 and parcelInf < lgi: amount += parcelInf arrayCarried[pos] = pointInf pos += 1 # renew pointInf and pointSup pointSup = pointInf - 1 # limiteSup pointInf = self.size - 1 - pos return self.approach(lgi, pointInf, pointSup, pos, arrayCarried, amount) pointSup = pointMid return self.approach(lgi, pointInf, pointSup, pos, arrayCarried, amount) elif amountCompare < lgi: if pointMid >= pointSup - 1: if amountCompareSup < lgi: amount += parcelSup arrayCarried[pos] = pointSup pointSup = pointSup - 1 # limiteSup else: amount += parcel arrayCarried[pos] = pointMid pointSup = pointMid - 1 # limiteSup pos += 1 # renew pointInf and pointSup pointInf = self.size - 1 - pos return self.approach(lgi, pointInf, pointSup, pos, arrayCarried, amount) pointInf = pointMid # pointSup is the same return self.approach(lgi, pointInf, pointSup, pos, arrayCarried, amount) else: # ie, amountCompare == lgi ie, element has just been FOUND! arrayCarried[pos] = pointMid # check for consistency checkUpAmountInArrayCarried(arrayCarried, lgi) return arrayCarried
#read: fd = open('myfile.dat', 'rb') read_data = scipy.io.numpyio.fread(fd, 1, 'i') print 'read_data', read_data read_data = scipy.io.numpyio.fread(fd, 1, 'i') print 'read_data', read_data mask = [0]*4 mask[0] = 2**8 - 1 # ie, 255 for i in range(1,4): mask[i] = mask[i-1] << 8 maxInt = 2**16 - 1 import IndicesCombiner c25to15 = IndicesCombiner.comb(25,15) def m4(): randomLgis = [] tmpfile = "tmp.bin" fileobj = open(tmpfile, 'wb') nOfBytes = 3 for i in range(5): lgi = random.randint(0,c25to15) randomLgis.append(lgi) print 'lgi', lgi bytes = binDec.packByteInt(lgi, nOfBytes) for byte in bytes: #print 'byte', byte fileobj.write(chr(byte))