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
promptthe exact instruction sent to the model
view full prompt
You are completing a coding task in one shot. Write complete files that satisfy the spec below. Tests (pytest) will be run against your files; you cannot run them yourself.
Reply with ONE JSON object and nothing else:
{"files": {"<relative path>": "<complete file content>", ...}}
Required deliverable files: lru.py
SPEC:
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.
reply1777 tokens
view raw reply
{"files": {"lru.py": "from collections import OrderedDict\n\nclass LRUCache:\n def __init__(self, capacity: int):\n self.capacity = capacity\n self.cache = OrderedDict()\n \n def get(self, key):\n if key in self.cache:\n self.cache.move_to_end(key)\n return self.cache[key]\n return -1\n \n def put(self, key, value):\n if key in self.cache:\n self.cache[key] = value\n self.cache.move_to_end(key)\n else:\n if len(self.cache) == self.capacity:\n self.cache.popitem(last=False)\n self.cache[key] = value"}}write_filelru.py · 599 B
graded 100.05 passed, 0 failed
pytest output
..... [100%] 5 passed in 0.07s
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 · 599 B
from collections import OrderedDict class LRUCache: def __init__(self, capacity: int): self.capacity = capacity self.cache = OrderedDict() def get(self, key): if key in self.cache: self.cache.move_to_end(key) return self.cache[key] return -1 def put(self, key, value): if key in self.cache: self.cache[key] = value self.cache.move_to_end(key) else: if len(self.cache) == self.capacity: self.cache.popitem(last=False) self.cache[key] = value
04 how it was graded
1
model calls
0
invalid actions
0
self test runs
1.8k
tokens out
—
tokens in
237.8s
wall time
agent actions: —
final pytest output
..... [100%] 5 passed in 0.07s