Files
training-cg/lru_cache.py
T
2026-06-19 17:56:27 +02:00

33 lines
720 B
Python

class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.slots = []
def __repr__(self):
return ", ".join(map(lambda x: str(x), self.slots))
def get(self, key: int) -> int:
if key <= len(self.slots) -1:
return self.slots[key]
return -1
def put(self, key: int, value: int) -> None:
if self.capacity > len(self.slots):
self.slots.append(value)
else:
self.slots[len(self.slots) - 1] = value
def main():
cache = LRUCache(2)
print(cache.get(1))
print(cache.get(0))
cache.put(0,1)
print(cache)
cache.put(1,2)
print(cache)
if __name__ == "__main__":
main()