399. Evaluate Division

Practice Evaluate Division for free on NudgeCode: Medium, Graph pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

Each equations[i] = [a, b] together with values[i] states that a / b = values[i], where a and b are variable names. For each query [c, d], return the value of c / d derived from the equations, or -1.0 if it cannot be determined. A variable that never appears in any equation is unknown, so any query that uses it (even x / x) gives -1.0.

The equations never contradict each other and no value is zero. Answers within 1e-5 are accepted.

Example

equations = [["a","b"],["b","c"]], values = [2.0,3.0],
queries = [["a","c"],["b","a"],["a","e"],["a","a"],["x","x"]]
->  [6.0,0.5,-1.0,1.0,-1.0]

Constraints