python doomsday_fuel solution.py https://github.com/ivanseed/google-foobar-help/blob/master/challeng

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python doomsday_fuel solution.py https://github.com/ivanseed/google-foobar-help/blob/master/challeng相关的知识,希望对你有一定的参考价值。

def answer(m):
    # variable table
    # m = matrix
    # e = empty row index array
    # l = matrix size

    # import
    from fractions import Fraction
    #m = [[0,0,0,0],[0,0,0,0],[Fraction(1,10),0,Fraction(8,10),Fraction(1,10)],[Fraction(1,10),Fraction(1,10),Fraction(4,10),Fraction(4,10)]]
    #print "input matrix is"
    #for r in m:
    #    print r
    # size of matrix
    l = len(m)

    # array of indecies of empty rows
    e = []
    for i in range(l):
        if sum(m[i]) == 0:
            e.append(i)

    #print "input matrix height and width is:"
    #print l 
    #print "absorting states:"
    #print e 

    # number of empty rows
    len(e)

    # preprocess
    for i in e:
        m[i][i] = 1
    for i in range(l):
        s = sum(m[i])
        for j in range(l):
            m[i][j] = Fraction(m[i][j], s)

    # sort matrix
    def nxt(n):
        nl = e + list(set(range(l))-set(e))
        return nl[n]

    newm = [[0 for x in range(l)] for y in range(l)]
    for i in range(l):
        for j in range(l):
            newm[i][j] = m[nxt(i)][nxt(j)]
    m = newm

    #print  "sorted matrix is"
    #for r in m:
    #    print r

    if l == len(e):
        return [1,1]

    lenR = len(e)
    lenQ = l - len(e)

    R = [r[0:len(e)] for r in m[len(e)::]]
    Q = [r[len(e)::] for r in m[len(e)::]]
    #print "R is"
    #for r in R:
    #    print r
    #print "Q is"
    #for r in Q:
    #    print r

    I = [[0 for x in range(lenQ)] for y in range(lenQ)]
    for i in range(lenQ):
        I[i][i] = 1

    F = I
    for i in range(lenQ):
        for j in range(lenQ):
            F[i][j] = F[i][j]-Fraction(Q[i][j])
    # inverse
    def transposeMatrix(m):
        t = []
        for r in range(len(m)):
            tRow = []
            for c in range(len(m[r])):
                if c == r:
                    tRow.append(m[r][c])
                else:
                    tRow.append(m[c][r])
            t.append(tRow)
        return t

    def getMatrixMinor(m,i,j):
        return [row[:j] + row[j+1:] for row in (m[:i]+m[i+1:])]

    def getMatrixDeternminant(m):
        #base case for 2x2 matrix
        if len(m) == 2:
            return m[0][0]*m[1][1]-m[0][1]*m[1][0]

        determinant = 0
        for c in range(len(m)):
            determinant += ((-1)**c)*m[0][c]*getMatrixDeternminant(getMatrixMinor(m,0,c))
        return determinant

    def getMatrixInverse(m):
        determinant = getMatrixDeternminant(m)
        #special case for 2x2 matrix:
        if len(m) == 2:
            return [[m[1][1]/determinant, -1*m[0][1]/determinant],
                    [-1*m[1][0]/determinant, m[0][0]/determinant]]

        #find matrix of cofactors
        cofactors = []
        for r in range(len(m)):
            cofactorRow = []
            for c in range(len(m)):
                minor = getMatrixMinor(m,r,c)
                cofactorRow.append(((-1)**(r+c)) * getMatrixDeternminant(minor))
            cofactors.append(cofactorRow)
        cofactors = transposeMatrix(cofactors)
        for r in range(len(cofactors)):
            for c in range(len(cofactors)):
                cofactors[r][c] = cofactors[r][c]/determinant
        return cofactors

    Inv = getMatrixInverse(F)
    print "inverse matrix is"
    for r in Inv:
        print r
    def getMatrixProduct(m, n):
        r = [[0 for x in range(len(m))] for y in range(len(m))]
        rett=[]
        for i in range(len(m)):
            ret=[]
            for j in range(len(n[0])):
                ret.append(sum([a*b for a,b in zip(m[i],[row[j] for row in n])]))
            rett.append(ret)
        return rett

    def findDivisor(num):
        # 2,3 are the most common divisor for many numbers hence I go by divisor of 2,3
        # if not then by the same number as divisor
        if num%2 == 0:
            return 2
        elif num%3==0:
            return 3
        return num

    def findLCM(lcmArray):
        lcm = 1
        while len(lcmArray) > 0:
            minOfLCMArray = min(lcmArray)
            divisor = findDivisor(minOfLCMArray)        

            for x in xrange(0, len(lcmArray)):
                Quotient = lcmArray[x]/divisor
                Reminder = lcmArray[x]%divisor
                if Reminder == 0:
                    lcmArray[x] = Quotient

            lcm*=divisor
            minOfLCMArray = min(lcmArray)
            if minOfLCMArray == 1:
                lcmArray.remove(minOfLCMArray)
        return lcm

    p = getMatrixProduct(Inv, R)
    #print "product[0] is "
    #print p[0]
    #print "sum is"
    #print sum(p[0])
    q = [ i * findLCM([den.denominator for den in p[0]]) for i in p[0] ]
    #q = [ i * fractions.gcd([den.denominator for den in p[0]]) for i in p[0] ]
    t = [ i.numerator for i in q ]
    t.append(sum(t))
    return t

以上是关于python doomsday_fuel solution.py https://github.com/ivanseed/google-foobar-help/blob/master/challeng的主要内容,如果未能解决你的问题,请参考以下文章

鼠标插入/离开处理程序

DP——硬币问题

QueenPuzzle-N皇后问题

linux下安装配置rabbitMQ

PHP-最大子序和

Java Interview Programming Questions