Implement Excel
CitadelPhone screenMedium
Build a small spreadsheet. Each cell has a name made of capital letters followed by a row number, such as A1, B12 or AA3, and holds either an integer or a formula. Write the class Spreadsheet with these methods.
set(cell, value)putsvalue, a string, incell, replacing whatever the cell held. A value that does not start with=is an integer, such as"7"or"-3". A value that starts with=is a formula: terms joined by+, where each term is a non-negative integer or the name of a cell, such as"=A1+5","=10+11"or"=B2+B3". The formula's value is the sum of its terms.get(cell)returns the integercellholds right now: a plain integer as it is, a formula as the sum of its terms using the cells' current values.
A cell that was never set holds 0. A formula is live: after a cell it uses changes, get on the formula reflects the new value without the formula being set again. set returns nothing.
Part 1: Cells, formulas and live reads
Implement set and get. For example, after set("A1", "3") and set("B1", "=A1+5"), get("B1") is 8. After set("A1", "10"), get("B1") is 15. After set("C1", "=10+11"), get("C1") is 21, and get("Z9") is 0 because Z9 was never set.
- Example 1
- init
[]- operations
[["set",["A1","3"]],["set",["B1","=A1+5"]],["get",["B1"]],["set",["A1","10"]],["get",["B1"]],["set",["C1","=10+11"]],["get",["C1"]],["get",["Z9"]]]- Output
[null,null,8,null,15,null,21,0]
Why:
B1 = A1 + 5 = 8. AfterA1becomes 10,B1reads 15 without being set again.C1 = 10 + 11 = 21, andZ9was never set, so it is 0.
Constraints
- A cell name is 1 to 3 capital letters followed by a row number from 1 to 1000, with no leading zeros.
-10^9 <= n <= 10^9for a plain integer valuen.- A formula has 1 to 20 terms, and each integer term is between 0 and 10^9.
- A formula may use cells that have not been set yet; they hold 0 until set.
- No call makes a cell depend on itself, directly or through other cells.
- Every value that is ever read fits in a signed 64-bit integer.
- At most 10^4 calls in total.