A/C Python, solution, easy to understand, beat 50.69%


  • 0
    W
    def calcEquation(self, equations, values, queries):
        """
        :type equations: List[List[str]]
        :type values: List[float]
        :type queries: List[List[str]]
        :rtype: List[float]
        """
        lenEQ = len(equations)
        lenValues = len(values)
        lenQuery = len(queries)
    
        if lenEQ != lenValues:
            #print "lenEQ != lenValues, something wrong"
            return
    
        class local:
            eqMap = {}
            res = []
            c_num_map = {}
            c_list = []
            prefix = "_"
    
        for idx in range(lenEQ):
            #print "idx = ", idx
            #print "equations[idx] = ", equations[idx]
            #print "values[idx] = ", values[idx]
            top = equations[idx][0]
            down = equations[idx][1]
    
            # top, down =  removeDup(top, down)
            #print "top = ", top, " down = ", down
            if down not in local.eqMap:
                local.eqMap[down] = [(top, values[idx])]
            else:
                local.eqMap[down].append((top, values[idx]))
    
    
        #print "local.eqMap = ", local.eqMap
        for k in sorted(local.eqMap, key=lambda k: len(local.eqMap[k]), reverse=True):
            ##print k
            local.c_list.append(k)
    
    
        #print "local.c_list = ", local.c_list
        res = []
        lenEquations = len(equations)
    
        local.c_num_map[local.c_list[0]] = 1
        for idx in range(len(local.c_list)):
            c = local.c_list[idx]
            #print "c = ", c
            if c not in local.c_num_map:
                #print "need set value"
                flagNew = True
                nbs = local.eqMap[c]
                #print "nbs = ", nbs
                for nb in nbs:
                    newc = nb[0]
                    #print "newc = ", newc
                    if newc in local.c_num_map:
                        newc_val = local.c_num_map[newc]
                        #print "newc_val = ", newc_val
                        local.c_num_map[c] = newc_val / nb[1]
                        flagNew = False
                        break
    
                if flagNew == True:
                    #print "special case, need do something"
                    intVal = 1
                    local.c_num_map[c] = local.prefix + str(intVal)
    
    
                    for nb in nbs:
                        newc = nb[0]
                        newc_val = intVal * nb[1]
                        local.c_num_map[newc] = local.prefix + str(newc_val)
    
                    local.prefix += "_"
            else:
                #print "already set value, local.c_num_map[c] = ", local.c_num_map[c]
                nbs = local.eqMap[c]
                #print "nbs = ", nbs
                for nb in nbs:
                    newc = nb[0]
                    val = nb[1] * local.c_num_map[c]
                    #print "newc = ", newc, " val = ", val
                    if newc not in local.c_num_map:
                        local.c_num_map[newc] = val
    
        #print "local.c_num_map = ", local.c_num_map
    
        for q in queries:
            #print "q = ", q
            tops = q[0]
            topP = 1
    
    
            downs = q[1]
            downP = 1
    
            if tops not in local.c_num_map or downs not in  local.c_num_map:
                res.append(-1)
                continue
    
            #print "tops = ", tops, " local.c_num_map[tops] = ", local.c_num_map[tops], " type(local.c_num_map[tops]) = ", type(local.c_num_map[tops])
            #print "topP = ", topP
    
            #print "downs = ", downs, " local.c_num_map[downs] = ", local.c_num_map[downs], " type(local.c_num_map[downs]) = ", type(local.c_num_map[downs])
            # #print "downP = ", downP
    
            if type(local.c_num_map[tops]) is str and type(local.c_num_map[downs]) is str:
                #print "all str"
                topStr = local.c_num_map[tops]
                downStr = local.c_num_map[downs]
    
                if topStr.count('_') != downStr.count('_'):
                    res.append(-1)
                else:
                    numPrefix = topStr.count('_')
                    topStr = topStr[numPrefix:]
                    downStr = downStr[numPrefix:]
    
                    #print "topStr = ", topStr, " downStr = ", downStr
                    topP = float(topStr)
                    downP = float(downStr)
    
                    res.append(topP/downP)
    
            elif type(local.c_num_map[tops]) is not str and type(local.c_num_map[downs]) is not str:
                #print "all int"
                if tops in local.c_num_map:
                    topP = local.c_num_map[tops]
                else:
                    topP = "xyz"
    
                # for top in tops:
                #     if top in local.c_num_map:
                #         topP = topP * local.c_num_map[top]
                #     else:
                #         topP = "xyz"
                #         break
                #print "topP = ", topP
    
    
                if downs in local.c_num_map:
                    downP = local.c_num_map[downs]
                else:
                    downP = "xyz"
    
                #print "downP = ", downP
    
                if topP != "xyz" and downP != "xyz":
                    res.append(topP/downP)
                else:
                    res.append(-1)
    
            else:
                #print "one str, one int"
                res.append(-1)
        #print "res = ", res
        return res

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.