-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathspreadsheet.py
More file actions
62 lines (46 loc) · 1.75 KB
/
spreadsheet.py
File metadata and controls
62 lines (46 loc) · 1.75 KB
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
53
54
55
56
57
58
59
60
61
62
"""
Write a class called Spreadsheet that will write (put) and obtain (get) values from a table
like in Excel. You can assume that values accessed will exist, but we may want to create a
caching system in order to avoid multiple highly intensive computational actions.
"""
class Spreadsheet:
def __init__(self):
self.cells = {}
self.cache = {}
self.graph = {}
def put(self, key: str, value: str) -> None:
self.cells[key] = value
self.recursively_clear_cache(key)
self.graph[key] = []
def get(self, key: str, visited=None) -> str:
if visited is None:
visited = set[str]()
if key in self.cache:
return self.cache[key]
if key in visited:
raise ValueError(f"Cycle detected: {key}")
visited.add(key)
value = self.cells[key]
without_equals_prefix = "".join(value.split("="))
values_list = without_equals_prefix.split("+")
total = 0
for new_value in values_list:
if new_value.isdigit():
total += int(new_value)
else:
total += self.get(new_value, visited.copy())
self.add_graph_dependency(new_value, key)
self.cache[key] = total
visited.remove(key)
return total
def recursively_clear_cache(self, key: str) -> None:
if key in self.cache:
del self.cache[key]
if not key in self.graph:
self.graph[key] = []
for parent_dep in self.graph[key]:
self.recursively_clear_cache(parent_dep)
def add_graph_dependency(self, child: str, parent: str) -> None:
if not child in self.graph:
self.graph[child] = []
self.graph[child].append(parent)