Участник:Turk0v/Evaluate Division

Материал из DISCOPAL
Перейти к: навигация, поиск
class Solution:
    def calcEquation(self, equations: List[List[str]], values: List[float], queries: List[List[str]]) -> List[float]:
        res_dict = defaultdict(dict)
        for (iter1, iter2), val in zip(equations, values):
            res_dict[iter1][iter1] = res_dict[iter2][iter2] = 1.0
            res_dict[iter1][iter2] = val
            res_dict[iter2][iter1] = 1 / val
        for s in res_dict:
            for k in res_dict[s]:
                for m in res_dict[s]:
                    res_dict[k][m] = res_dict[k][s] * res_dict[s][m]
        return [res_dict[iter1].get(iter2, -1.0) for iter1, iter2 in queries]