aboutsummaryrefslogtreecommitdiff
path: root/design_spreadsheet.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-09-18 20:58:15 -0600
committerJack Sangdahl <[email protected]>2025-09-18 20:58:15 -0600
commitc9b062a27109bc79e6dd015644509c745e696808 (patch)
tree9a888b2638cde4eb13d8d0f362a5b94fca6f9af9 /design_spreadsheet.py
parent0d81814bf3d0d8358d1e527de88683732ed55d23 (diff)
downloadleetcode-c9b062a27109bc79e6dd015644509c745e696808.tar.gz
leetcode-c9b062a27109bc79e6dd015644509c745e696808.zip
3484. design spreadsheet
Diffstat (limited to 'design_spreadsheet.py')
-rw-r--r--design_spreadsheet.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/design_spreadsheet.py b/design_spreadsheet.py
new file mode 100644
index 0000000..bd26a29
--- /dev/null
+++ b/design_spreadsheet.py
@@ -0,0 +1,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"))