Evaluate division
Your task
You maintain n variables and process a list of operations. Each operation is either ["set", a, b, w] meaning the ratio value(a)/value(b) equals the float w, or ["q", a, b] meaning 'return the value of value(a)/value(b)'. For each query, return the ratio if a and b are connected by previously set relations, otherwise return -1.0. Queries where a == b in a known component return 1.0. Return the list of query answers (floats), each rounded to 5 decimal places.
Examples
weight_queries(n: int, ops: list) -> list[float]Example 1
Input
weight_queries(4, [["set", 0, 1, 2], ["set", 1, 2, 3], ["q", 0, 2], ["q", 0, 3]])Output
[6, -1]