-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathdesign-spreadsheet.py
46 lines (36 loc) · 972 Bytes
/
design-spreadsheet.py
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
# Time: ctor: O(1)
# setCell: O(1)
# resetCell: O(1)
# getValue: O(1)
# Space: O(n)
import collections
# hash table
class Spreadsheet(object):
def __init__(self, rows):
"""
:type rows: int
"""
self.__lookup = collections.defaultdict(int)
def setCell(self, cell, value):
"""
:type cell: str
:type value: int
:rtype: None
"""
self.__lookup[cell] = value
def resetCell(self, cell):
"""
:type cell: str
:rtype: None
"""
if cell in self.__lookup:
del self.__lookup[cell]
def getValue(self, formula):
"""
:type formula: str
:rtype: int
"""
left, right = formula[1 :].split('+')
x = self.__lookup.get(left, 0) if left[0].isalpha() else int(left)
y = self.__lookup.get(right, 0) if right[0].isalpha() else int(right)
return x+y