-
Notifications
You must be signed in to change notification settings - Fork 0
/
097.py
159 lines (132 loc) · 5.02 KB
/
097.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
Problem:
Write a map implementation with a get function that lets you retrieve the value of a
key at a particular time.
It should contain the following methods:
set(key, value, time): # sets key to value for t = time.
get(key, time): # gets the key at t = time.
The map should work like this. If we set a key at a particular time, it will maintain
that value forever or until it gets set at a later time. In other words, when we get a
key at a time, it should return the value that was set for that key set at the most
recent time.
Consider the following examples:
d.set(1, 1, 0) # set key 1 to value 1 at time 0
d.set(1, 2, 2) # set key 1 to value 2 at time 2
d.get(1, 1) # get key 1 at time 1 should be 1
d.get(1, 3) # get key 1 at time 3 should be 2
d.set(1, 1, 5) # set key 1 to value 1 at time 5
d.get(1, 0) # get key 1 at time 0 should be null
d.get(1, 10) # get key 1 at time 10 should be 1
d.set(1, 1, 0) # set key 1 to value 1 at time 0
d.set(1, 2, 0) # set key 1 to value 2 at time 0
d.get(1, 0) # get key 1 at time 0 should be 2
"""
from typing import Any, Dict, Optional
class Node:
def __init__(self, val_list: Dict[int, int], time: int) -> None:
self.val_list = val_list
self.time = time
self.next = None
self.prev = None
def __repr__(self) -> str:
if self.next:
return f"{self.val_list} ({self.time}) <=> {str(self.next)}"
return f"{self.val_list} ({self.time})"
def __eq__(self, other: Any) -> bool:
if type(other) == Node:
return self.time == other.time
return False
class Double_Linked_List:
def __init__(self) -> None:
self.head = None
self.rear = None
self.length = 0
def __repr__(self) -> str:
return str(self.head)
def __bool__(self) -> bool:
return bool(self.head)
def add(self, val_list: Dict[int, int], time: int) -> None:
self.length += 1
if self.head == None:
self.head = Node(val_list, time)
self.rear = self.head
else:
self.rear.next = Node(val_list, time)
self.rear.next.prev = self.rear
self.rear = self.rear.next
class Map:
def __init__(self) -> None:
self.linked_list = Double_Linked_List()
def set(self, key: int, value: int, time: int) -> None:
if not self.linked_list:
self.linked_list.add({key: value}, time)
else:
pos = self.linked_list.head
while pos and pos.time < time:
pos = pos.next
# adding the new node at the head
if pos == self.linked_list.head:
temp_val_list = {key: value}
new_node = Node(temp_val_list, time)
self.linked_list.head.prev = new_node
new_node.next = self.linked_list.head
self.head = new_node
# adding the new value
if pos:
if time == pos.time:
pos.val_list[key] = value
else:
temp_val_list = dict(pos.prev.val_list)
temp_val_list[key] = value
new_node = Node(temp_val_list, time)
new_node.next = pos
new_node.prev = pos.prev
pos.prev.next = new_node
pos.prev = new_node
return
temp_val_list = dict(self.linked_list.rear.val_list)
temp_val_list[key] = value
self.linked_list.add(temp_val_list, time)
def get(self, key: int, time: int) -> Optional[int]:
if not self.linked_list:
return None
pos = self.linked_list.head
while pos and pos.time < time:
pos = pos.next
# key in the rear
if not pos:
try:
temp = self.linked_list.rear.val_list[key]
return temp
except:
return None
# key in the current node
elif pos and pos.time == time:
try:
temp = pos.val_list[key]
return temp
except:
return None
# key in previous node
else:
try:
temp = pos.prev.val_list[key]
return temp
except:
return None
if __name__ == "__main__":
d = Map()
d.set(1, 1, 0) # set key 1 to value 1 at time 0
d.set(1, 2, 2) # set key 1 to value 2 at time 2
print(d.get(1, 1)) # get key 1 at time 1 should be 1
print(d.get(1, 3)) # get key 1 at time 3 should be 2
print()
d = Map()
d.set(1, 1, 5) # set key 1 to value 1 at time 5
print(d.get(1, 0)) # get key 1 at time 0 should be null
print(d.get(1, 10)) # get key 1 at time 10 should be 1
print()
d = Map()
d.set(1, 1, 0) # set key 1 to value 1 at time 0
d.set(1, 2, 0) # set key 1 to value 2 at time 0
print(d.get(1, 0)) # get key 1 at time 0 should be 2