Skip to content

Commit 06dd9f1

Browse files
committed
reviewer feedback implemented
1 parent e30a88f commit 06dd9f1

File tree

2 files changed

+39
-92
lines changed

2 files changed

+39
-92
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
node_modules
33
.venv
44
__pycache__
5+
package-lock.json
Lines changed: 38 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,47 @@
11
import time
22

3-
our_list = []
4-
lookup_map = {}
5-
limit = 0
6-
7-
# `LruCache(limit)` should construct
8-
# an LRU cache which never stores more than `limit` entries.
9-
def LruCache(user_limit):
10-
global limit, our_list, lookup_map
11-
limit = user_limit
12-
our_list = []
13-
lookup_map = {}
14-
15-
# * `set(key, value)` should associate `value` with the passed `key`.
16-
def set(key, value):
17-
global our_list, lookup_map
18-
19-
if key in lookup_map:
20-
old_item = lookup_map[key]
21-
our_list.remove(old_item)
22-
23-
wrapped_item = {
24-
"key": key,
25-
"value": value,
26-
"tracker": time.time()
27-
}
28-
29-
#add to list and map
30-
our_list.insert(0, wrapped_item)
31-
lookup_map[key] = wrapped_item
32-
33-
#if full remove oldest timestamp so last
34-
if len(our_list) > limit:
35-
oldest_item = our_list.pop()
36-
37-
del lookup_map[oldest_item["key"]]
38-
39-
40-
# * `get(key)` should look-up the value previously associated with `key`.
41-
def get(key):
42-
global our_list, lookup_map
43-
#check map instead of for loop
44-
if key in lookup_map:
45-
# find by key
46-
item = lookup_map[key]
3+
class LruCache:
4+
# `LruCache(limit)` should construct
5+
# an LRU cache which never stores more than `limit` entries.
6+
def __init__(self, user_limit):
7+
self.limit = user_limit
8+
self.our_list = []
9+
self.lookup_map = {}
10+
11+
# * `set(key, value)` should associate `value` with the passed `key`.
12+
def set(self, key, value):
4713

48-
# // tracker to now timestamp updaed
49-
item["tracker"] = time.time()
14+
if key in self.lookup_map:
15+
old_item = self.lookup_map[key]
16+
self.our_list.remove(old_item)
17+
18+
wrapped_item = {
19+
"key": key,
20+
"value": value,
21+
}
5022

51-
#move to front
52-
our_list.remove(item)
53-
our_list.insert(0, item)
23+
#add to list and map
24+
self.our_list.insert(0, wrapped_item)
25+
self.lookup_map[key] = wrapped_item
5426

55-
return item["value"]
56-
return None
57-
58-
59-
#before i did it this was but was looping over each item and did not
60-
#fit the required complexity
61-
# def get_old(key):
62-
# for item in our_list:
63-
# if item["key"] == key:
64-
# item["tracker"] = time.time()
65-
# our_list.remove(item)
66-
# our_list.insert(0, item)
67-
# return item["value"]
68-
# return None
69-
70-
#and before tried with an id not timestamp
71-
# tracker_number = 0
27+
#if full remove oldest timestamp so last
28+
if len(self.our_list) > self.limit:
29+
oldest_item = self.our_list.pop()
30+
31+
del self.lookup_map[oldest_item["key"]]
7232

73-
# def set(key, value):
74-
# global tracker_number, our_list, lookup_map
75-
76-
77-
# wrapped_item = {
78-
# "key": key,
79-
# "value": value,
80-
# "tracker": tracker_number
81-
# }
82-
83-
84-
# tracker_number += 1
85-
86-
# our_list.insert(0, wrapped_item)
87-
# lookup_map[key] = wrapped_item
8833

89-
# and the loop
90-
# def get_old(key):
91-
# global tracker_number, our_list
92-
# for item in our_list:
93-
# if item["key"] == key:
94-
# item["tracker"] = tracker_number
95-
# tracker_number += 1
34+
# * `get(key)` should look-up the value previously associated with `key`.
35+
def get(self, key):
36+
#check map instead of for loop
37+
if key in self.lookup_map:
38+
# find by key
39+
item = self.lookup_map[key]
9640

97-
# our_list.remove(item)
98-
# our_list.insert(0, item)
41+
#move to front
42+
self.our_list.remove(item)
43+
self.our_list.insert(0, item)
9944

100-
# return item["value"]
101-
# return None
45+
return item["value"]
46+
return None
47+

0 commit comments

Comments
 (0)