Implement Excel

Asked at CitadelPhone screen

Reported by the problem's creator, not verified

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) puts value, a string, in cell, 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 integer cell holds 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.

Examples

  1. Example 1

    Input: 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]

    Explanation: B1 = A1 + 5 = 8. After A1 becomes 10, B1 reads 15 without being set again. C1 = 10 + 11 = 21, and Z9 was never set, so it is 0.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.