-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem_1.py
236 lines (198 loc) · 5.64 KB
/
problem_1.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
class Node:
def __init__(self,value):
self.value = value
self.next = None
self.prev = None
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def prepend(self, data):
if self.head is None:
self.head = Node(data)
self.tail = self.head
else:
new_node = Node(data)
self.head.prev = new_node
new_node.next = self.head
self.head = new_node
return self.head
def append(self, data):
if self.head is None:
self.head = Node(data)
self.tail = self.head
else:
new_node = Node(data)
new_node.prev = self.tail
self.tail.next = new_node
self.tail = new_node
def search(self, data):
if self.head is None:
return None
else:
current_node = self.head
while current_node:
if current_node.value == data:
return current_node
else:
current_node = current_node.next
return None
def remove(self, data):
if self.head is None:
return
elif self.head.value == data:
self.head = self.head.next
return
else:
current_node = self.head
#print(current_node.next.value)
while current_node:
if current_node.next.value == data:
current_node.next = current_node.next.next
return
current_node = current_node.next
def pop(self):
if self.head is None:
return
else:
node = self.head.value
self.head = self.head.next
return node
def poptail(self):
if self.head is None:
return
else:
node = self.tail
self.tail = self.tail.prev
self.tail.next = None
return node
def insert(self, data, position):
if position == 0:
self.prepend(data)
return
if position > self.size():
position = self.size()
else:
position == position
new_node = Node(data)
current_node = self.head
for i in range(position-1):
current_node = current_node.next
new_node.next = current_node.next
current_node.next = new_node
def print_list(self):
current_node = self.head
while current_node is not None:
print(current_node.value)
current_node = current_node.next
def to_list(self):
createList = []
current_node = self.head
while current_node is not None:
createList.append(current_node.value)
current_node = current_node.next
return createList
def size(self):
if self.head is None:
return None
else:
size = 0
current_node =self.head
while current_node:
size += 1
current_node = current_node.next
return size
class LRU_Cache(object):
def __init__(self, capacity):
self.cache_size = capacity
self.cache_map = {}
self.lru_list = LinkedList()
# Initialize class variables
pass
def _lru_obj_find(self):
node = self.lru_list.poptail()
return node
def get(self, key):
# Retrieve item from provided key. Return -1 if nonexistent.
if key in self.cache_map:
node_obj = self.cache_map[key]
if node_obj.value is self.lru_list.head.value:
return self.lru_list.head.value
if node_obj.value is self.lru_list.tail.value:
self.lru_list.prepend(node_obj.value)
self.lru_list.poptail()
return node_obj.value
else:
if node_obj.prev is not None:
node_obj.prev.next = node_obj.next
if node_obj.next is not None:
node_obj.next.prev = node_obj.prev
self.lru_list.prepend(node_obj.value)
return node_obj.value
else:
return -1
pass
def set(self, key, value):
# Set the value if the key is not present in the cache. If the cache is at capacity remove the oldest item.
if key in self.cache_map:
return
else:
if self.cache_size != 0:
self.cache_map[key] = self.lru_list.prepend(value)
self.cache_size -= 1
else:
lru_key = self._lru_obj_find()
#print(lru_key.value)
del self.cache_map[lru_key.value]
self.cache_map[key] = self.lru_list.prepend(value)
return
# Test Case 1
our_cache = LRU_Cache(5) # Cache Size define to 5
our_cache.set(3, 3)
our_cache.set(1, 1)
our_cache.set(2, 2)
our_cache.set(4, 4)
our_cache.set(5, 5)
print(our_cache.get(1))
# 1
print(our_cache.get(2))
# 2
print(our_cache.get(4))
# 4
print(our_cache.get(77))
# -1
our_cache.set(6, 6)
our_cache.set(7, 7)
print(our_cache.get(5))
# -1
# Test Case 2
our_cache = LRU_Cache(3)
print(our_cache.get(1))
# -1
our_cache.set(1,1)
our_cache.set(2,2)
print(our_cache.get(1))
# 1
our_cache.set(7,7)
print(our_cache.get(2))
# 2
our_cache.set(8,8)
print(our_cache.get(1))
# -1
# Edge Case
our_cache = LRU_Cache(2)
our_cache.set(1,1)
our_cache.set(2,2)
print(our_cache.get(1))
# 1
print(our_cache.get(2))
# 2
print(our_cache.get(1))
# 1
print(our_cache.get(1))
# 1
print(our_cache.get(2))
# 2
our_cache.set(4,4)
print(our_cache.get(1))
# -1