LRU cache
← run 2 · raw JSON · challenge definitions
100
score
tests (60%)5/5 passed
deliverables (20%)100%
content checks (20%)100%
01 what the model was asked
Create lru.py with a class:
class LRUCache:
def __init__(self, capacity: int)
def get(self, key) -> value # return the stored value, or -1 if absent
def put(self, key, value) -> None # insert or update
Semantics: the cache holds at most `capacity` entries. Both get() and put() count as a use of the key. When put() would exceed capacity, evict the least-recently-used key first. put() on an existing key updates its value and makes it most-recently-used.
Use only the Python standard library.
required deliverables + checks
✓deliverable: lru.py
✓lru.py must contain “class LRUCache”
✓lru.py must contain “def get”
✓lru.py must contain “def put”
pytest suite it was graded against
test_lru.py · 46 lines · 842 B
from lru import LRUCache def test_basic_put_get(): c = LRUCache(2) c.put("a", 1) assert c.get("a") == 1 assert c.get("missing") == -1 def test_eviction_order(): c = LRUCache(2) c.put("a", 1) c.put("b", 2) c.put("c", 3) assert c.get("a") == -1 assert c.get("b") == 2 assert c.get("c") == 3 def test_get_refreshes_recency(): c = LRUCache(2) c.put("a", 1) c.put("b", 2) c.get("a") c.put("c", 3) assert c.get("b") == -1 assert c.get("a") == 1 def test_put_overwrite_refreshes(): c = LRUCache(2) c.put("a", 1) c.put("b", 2) c.put("a", 10) c.put("c", 3) assert c.get("b") == -1 assert c.get("a") == 10 def test_capacity_one(): c = LRUCache(1) c.put("a", 1) c.put("b", 2) assert c.get("a") == -1 assert c.get("b") == 2
02 what it did — full transcript
write_fileturn 1 · lru.py · 590 B
run_teststurn 2 ·
✓ tests green5 passed
doneturn 3 ·
✓ done — gate green
graded 100.05 passed, 0 failed
pytest output
..... [100%] 5 passed in 0.01s
03 what it wrote
1
files
21
lines
18
source lines
3
functions
1
classes
4
cyclomatic
4
max nesting
5.3
avg fn lines
stdlib imports: collections
| File | LOC | SLOC | Fns | Complexity | Depth | Imports |
|---|---|---|---|---|---|---|
| lru.py | 21 | 18 | 3 | 4 | 4 | collections |
lru.py · 21 lines · 590 B
from collections import OrderedDict class LRUCache: def __init__(self, capacity: int): self.capacity = capacity self.cache = OrderedDict() def get(self, key): if key not in self.cache: return -1 self.cache.move_to_end(key) return self.cache[key] def put(self, key, value): if key in self.cache: self.cache[key] = value self.cache.move_to_end(key) else: self.cache[key] = value if len(self.cache) > self.capacity: self.cache.popitem(last=False)
04 how it was graded
3
model calls
0
invalid actions
1
self test runs
3.5k
tokens out
—
tokens in
336.4s
wall time
agent actions: write_file×1, run_tests×1, done×1
final pytest output
..... [100%] 5 passed in 0.01s