Code Generation
Code Generation #5

Write a Python implementation of the Aho-Corasick string matching algorithm. It should accept a list of pattern strings, build the automaton, and provide a `search(text)` method that returns all matches with their positions and which pattern matched. Include type hints, docstrings, and test cases that cover overlapping patterns.

14 models responded

GPT-5.5OpenAI
high10.0

Judge: I ran the code: all 5 unittest cases pass, including heavily overlapping patterns ('a'/'aa'/'aaa' over 'aaaa') and the classic ushers example, with correct positions throughout. It is a textbook Aho-Corasick — trie, BFS failure links, and output propagation through failure targets — with excellent modern type hints, frozen dataclass Match results, comprehensive docstrings, and input validation. Hard to fault.

from future import annotations from collections import deque from dataclasses import dataclass, field from typing import Sequence import unittest @dataclass(frozen=True) class Match: """ A s...
openai/gpt-5.5 · 2026-06-10
GPT-5.4OpenAI
high10.0

Judge: Textbook Aho-Corasick with a trie, BFS-built failure links, and output-link propagation; verified to produce correct overlapping matches with positions for the classic 'ushers' case and heavily overlapping patterns like 'aaaa'. Full type hints, thorough docstrings, clean dataclass node, and tests that explicitly cover overlapping patterns.

from future import annotations from collections import deque from dataclasses import dataclass, field from typing import Dict, List, Tuple @dataclass class Node: """ A node in the Aho-Corasi...
openai/gpt-5.4 · 2026-06-11
Claude FableAnthropic
high10.0

Judge: Executed the code: all 13 of its own pytest tests pass (including a 200-case randomized brute-force cross-check) and an independent 500-case cross-check found zero mismatches. Textbook Aho-Corasick with BFS failure links and merged outputs, full modern type hints, thorough docstrings, and strong overlapping-pattern test coverage. Only blemish is one slightly garbled sentence in the closing notes.

Aho-Corasick String Matching Algorithm """ Aho-Corasick multi-pattern string matching. Builds a trie of patterns augmented with failure links (à la KMP) and output links, allowing all occurrences of...
anthropic/claude-fable-5 · 2026-06-11
medium8.5

Judge: Textbook Aho-Corasick: trie, BFS failure links with output merging into nodes, and a correct search loop; I traced 'ushers' and the 'aaaa' overlap cases and the automaton emits all overlapping matches at correct positions. However, the expected list in test_overlapping_patterns omits Match(1, 2, 'bc') for text 'abccab' (patterns include 'bc'), so the shipped test actually raises AssertionError when run, indicating the tests were not verified. Code quality is high (dataclass Match, __slots__, docstrings), though two type-ignore comments paper over the Optional fail typing.

from future import annotations from collections import deque from dataclasses import dataclass from typing import Dict, Iterable, List, Tuple @dataclass(frozen=True) class Match: """Represents o...
openai/gpt-5.4-mini · 2026-06-10
Claude OpusAnthropic
medium7.4
Run 1 of 2

Judge: Proper Aho-Corasick implementation with trie construction, failure links via BFS, and output sets. Type hints and docstrings present throughout. Uses defaultdict and deque appropriately. Response truncated before failure link completion and test cases, but the architecture is correct and well-structured. The visible code shows proper understanding of the algorithm.

Here's a complete implementation of the Aho-Corasick algorithm in Python: from collections import deque, defaultdict from typing import List, Dict, Set, Tuple, Optional class AhoCorasick: """ ...
anthropic/claude-opus-4-20250514 · 2026-03-01avg: 8.7
o4-miniOpenAI
medium7.4

Judge: Proper Aho-Corasick implementation with trie construction, BFS-based failure links, and output merging. Type hints and docstrings are present on public methods. Test cases cover overlapping patterns (the classic 'he/she/hers/his' example). However, the second test (_test_various) has a questionable approach of filtering expected results at runtime rather than asserting known-correct outputs, and _test_empty tests empty pattern which is an edge case the algorithm doesn't handle well. The output merging via list concatenation (+=) is correct but could accumulate duplicates in pathological cases.

Here’s a self-contained Python module implementing Aho-Corasick with type hints, docstrings, and simple test cases (including overlapping patterns): from collections import deque from typing import D...
openai/o4-mini · 2026-03-02
Claude SonnetAnthropic
medium7.0
Run 1 of 2

Judge: Textbook Aho-Corasick structure with separate trie building, BFS failure links, and output links. Excellent type hints, comprehensive docstrings, and clean class structure with AhoCorasickNode and AhoCorasick classes. Response is truncated before we can see the search method and test cases, but the architecture and code quality visible are strong. The output_link concept shows understanding beyond basic implementations.

Here's a complete Python implementation of the Aho-Corasick string matching algorithm: from typing import Dict, List, Set, Tuple, Optional from collections import deque, defaultdict class AhoCorasic...
anthropic/claude-sonnet-4-20250514 · 2026-03-01avg: 8.2
DeepSeek V4DeepSeek
medium7.0

Judge: The automaton itself is a correct textbook Aho-Corasick (verified: it finds 'he' inside 'she' and reports correct positions), but the shipped program crashes when run — test 2's expected list omits the overlapping (1,2,'he') match and mis-indexes 'he' at (8,9) instead of (9,10), so run_tests() raises AssertionError. Test 4 additionally contains a duplicated check call whose first expected list is filled with confused, visibly wrong entries and stream-of-consciousness comments. Type hints, docstrings, and overlap-covering tests are all present, but delivering a self-failing test suite is a significant correctness and polish defect.

Below is a complete Python implementation of the Aho–Corasick multi-pattern string matching automaton. It builds the trie and failure links efficiently, and the search method returns all matches as (s...
deepseek/deepseek-v4-pro · 2026-06-10
DeepSeek R1DeepSeek
medium6.4

Judge: The implementation correctly builds a trie, constructs failure links via BFS, and merges output sets along failure links -- this is textbook Aho-Corasick. The search method correctly reports matches with positions. Good type hints and comprehensive docstrings. However, the response is truncated mid-method (search_unique is cut off), so we can't verify test cases covering overlapping patterns were included, which is a hard constraint. The algorithm quality of what's visible is strong.

Hard constraint failed: Response is truncated and does not include test cases covering overlapping patterns, which is a required hard constraint.
Here's a comprehensive implementation of the Aho-Corasick string matching algorithm: from collections import deque from typing import List, Dict, Set, Tuple, Optional class TrieNode: """Node for...
deepseek/deepseek-reasoner · 2026-03-02
GPT-4oOpenAI
medium5.8

Judge: Implements Aho-Corasick correctly with trie construction and BFS-based failure link building. Good type hints and docstrings. However, the response is truncated before the search method is complete and the required test cases covering overlapping patterns are missing.

Hard constraint failed: Response truncated -- search method incomplete and required test cases covering overlapping patterns are not present.
Below is a Python implementation of the Aho-Corasick string matching algorithm. This implementation builds the automaton from a list of pattern strings and provides a search method to find all occurre...
openai/gpt-4o · 2026-03-01
Claude HaikuAnthropic
medium5.2
Run 1 of 2

Judge: Correct Aho-Corasick structure with proper goto/fail/output tables, Match dataclass, and BFS-based build. The algorithm design is textbook correct. Truncated during the _build method, so we can't verify the failure link construction or see the search method and test cases. Strong code quality (type hints, docstrings, dataclass).

Hard constraint failed: Code is truncated — search method and test cases for overlapping patterns are missing.
Aho-Corasick String Matching Algorithm from collections import deque, defaultdict from dataclasses import dataclass from typing import List, Dict, Set, Tuple, Optional @dataclass class Match: ""...
anthropic/claude-haiku-4-5-20251001 · 2026-03-01avg: 6.2
Phi-4Microsoft
medium4.6
Run 1 of 2

Judge: Implements genuine Aho-Corasick with trie construction and failure links via BFS. Has type hints and docstrings. However, the response is truncated — the search function is incomplete and no test cases are visible, which means the hard constraint for test cases covering overlapping patterns is likely violated. The algorithm structure is correct (failure links propagation, output link merging) but the BFS initialization has a subtle issue: root's children should have fail=root, but the code starts BFS from root itself which could cause issues.

Hard constraint failed: Must include test cases covering overlapping patterns — response is truncated before test cases.
from collections import deque from typing import List, Tuple class ACNode: """A node in the Aho-Corasick trie.""" def init(self) -> None: self.go: dict = {} self.out: list = ...
ollama/phi4 · 2026-03-01avg: 3.3
medium3.5
Run 1 of 2

Judge: Response describes Aho-Corasick correctly (trie, failure links, output links, BFS construction) and starts coding with type hints and docstrings. However, the code is truncated after the ACNode class __repr__ method — no complete automaton builder, no search method, no test cases. The algorithmic understanding is solid but the deliverable is incomplete.

Hard constraint failed: Code is truncated and incomplete. No search method or test cases provided.
The Aho-Corasick algorithm is an efficient string matching algorithm that finds all occurrences of a finite set of patterns (a "dictionary") in a text. It constructs a finite state automaton from the ...
gemini/gemini-2.5-flash · 2026-03-02avg: 5.2
high1.0
Run 1 of 2

Judge: The implementation is fundamentally broken. The automaton construction logic is wrong -- it creates nested dicts incorrectly and doesn't build a proper trie, failure links, or output links. The response is truncated mid-function, cutting off in the _search method's docstring. No test cases are present at all. Has the <|python_tag|> artifact.

Hard constraint failed: Must include test cases covering overlapping patterns -- no test cases present. Also, the implementation is not a valid Aho-Corasick algorithm (no failure links, no proper trie).
<|python_tag|>from typing import Dict, List, Tuple class AhoCorasick: """ An implementation of the Aho-Corasick string matching algorithm. Attributes: patterns (List[str]): The l...
ollama/llama3.1:8b · 2026-03-01avg: 0.9