-
-
Notifications
You must be signed in to change notification settings - Fork 27
Glasgow | 25 SDC NOV | Katarzyna Kazimierczuk | Sprint 2 | Lru cache #118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the value of property |
||
| } | ||
|
|
||
| #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 | ||
|
Comment on lines
+59
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this code are not used, can you remove them to keep the code clean? |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OrderedDictis probably more efficient.