Given a collection of assignments such as a = b + 1 - 2, along with values for variables such as b = 3, determine the value of a requested target variable. An expression may depend on other variables and may contain multiple arithmetic operations.
Invalid formulas must be handled, including formulas that refer to an undefined variable or formulas whose dependencies form a cycle.
evaluate(expressions, target)
expressions contains the variable-assignment formulas, and target identifies the variable whose value should be determined.
The supplied example does not include a target name or an output value, but it demonstrates that an assignment may contain multiple operations and may refer to another variable.
Input: evaluate(["a = b + 1 - 2", "b = 3"], "a")
Output: 2
b evaluates to 3, so a is 3 + 1 - 2 = 2.
Input: evaluate(["a = b + 4", "b = 2"], "a")
Output: 6
The dependency on b is resolved first, giving a = 2 + 4 = 6.
a = b + 1 - 2.