A lightweight spreadsheet in the browser evaluates formulas like =A1*(B2+3)-4/2. Write evaluateFormula(formula, cells) that returns the formula's value.
=, which is ignored. Spaces can appear anywhere between tokens.A1 or BC12), the operators + - * /, and parentheses.cells[name]; a cell missing from cells counts as 0.* and / bind tighter than + and -, operators of the same level go left to right, and - can also be a unary minus (as in -A1 or 2*-3). / divides and truncates toward zero.Input: formula = "=A1*(B2+3)-4/2", cells = {"A1":2,"B2":5} Output: 14
2 * (5 + 3) - 2 is 14.
Input: formula = "=-C3 + 7 / 2", cells = {"C3":4} Output: -1
-4 + 3 (7 / 2 truncates to 3) is -1.
1 <= formula.length <= 10^42^31.Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #630 Evaluate a Spreadsheet Formula. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.