Design and implement a small in-memory spreadsheet.
The spreadsheet must support cell labels such as A1 and B10, where a label consists of uppercase letters followed by a positive row number.
Implement the following API:
set_cell(label, value)
get_cell(label) -> int
Part 1: Plain integer cells
A cell can store an integer value.
Example:
set_cell("A1", 10)
get_cell("A1") # returns 10
Part 2: Formulas
A cell can also store a formula. A formula is an expression containing only:
-
integer literals,
-
cell references,
-
the
+
operator.
You do not need to support subtraction, multiplication, division, parentheses, or operator precedence beyond addition.
Examples:
set_cell("A1", "10")
set_cell("A2", "A1+20")
get_cell("A2") # returns 30
set_cell("A3", "A1+A2+5")
get_cell("A3") # returns 45
Formulas should be evaluated using the current values of referenced cells. If a referenced cell does not exist, raise an error.
Part 3: Cycle detection
The spreadsheet must detect cyclic dependencies and report an error instead of recursing forever.
Example:
set_cell("A1", "B1+1")
set_cell("B1", "A1+1")
get_cell("A1") # raises a cycle-detected error
Implement the data structures and logic needed for set_cell and get_cell.