class LRUCache: def __init__(self, limit=10): self.limit = limit self.length = 0 self.storage = DoublyLinkedList() """ Retrieves the value associated with the given key. Also needs to move the key-value pair to the end of the order such that the pair is considered most-recently used. Returns the value associated with the key or None if the key-value pair doesn't exist in the cache. """ def get(self, key): current = self.storage.head while current is not None: if current.value[0] == key: self.storage.move_to_front(current) return current.value[1] current = current.next return None """ Adds the given key-value pair to the cache. The newly- added pair should be considered the most-recently used entry in the cache. If the cache is already at max capacity before this entry is added, then the oldest entry in the cache needs to be removed to make room. Additionally, in the case that the key already exists in the cache, we simply want to overwrite the old value associated with the key with the newly-specified value. """ def set(self, key, value): current = self.storage.head replaced = False while current is not None and replaced is not True: if current.value[0] == key: current.value[1] = value self.storage.move_to_front(current) replaced = True current = current.next if self.length == self.limit and replaced is not True: self.storage.remove_from_tail() self.storage.add_to_head([key, value]) elif replaced is not True: self.storage.add_to_head([key, value]) self.length = self.storage.length
class LRUCache: def __init__(self, limit=10): self.limit = limit self.entries = {} self.cache = DoublyLinkedList() """ Retrieves the value associated with the given key. Also needs to move the key-value pair to the top of the order such that the pair is considered most-recently used. Returns the value associated with the key or None if the key-value pair doesn't exist in the cache. """ def get(self, key): try: node, value = self.entries[key] self.cache.move_to_front(node) return value except KeyError: return None """ Adds the given key-value pair to the cache. The newly- added pair should be considered the most-recently used entry in the cache. If the cache is already at max capacity before this entry is added, then the oldest entry in the cache needs to be removed to make room. Additionally, in the case that the key already exists in the cache, we simply want to overwrite the old value associated with the key with the newly-specified value. """ def set(self, key, value): try: self.entries[key][1] = value self.cache.move_to_front(self.entries[key][0]) except KeyError: if len(self.entries) == self.limit: key_to_remove = self.cache.remove_from_tail() self.entries.pop(key_to_remove) node = self.cache.add_to_head(key) self.entries[key] = [node, value] """ Method added for testing purposes. Prints all the elements in the cache in their current order. """ def print_cache(self): cache = [] pointer = self.cache.head while pointer: cache.append(pointer.value) pointer = pointer.next print(cache)
class Stack: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? # self.storage = ? self.storage = DoublyLinkedList() def push(self, value): self.size += 1 self.storage.add_to_head(value) def pop(self): if self.len() > 0: self.size -= 1 return self.storage.remove_from_head() else: return None def len(self): return len(self.storage)
class Queue: def __init__(self): self.size = 0 # Why is our DLL a good choice to store our elements? # self.storage = ? self.dll = DoublyLinkedList() def enqueue(self, value): self.dll.add_to_head(value) self.size += 1 def dequeue(self): if self.size == 0: pass else: self.size -= 1 value = self.dll.remove_from_tail() return value def len(self): return self.size