aboutsummaryrefslogtreecommitdiff
path: root/design_spreadsheet.py
blob: bd26a2905af64c8cf889351b814fa1f8fd14c03d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 3484. Design Spreadsheet

"""
a spreadsheet is a grid with 26 columns labeled from 'A' to 'Z' and a given
number of 'rows'. each cell in the spreadsheet can hold an integer value
between 0 and 10^5. note: if 'spreadsheetGetValue' references a cell that has
not been explicitly set using 'spreadsheetSetCell', its value is considered
to be 0.
"""


class Spreadsheet(object):
    def __init__(self, rows):
        """
        :type rows: int
        """
        self.rows = rows
        self.cells = {}

    def setCell(self, cell, value):
        """
        :type cell: str
        :type value: int
        :rtype: None
        """
        self.cells[cell] = value

    def resetCell(self, cell):
        """
        :type cell: str
        :rtype: None
        """
        self.cells.pop(cell, None)

    def getValue(self, formula):
        """
        :type formula: str
        :rtype: int
        """
        parts = formula[1:].split("+")
        return sum(int(p) if p.isdigit() else self.cells.get(p, 0) for p in parts)


if __name__ == "__main__":
    obj = Spreadsheet(3)
    print(obj.getValue(formula="=5+7"))
    obj.setCell(cell="A1", value=10)
    print(obj.getValue(formula="=A1+6"))
    obj.setCell(cell="B2", value=15)
    print(obj.getValue(formula="=A1+B2"))
    obj.resetCell(cell="A1")
    print(obj.getValue(formula="=A1+B2"))