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.

9 models responded

Claude OpusAnthropic
medium7.4

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: ```python from collections import deque, defaultdict from typing import List, Dict, Set, Tuple, Optional class AhoCorasick:...
anthropic/claude-opus-4-20250514 · 2026-03-01
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): ```python from collections import deque from typin...
openai/o4-mini · 2026-03-02
Claude SonnetAnthropic
medium7.0

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: ```python from typing import Dict, List, Set, Tuple, Optional from collections import deque, defaultdict class...
anthropic/claude-sonnet-4-20250514 · 2026-03-01
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: ```python from collections import deque from typing import List, Dict, Set, Tuple, Optional class TrieNode: ...
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 occur...
openai/gpt-4o · 2026-03-01
Claude HaikuAnthropic
medium5.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 ```python from collections import deque, defaultdict from dataclasses import dataclass from typing import List, Dict, Set, Tuple, Optional @dataclass class ...
anthropic/claude-haiku-4-5-20251001 · 2026-03-01
Phi-4Microsoft
medium4.6

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.
```python 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 = {} ...
ollama/phi4 · 2026-03-01
medium3.5

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-02
high1.0

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-01