Guest User

LRUCache

a guest
Apr 25th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | None | 0 0
  1. from collections import OrderedDict
  2.  
  3. class LRUCache:
  4.     def __init__(self, capacity):
  5.         self.cache = OrderedDict()
  6.         self.capacity = capacity
  7.  
  8.     def get(self, key):
  9.         if key in self.cache:
  10.             self.cache.move_to_end(key)
  11.         return self.cache.get(key, -1)
  12.        
  13.     def put(self, key, value):
  14.         if key in self.cache:
  15.             self.cache.move_to_end(key)
  16.         else:
  17.             if(len(self.cache) >= self.capacity):
  18.                 self.cache.popitem(last=False)
  19.         self.cache[key] = value
Add Comment
Please, Sign In to add comment