Skip to content

Commit 43b2a92

Browse files
authored
Create design-spreadsheet.py
1 parent d79326d commit 43b2a92

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Python/design-spreadsheet.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Time: ctor: O(1)
2+
# setCell: O(1)
3+
# resetCell: O(1)
4+
# getValue: O(1)
5+
# Space: O(n)
6+
7+
import collections
8+
9+
10+
# hash table
11+
class Spreadsheet(object):
12+
13+
def __init__(self, rows):
14+
"""
15+
:type rows: int
16+
"""
17+
self.__lookup = collections.defaultdict(int)
18+
19+
20+
def setCell(self, cell, value):
21+
"""
22+
:type cell: str
23+
:type value: int
24+
:rtype: None
25+
"""
26+
self.__lookup[cell] = value
27+
28+
29+
def resetCell(self, cell):
30+
"""
31+
:type cell: str
32+
:rtype: None
33+
"""
34+
if cell in self.__lookup:
35+
del self.__lookup[cell]
36+
37+
38+
def getValue(self, formula):
39+
"""
40+
:type formula: str
41+
:rtype: int
42+
"""
43+
left, right = formula[1 :].split('+')
44+
x = self.__lookup.get(left, 0) if left[0].isalpha() else int(left)
45+
y = self.__lookup.get(right, 0) if right[0].isalpha() else int(right)
46+
return x+y

0 commit comments

Comments
 (0)