Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
node_modules
.venv
__pycache__
package-lock.json
36 changes: 36 additions & 0 deletions Sprint-2/implement_lru_cache/lru_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from collections import OrderedDict

class LruCache:
# TASK
# `LruCache(limit)` should construct
# an LRU cache which never stores more than `limit` entries.
def __init__(self, user_limit):
self.limit = user_limit
self.key_dictionary = OrderedDict()

# TASK
# `set(key, value)` should associate `value` with the passed `key`.
def set(self, key, value):

if key in self.lookup_map:
old_item = self.lookup_map[key]
self.key_dictionary.pop(key)
wrapped_item = {
"key": key,
"value": value,
}

self.key_dictionary[key] = wrapped_item
self.key_dictionary.move_to_end(key, last=False)

if len(self.our_key_dictionary) > self.limit:
self.oey_dictionary.popitem()

def get(self, key):
if key in self.our_key_dictionary:
item = self.our_key_dictionary[key]
self.key_dictionary.move_to_end(key, last=False)

return item["value"]
return None