-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdesign-log-storage-system.py
40 lines (35 loc) · 1.11 KB
/
design-log-storage-system.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
class LogSystem:
def __init__(self):
self.storage = []
def put(self, id, timestamp):
"""
:type id: int
:type timestamp: str
:rtype: void
"""
ts = tuple(map(int, timestamp.split(':'))) + (id,)
self.storage.insert(bisect.bisect(self.storage, ts), ts)
def retrieve(self, s, e, gra):
"""
:type s: str
:type e: str
:type gra: str
:rtype: List[int]
"""
gra_idx = {
'Year': 1,
'Month': 2,
'Day': 3,
'Hour': 4,
'Minute': 5,
'Second': 6,
}[gra]
lo = list(map(int, s.split(':')))[:gra_idx] + [0] * (7 - gra_idx)
hi = list(map(int, e.split(':')))[:gra_idx] + [0] * (7 - gra_idx)
hi[gra_idx - 1] += 1
lo, hi = tuple(lo), tuple(hi)
return [ts[-1] for ts in self.storage[bisect.bisect_left(self.storage, lo) : bisect.bisect_left(self.storage, hi)]]
# Your LogSystem object will be instantiated and called as such:
# obj = LogSystem()
# obj.put(id,timestamp)
# param_2 = obj.retrieve(s,e,gra)