770. Basic Calculator IV

Practice Basic Calculator IV for free on NudgeCode: Hard, Stack pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

expression is built from non-negative integers, variables (lowercase words such as a or temp), +, -, * and parentheses, with every token separated by a single space (e.g. "e + 8 - a + 5", "( e + 8 ) * ( e - 8 )"). The usual precedence applies: parentheses, then *, then + / - left to right. There is no unary minus.

evalvars[i] has the known value evalints[i]; substitute these, then simplify the expression to a polynomial in the remaining variables and return its terms as a list of strings:

Example

expression = "e + 8 - a + 5", evalvars = ["e"], evalints = [1]  ->  ["-1*a","14"]
expression = "e - 8 + temperature - pressure", evalvars = ["e","temperature"], evalints = [1,12]
->  ["-1*pressure","5"]
expression = "(e + 8) * (e - 8)", evalvars = [], evalints = []  ->  ["1*e*e","-64"]

Constraints