Skip to content

Commit 0af9dcf

Browse files
committed
reviewer feedback implemented
1 parent 6d42d37 commit 0af9dcf

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed
Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,36 @@
1-
import time
21
from collections import OrderedDict
32

43
class LruCache:
5-
# `LruCache(limit)` should construct
4+
# TASK
5+
# `LruCache(limit)` should construct
66
# an LRU cache which never stores more than `limit` entries.
77
def __init__(self, user_limit):
88
self.limit = user_limit
9-
self.our_list = OrderedDict()
10-
self.lookup_map = {}
9+
self.our_key_dictionary = OrderedDict()
1110

12-
# * `set(key, value)` should associate `value` with the passed `key`.
11+
# TASK
12+
# `set(key, value)` should associate `value` with the passed `key`.
1313
def set(self, key, value):
1414

1515
if key in self.lookup_map:
1616
old_item = self.lookup_map[key]
17-
self.our_list.pop(key)
17+
self.our_key_dictionary.pop(key)
1818
wrapped_item = {
1919
"key": key,
2020
"value": value,
2121
}
2222

23-
#add to list and map
24-
self.our_list[key] = wrapped_item
25-
self.our_list.move_to_end(key, last=False)
26-
self.lookup_map[key] = wrapped_item
23+
self.our_key_dictionary[key] = wrapped_item
24+
self.our_key_dictionary.move_to_end(key, last=False)
2725

28-
#if full remove oldest timestamp so last
29-
if len(self.our_list) > self.limit:
30-
# oldest_item = self.our_list.pop()
31-
32-
# del self.lookup_map[oldest_item["key"]]
33-
oldest_key, oldest_item = self.our_list.popitem()
34-
del self.lookup_map[oldest_key]
26+
if len(self.our_key_dictionary) > self.limit:
27+
self.our_key_dictionary.popitem()
3528

36-
# * `get(key)` should look-up the value previously associated with `key`.
3729
def get(self, key):
38-
#check map instead of for loop
39-
if key in self.lookup_map:
40-
# find by key
41-
item = self.lookup_map[key]
42-
43-
#move to front
44-
# self.our_list.remove(item)
45-
# self.our_list.insert(0, item)
46-
self.our_list.move_to_end(key, last=False)
47-
30+
if key in self.our_key_dictionary:
31+
item = self.our_key_dictionary[key]
32+
self.our_key_dictionary.move_to_end(key, last=False)
33+
4834
return item["value"]
4935
return None
5036

0 commit comments

Comments
 (0)