Word ladder (shortest path)
← 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 ladder.py with exactly one function:
ladder_length(begin: str, end: str, word_list: list[str]) -> int
A transformation sequence goes from begin to end, changing exactly one letter per step; every intermediate word (and end itself) must appear in word_list. Return the number of words in the SHORTEST such sequence counting both endpoints, or 0 if none exists. begin does not need to be in word_list. All words are lowercase and the same length.
Example: begin="hit", end="cog", word_list=["hot","dot","dog","lot","log","cog"] -> 5 (hit -> hot -> dot -> dog -> cog).
Use only the Python standard library. A breadth-first search is the intended approach; brute-force recursion will be too slow for the larger test.
required deliverables + checks
✓deliverable: ladder.py
✓ladder.py must contain “def ladder_length”
pytest suite it was graded against
test_ladder.py · 27 lines · 744 B
import time from ladder import ladder_length def test_classic_example(): assert ladder_length("hit", "cog", ["hot", "dot", "dog", "lot", "log", "cog"]) == 5 def test_unreachable_end(): assert ladder_length("hit", "cog", ["hot", "dot", "dog", "lot", "log"]) == 0 def test_single_step(): assert ladder_length("a", "c", ["a", "b", "c"]) == 2 def test_shortest_is_found(): words = ["hot", "hit", "hat", "cat", "cot", "cog", "dog"] assert ladder_length("hit", "cot", words) == 3 def test_larger_input_is_fast(): words = [a + b + c for a in "abcdefgh" for b in "abcdefgh" for c in "abcdefgh"] start = time.monotonic() assert ladder_length("aaa", "hhh", words) == 4 assert time.monotonic() - start < 2.0
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: ladder.py
SPEC:
Create ladder.py with exactly one function:
ladder_length(begin: str, end: str, word_list: list[str]) -> int
A transformation sequence goes from begin to end, changing exactly one letter per step; every intermediate word (and end itself) must appear in word_list. Return the number of words in the SHORTEST such sequence counting both endpoints, or 0 if none exists. begin does not need to be in word_list. All words are lowercase and the same length.
Example: begin="hit", end="cog", word_list=["hot","dot","dog","lot","log","cog"] -> 5 (hit -> hot -> dot -> dog -> cog).
Use only the Python standard library. A breadth-first search is the intended approach; brute-force recursion will be too slow for the larger test.
reply196 tokens
view raw reply
{"files": {"ladder.py": "import collections\nfrom typing import List\n\ndef ladder_length(begin: str, end: str, word_list: List[str]) -> int:\n if begin not in word_list and end not in word_list:\n return 0\n word_set = set(word_list)\n queue = [(begin, 1)]\n visited = {begin}\n while queue:\n word, step = queue.pop(0)\n for i in range(len(word)):\n for char in 'abcdefghijklmnopqrstuvwxyz':\n new_word = word[:i] + char + word[i+1:] \n if new_word not in visited and new_word in word_set:\n if new_word == end:\n return step + 1\n queue.append((new_word, step + 1))\n visited.add(new_word)\n return 0"}}write_fileladder.py · 719 B
graded 100.05 passed, 0 failed
pytest output
..... [100%] 5 passed in 0.01s
03 what it wrote
1
files
20
lines
19
source lines
1
functions
0
classes
9
cyclomatic
6
max nesting
17.0
avg fn lines
stdlib imports: collections, typing
| File | LOC | SLOC | Fns | Complexity | Depth | Imports |
|---|---|---|---|---|---|---|
| ladder.py | 20 | 19 | 1 | 9 | 6 | collections, typing |
ladder.py · 20 lines · 719 B
import collectionsfrom typing import List def ladder_length(begin: str, end: str, word_list: List[str]) -> int: if begin not in word_list and end not in word_list: return 0 word_set = set(word_list) queue = [(begin, 1)] visited = {begin} while queue: word, step = queue.pop(0) for i in range(len(word)): for char in 'abcdefghijklmnopqrstuvwxyz': new_word = word[:i] + char + word[i+1:] if new_word not in visited and new_word in word_set: if new_word == end: return step + 1 queue.append((new_word, step + 1)) visited.add(new_word) return 0
04 how it was graded
1
model calls
0
invalid actions
0
self test runs
196
tokens out
—
tokens in
8.7s
wall time
agent actions: —
final pytest output
..... [100%] 5 passed in 0.01s