Skip to content

Commit b9519f4

Browse files
init impl
1 parent 309325d commit b9519f4

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import heapq
2+
import unittest
3+
from collections import defaultdict
4+
5+
class TimeMap:
6+
7+
def __init__(self):
8+
"""
9+
Initialize your data structure here.
10+
"""
11+
self.valueToTimeKeyEntry = defaultdict(lambda: [])
12+
13+
def set(self, key: str, value: str, timestamp: int) -> None:
14+
heapq.heappush(self.valueToTimeKeyEntry[key], (-timestamp, value))
15+
16+
def get(self, key: str, timestamp: int) -> str:
17+
tempList = []
18+
maxHeap = self.valueToTimeKeyEntry[key]
19+
20+
result = ""
21+
while maxHeap:
22+
topTime, topValue = heapq.heappop(maxHeap)
23+
tempList.append((topTime, topValue))
24+
if -topTime <= timestamp:
25+
result = topValue
26+
break
27+
28+
for item in tempList:
29+
heapq.heappush(maxHeap, item)
30+
31+
return result
32+
33+
class TestFailure(unittest.TestCase):
34+
35+
def test_failure(self):
36+
# Input: inputs = ["TimeMap", "set", "get", "get", "set", "get", "get"], inputs = [[], ["foo", "bar", 1],
37+
# ["foo", 1], ["foo", 3],
38+
# ["foo", "bar2", 4], ["foo", 4],
39+
# ["foo", 5]]
40+
# Output: [null, null, "bar", "bar", null, "bar2", "bar2"]
41+
timeMap = TimeMap()
42+
timeMap.set("foo", "bar", 1)
43+
print(timeMap.get("foo", 1))
44+
print(timeMap.get("foo", 3))
45+
timeMap.set("foo", "bar2", 4)
46+
print(timeMap.get("foo", 4))
47+
print(timeMap.get("foo", 5))
48+

Python/TestDataStructureDict.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,18 @@ def test_DeleteElement(self):
7575
del dictionary['fruit']
7676
print(dictionary)
7777

78+
@unittest.skip
7879
def test_NestedDefaultDict(self):
7980
# nestedDict = defaultdict(lambda: defaultdict(dict))
8081

8182
nestedDict = defaultdict(defaultdict)
8283
nestedDict[0][1] = 2
8384
print(nestedDict[0][1])
8485

86+
def test_NestedDefaultDictGetValue(self):
87+
startToEndAndTotalTime = defaultdict(lambda: defaultdict(lambda: (0, 0)))
88+
totalTime, num = startToEndAndTotalTime["spa"][0]
89+
print(totalTime)
8590

8691
if __name__ == '__main__':
8792
unittest.main()

Python/TestUtilities.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ def test_bisectSearch(self):
2020
print("The rightmost index to insert, so list remains sorted is : ", end="")
2121
print(bisect.bisect_right(li, 4))
2222

23+
@unittest.skip
2324
def test_collectionsCounter(self):
2425
li = [1, 3, 4, 4, 4, 6, 7]
2526

2627
# returns a default dictionary
2728
result = collections.Counter(li)
2829
self.assertEqual(0, result[0])
2930

31+
def test_mod(self):
32+
i = -2
33+
a = i % 4
34+
print(a)
35+
3036
if __name__ == '__main__':
3137
unittest.main()

0 commit comments

Comments
 (0)