MCPcopy Create free account
hub / github.com/ByteByteGoHq/coding-interview-patterns / put

Method put

python3/Linked Lists/lru_cache.py:29–41  ·  view source on GitHub ↗
(self, key: int, value: int)

Source from the content-addressed store, hash-verified

27 return self.hashmap[key].val
28
29 def put(self, key: int, value: int) -> None:
30 # If a node with this key already exists, remove it from the
31 # linked list.
32 if key in self.hashmap:
33 self.remove_node(self.hashmap[key])
34 node = DoublyLinkedListNode(key, value)
35 self.hashmap[key] = node
36 # Remove the least recently used node from the cache if adding
37 # this new node will result in an overflow.
38 if len(self.hashmap) > self.capacity:
39 del self.hashmap[self.head.next.key]
40 self.remove_node(self.head.next)
41 self.add_to_tail(node)
42
43 def add_to_tail(self, node: DoublyLinkedListNode) -> None:
44 prev_node = self.tail.prev

Callers

nothing calls this directly

Calls 3

remove_nodeMethod · 0.95
add_to_tailMethod · 0.95

Tested by

no test coverage detected