-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathlru_cache.py
More file actions
101 lines (76 loc) · 2.42 KB
/
lru_cache.py
File metadata and controls
101 lines (76 loc) · 2.42 KB
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
import time
our_list = []
lookup_map = {}
limit = 0
# `LruCache(limit)` should construct
# an LRU cache which never stores more than `limit` entries.
def LruCache(user_limit):
global limit, our_list, lookup_map
limit = user_limit
our_list = []
lookup_map = {}
# * `set(key, value)` should associate `value` with the passed `key`.
def set(key, value):
global our_list, lookup_map
if key in lookup_map:
old_item = lookup_map[key]
our_list.remove(old_item)
wrapped_item = {
"key": key,
"value": value,
"tracker": time.time()
}
#add to list and map
our_list.insert(0, wrapped_item)
lookup_map[key] = wrapped_item
#if full remove oldest timestamp so last
if len(our_list) > limit:
oldest_item = our_list.pop()
del lookup_map[oldest_item["key"]]
# * `get(key)` should look-up the value previously associated with `key`.
def get(key):
global our_list, lookup_map
#check map instead of for loop
if key in lookup_map:
# find by key
item = lookup_map[key]
# // tracker to now timestamp updaed
item["tracker"] = time.time()
#move to front
our_list.remove(item)
our_list.insert(0, item)
return item["value"]
return None
#before i did it this was but was looping over each item and did not
#fit the required complexity
# def get_old(key):
# for item in our_list:
# if item["key"] == key:
# item["tracker"] = time.time()
# our_list.remove(item)
# our_list.insert(0, item)
# return item["value"]
# return None
#and before tried with an id not timestamp
# tracker_number = 0
# def set(key, value):
# global tracker_number, our_list, lookup_map
# wrapped_item = {
# "key": key,
# "value": value,
# "tracker": tracker_number
# }
# tracker_number += 1
# our_list.insert(0, wrapped_item)
# lookup_map[key] = wrapped_item
# and the loop
# def get_old(key):
# global tracker_number, our_list
# for item in our_list:
# if item["key"] == key:
# item["tracker"] = tracker_number
# tracker_number += 1
# our_list.remove(item)
# our_list.insert(0, item)
# return item["value"]
# return None