Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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.our_key_dictionary = OrderedDict()
Comment thread
katarzynakaz marked this conversation as resolved.
Outdated

# 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.our_key_dictionary.pop(key)
wrapped_item = {
"key": key,
"value": value,
}

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

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

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

return item["value"]
return None