py-14-regex-engine
0.158
Proposed solution
```python
def fullmatch(pattern: str, text: str) -> bool:
"""Return True iff `pattern` matches the ENTIRE `text`."""
def match_char(pat, txt):
return pat == '.' or pat == txt
def match_class(pat, txt):
if not pat.startswith('[') or not pat.endswith(']'):
return False
negated = pat[1] == '^'
if negated:
pat = pat[2:]
else:
pat = pat[1:]
match = pat[0] == '-'
if match:
pat = pat[1:]
if pat == '':
return not negated
if pat[0] == ']':
return False
if match:
start = pat[0]
end = pat[1]
if start == end:
return negated == (txt[0] == start)
else:
return start <= txt[0] <= end
else:
return txt[0] in pat[1:-1]
def match_star(pat, txt):
if not pat.startswith('*'):
return False
pat = pat[1:]
if not pat.startswith('?'):
return match(pat, txt)
if pat.startswith('?'):
pat = pat[1:]
if not pat.startswith('+'):
return match(pat, txt) or match(pat, txt[1:])
if pat.startswith('+'):
pat = pat[1:]
while txt and match(pat, txt[0]):
if fullmatch(pat, txt[1:]):
return True
return False
def match_plus(pat, txt):
if not pat.startswith('+'):
return False
pat = pat[1:]
if not pat.startswith('?'):
return match(pat, txt)
if pat.startswith('?'):
pat = pat[1:]
if not pat.startswith('*'):
return match(pat, txt) or match(pat, txt[1:])
if pat.startswith('*'):
pat = pat[1:]
while txt and match(pat, txt[0]):
if fullmatch(pat, txt[1:]):
return True
return False
def match_question(pat, txt):
if not pat.startswith('?'):
return match(pat, txt)
pat = pat[1:]
if not pat.startswith('*'):
return match(pat, txt) or match(pat, txt[1:])
if pat.startswith('*'):
pat = pat[1:]
while txt and match(pat, txt[0]):
if fullmatch(pat, txt[1:]):
return True
return False
def match_element(pat, txt):
if pat.startswith('\\'):
return match_char(pat[1:], txt[0])
if pat.startswith('['):
return match_class(pat, txt[0])
if pat.startswith('*'):
return match_star(pat, txt)
if pat.startswith('+'):
return match_plus(pat, txt)
if pat.startswith('?'):
return match_question(pat, txt)
return match_char(pat, txt)
if not pattern or not text:
return not pattern and not text
if pattern[0] == '^':
return match(pattern[1:], text) and not text
if pattern[-1] == '$':
return match(pattern[:-1], text) and not text
if not text:
return False
for i in range(len(text)):
if match_element(pattern, text[i:]):
if len(pattern) == len(text[i:]) or (len(pattern) < len(text[i:]) and pattern[-1] in '*+?'):
if fullmatch(pattern, text[i+1:]):
return True
return False
```Test output (stdout)
___________________
def test_char_class_basic():
> assert fullmatch("[abc]", "a") is True
E AssertionError: assert False is True
E + where False = fullmatch('[abc]', 'a')
test_regex_engine.py:94: AssertionError
____________________________ test_char_class_ranges ____________________________
def test_char_class_ranges():
> assert fullmatch("[a-z]", "m") is True
E AssertionError: assert False is True
E + where False = fullmatch('[a-z]', 'm')
test_regex_engine.py:103: AssertionError
_______________________ test_char_class_with_quantifiers _______________________
def test_char_class_with_quantifiers():
> assert fullmatch("[a-z]+", "hello") is True
E AssertionError: assert False is True
E + where False = fullmatch('[a-z]+', 'hello')
test_regex_engine.py:114: AssertionError
___________________________ test_negated_char_class ____________________________
def test_negated_char_class():
> assert fullmatch("[^0-9]", "a") is True
E AssertionError: assert False is True
E + where False = fullmatch('[^0-9]', 'a')
test_regex_engine.py:123: AssertionError
__________________________ test_literal_dash_in_class __________________________
def test_literal_dash_in_class():
# '-' first or last in the class is a literal dash
> assert fullmatch("[-a]", "-") is True
E AssertionError: assert False is True
E + where False = fullmatch('[-a]', '-')
test_regex_engine.py:133: AssertionError
_________________________ test_escaped_metacharacters __________________________
def test_escaped_metacharacters():
> assert fullmatch("h\\.t", "h.t") is True
E AssertionError: assert False is True
E + where False = fullmatch('h\\.t', 'h.t')
test_regex_engine.py:144: AssertionError
_____________________ test_escaped_element_with_quantifier _____________________
def test_escaped_element_with_quantifier():
# quantifier applies to the escaped element
> assert fullmatch("\\.*", "") is True
E AssertionError: assert False is True
E + where False = fullmatch('\\.*', '')
test_regex_engine.py:156: AssertionError
_______________________ test_combined_realistic_patterns _______________________
def test_combined_realistic_patterns():
# identifier-ish: letter then letters/digits/underscore-via-class
> assert fullmatch("[a-z][a-z0-9]*", "x") is True
E AssertionError: assert False is True
E + where False = fullmatch('[a-z][a-z0-9]*', 'x')
test_regex_engine.py:165: AssertionError
=========================== short test summary info ============================
FAILED test_regex_engine.py::test_plain_literals - AssertionError: assert Fal...
FAILED test_regex_engine.py::test_dot_matches_any_single_char - AssertionErro...
FAILED test_regex_engine.py::test_star_zero_or_more - AssertionError: assert ...
FAILED test_regex_engine.py::test_plus_one_or_more - AssertionError: assert F...
FAILED test_regex_engine.py::test_question_zero_or_one - AssertionError: asse...
FAILED test_regex_engine.py::test_greedy_backtracking_star - AssertionError: ...
FAILED test_regex_engine.py::test_dotstar_matches_anything - AssertionError: ...
FAILED test_regex_engine.py::test_dotstar_backtracking_with_suffix - Assertio...
FAILED test_regex_engine.py::test_char_class_basic - AssertionError: assert F...
FAILED test_regex_engine.py::test_char_class_ranges - AssertionError: assert ...
FAILED test_regex_engine.py::test_char_class_with_quantifiers - AssertionErro...
FAILED test_regex_engine.py::test_negated_char_class - AssertionError: assert...
FAILED test_regex_engine.py::test_literal_dash_in_class - AssertionError: ass...
FAILED test_regex_engine.py::test_escaped_metacharacters - AssertionError: as...
FAILED test_regex_engine.py::test_escaped_element_with_quantifier - Assertion...
FAILED test_regex_engine.py::test_combined_realistic_patterns - AssertionErro...
16 failed, 3 passed in 0.06s
Self-repair attempts
[
{
"total": 19,
"answer": "```python\ndef fullmatch(pattern: str, text: str) -> bool:\n \"\"\"Return True iff `pattern` matches the ENTIRE `text`.\"\"\"\n \n def match_char(pat, txt):\n return pat == '.' or pat == txt\n\n def match_class(pat, txt):\n if not pat.startswith('[') or not pat.endswith(']'):\n return False\n negated = pat[1] == '^'\n if negated:\n pat = pat[2:]\n else:\n pat = pat[1:]\n match = pat[0] == '-'\n if match:\n pat = pat[1:]\n if pat == '':\n return not negated\n if pat[0] == ']':\n return False\n if match:\n start = pat[0]\n end = pat[1]\n if start == end:\n return negated == (txt[0] == start)\n else:\n return start <= txt[0] <= end\n else:\n return txt[0] in pat[1:-1]\n\n def match_star(pat, txt):\n if not pat.startswith('*'):\n return False\n pat = pat[1:]\n if not pat.startswith('?'):\n return match(pat, txt)\n if pat.startswith('?'):\n pat = pat[1:]\n if not pat.startswith('+'):\n return match(pat, txt) or match(pat, txt[1:])\n if pat.startswith('+'):\n pat = pat[1:]\n while txt and match(pat, txt[0]):\n if fullmatch(pat, txt[1:]):\n return True\n return False\n\n def match_plus(pat, txt):\n if not pat.startswith('+'):\n return False\n pat = pat[1:]\n if not pat.startswith('?'):\n return match(pat, txt)\n if pat.startswith('?'):\n pat = pat[1:]\n if not pat.startswith('*'):\n return match(pat, txt) or match(pat, txt[1:])\n if pat.startswith('*'):\n pat = pat[1:]\n while txt and match(pat, txt[0]):\n if fullmatch(pat, txt[1:]):\n return True\n return False\n\n def match_question(pat, txt):\n if not pat.startswith('?'):\n return match(pat, txt)\n pat = pat[1:]\n if not pat.startswith('*'):\n return match(pat, txt) or match(pat, txt[1:])\n if pat.startswith('*'):\n pat = pat[1:]\n while txt and match(pat, txt[0]):\n if fullmatch(pat, txt[1:]):\n return True\n return False\n\n def match_element(pat, txt):\n if pat.startswith('\\\\'):\n return match_char(pat[1:], txt[0])\n if pat.startswith('['):\n return match_class(pat, txt[0])\n if pat.startswith('*'):\n return match_star(pat, txt)\n if pat.startswith('+'):\n return match_plus(pat, txt)\n if pat.startswith('?'):\n return match_question(pat, txt)\n return match_char(pat, txt)\n\n if not pattern or not text:\n return not pattern and not text\n if pattern[0] == '^':\n return match(pattern[1:], text) and not text\n if pattern[-1] == '$':\n return match(pattern[:-1], text) and not text\n if not text:\n return False\n\n for i in range(len(text)):\n if match_element(pattern, text[i:]):\n if len(pattern) == len(text[i:]) or (len(pattern) < len(text[i:]) and pattern[-1] in '*+?'):\n if fullmatch(pattern, text[i+1:]):\n return True\n return False\n```",
"passed": 3,
"test_error": "y:133: AssertionError\n_________________________ test_escaped_metacharacters __________________________\n\n def test_escaped_metacharacters():\n> assert fullmatch(\"h\\\\.t\", \"h.t\") is True\nE AssertionError: assert False is True\nE + where False = fullmatch('h\\\\.t', 'h.t')\n\ntest_regex_engine.py:144: AssertionError\n_____________________ test_escaped_element_with_quantifier _____________________\n\n def test_escaped_element_with_quantifier():\n # quantifier applies to the escaped element\n> assert fullmatch(\"\\\\.*\", \"\") is True\nE AssertionError: assert False is True\nE + where False = fullmatch('\\\\.*', '')\n\ntest_regex_engine.py:156: AssertionError\n_______________________ test_combined_realistic_patterns _______________________\n\n def test_combined_realistic_patterns():\n # identifier-ish: letter then letters/digits/underscore-via-class\n> assert fullmatch(\"[a-z][a-z0-9]*\", \"x\") is True\nE AssertionError: assert False is True\nE + where False = fullmatch('[a-z][a-z0-9]*', 'x')\n\ntest_regex_engine.py:165: AssertionError\n=========================== short test summary info ============================\nFAILED test_regex_engine.py::test_plain_literals - AssertionError: assert Fal...\nFAILED test_regex_engine.py::test_dot_matches_any_single_char - AssertionErro...\nFAILED test_regex_engine.py::test_star_zero_or_more - AssertionError: assert ...\nFAILED test_regex_engine.py::test_plus_one_or_more - AssertionError: assert F...\nFAILED test_regex_engine.py::test_question_zero_or_one - AssertionError: asse...\nFAILED test_regex_engine.py::test_greedy_backtracking_star - AssertionError: ...\nFAILED test_regex_engine.py::test_dotstar_matches_anything - AssertionError: ...\nFAILED test_regex_engine.py::test_dotstar_backtracking_with_suffix - Assertio...\nFAILED test_regex_engine.py::test_char_class_basic - AssertionError: assert F...\nFAILED test_regex_engine.py::test_char_class_ranges - AssertionError: assert ...\nFAILED test_regex_engine.py::test_char_class_with_quantifiers - AssertionErro...\nFAILED test_regex_engine.py::test_negated_char_class - AssertionError: assert...\nFAILED test_regex_engine.py::test_literal_dash_in_class - AssertionError: ass...\nFAILED test_regex_engine.py::test_escaped_metacharacters - AssertionError: as...\nFAILED test_regex_engine.py::test_escaped_element_with_quantifier - Assertion...\nFAILED test_regex_engine.py::test_combined_realistic_patterns - AssertionErro...\n16 failed, 3 passed in 0.06s"
},
{
"total": 19,
"answer": "```python\ndef fullmatch(pattern: str, text: str) -> bool:\n \"\"\"Return True iff `pattern` matches the ENTIRE `text`.\"\"\"\n \n def match_char(pat, txt):\n return pat == '.' or pat == txt\n\n def match_class(pat, txt):\n if not pat.startswith('[') or not pat.endswith(']'):\n return False\n negated = pat[1] == '^'\n if negated:\n pat = pat[2:]\n else:\n pat = pat[1:]\n match = pat[0] == '-'\n if match:\n pat = pat[1:]\n if pat == '':\n return not negated\n if pat[0] == ']':\n return False\n if match:\n start = pat[0]\n end = pat[1]\n if start == end:\n return negated == (txt[0] == start)\n else:\n return start <= txt[0] <= end\n else:\n return txt[0] in pat[1:-1]\n\n def match_star(pat, txt):\n if not pat.startswith('*'):\n return False\n pat = pat[1:]\n if not pat.startswith('?'):\n return match(pat, txt)\n if pat.startswith('?'):\n pat = pat[1:]\n if not pat.startswith('+'):\n return match(pat, txt) or match(pat, txt[1:])\n if pat.startswith('+'):\n pat = pat[1:]\n while txt and match(pat, txt[0]):\n if fullmatch(pat, txt[1:]):\n return True\n return False\n\n def match_plus(pat, txt):\n if not pat.startswith('+'):\n return False\n pat = pat[1:]\n if not pat.startswith('?'):\n return match(pat, txt)\n if pat.startswith('?'):\n pat = pat[1:]\n if not pat.startswith('*'):\n return match(pat, txt) or match(pat, txt[1:])\n if pat.startswith('*'):\n pat = pat[1:]\n while txt and match(pat, txt[0]):\n if fullmatch(pat, txt[1:]):\n return True\n return False\n\n def match_question(pat, txt):\n if not pat.startswith('?'):\n return match(pat, txt)\n pat = pat[1:]\n if not pat.startswith('*'):\n return match(pat, txt) or match(pat, txt[1:])\n if pat.startswith('*'):\n pat = pat[1:]\n while txt and match(pat, txt[0]):\n if fullmatch(pat, txt[1:]):\n return True\n return False\n\n def match_element(pat, txt):\n if pat.startswith('\\\\'):\n return match_char(pat[1:], txt[0])\n if pat.startswith('['):\n return match_class(pat, txt[0])\n if pat.startswith('*'):\n return match_star(pat, txt)\n if pat.startswith('+'):\n return match_plus(pat, txt)\n if pat.startswith('?'):\n return match_question(pat, txt)\n return match_char(pat, txt)\n\n if not pattern or not text:\n return not pattern and not text\n if pattern[0] == '^':\n return match(pattern[1:], text) and not text\n if pattern[-1] == '$':\n return match(pattern[:-1], text) and not text\n if not text:\n return False\n\n for i in range(len(text)):\n if match_element(pattern, text[i:]):\n if len(pattern) == len(text[i:]) or (len(pattern) < len(text[i:]) and pattern[-1] in '*+?'):\n if fullmatch(pattern, text[i+1:]):\n return True\n return False\n```",
"passed": 3,
"test_error": "y:133: AssertionError\n_________________________ test_escaped_metacharacters __________________________\n\n def test_escaped_metacharacters():\n> assert fullmatch(\"h\\\\.t\", \"h.t\") is True\nE AssertionError: assert False is True\nE + where False = fullmatch('h\\\\.t', 'h.t')\n\ntest_regex_engine.py:144: AssertionError\n_____________________ test_escaped_element_with_quantifier _____________________\n\n def test_escaped_element_with_quantifier():\n # quantifier applies to the escaped element\n> assert fullmatch(\"\\\\.*\", \"\") is True\nE AssertionError: assert False is True\nE + where False = fullmatch('\\\\.*', '')\n\ntest_regex_engine.py:156: AssertionError\n_______________________ test_combined_realistic_patterns _______________________\n\n def test_combined_realistic_patterns():\n # identifier-ish: letter then letters/digits/underscore-via-class\n> assert fullmatch(\"[a-z][a-z0-9]*\", \"x\") is True\nE AssertionError: assert False is True\nE + where False = fullmatch('[a-z][a-z0-9]*', 'x')\n\ntest_regex_engine.py:165: AssertionError\n=========================== short test summary info ============================\nFAILED test_regex_engine.py::test_plain_literals - AssertionError: assert Fal...\nFAILED test_regex_engine.py::test_dot_matches_any_single_char - AssertionErro...\nFAILED test_regex_engine.py::test_star_zero_or_more - AssertionError: assert ...\nFAILED test_regex_engine.py::test_plus_one_or_more - AssertionError: assert F...\nFAILED test_regex_engine.py::test_question_zero_or_one - AssertionError: asse...\nFAILED test_regex_engine.py::test_greedy_backtracking_star - AssertionError: ...\nFAILED test_regex_engine.py::test_dotstar_matches_anything - AssertionError: ...\nFAILED test_regex_engine.py::test_dotstar_backtracking_with_suffix - Assertio...\nFAILED test_regex_engine.py::test_char_class_basic - AssertionError: assert F...\nFAILED test_regex_engine.py::test_char_class_ranges - AssertionError: assert ...\nFAILED test_regex_engine.py::test_char_class_with_quantifiers - AssertionErro...\nFAILED test_regex_engine.py::test_negated_char_class - AssertionError: assert...\nFAILED test_regex_engine.py::test_literal_dash_in_class - AssertionError: ass...\nFAILED test_regex_engine.py::test_escaped_metacharacters - AssertionError: as...\nFAILED test_regex_engine.py::test_escaped_element_with_quantifier - Assertion...\nFAILED test_regex_engine.py::test_combined_realistic_patterns - AssertionErro...\n16 failed, 3 passed in 0.06s"
},
{
"total": 19,
"answer": "```python\ndef fullmatch(pattern: str, text: str) -> bool:\n \"\"\"Return True iff `pattern` matches the ENTIRE `text`.\"\"\"\n \n def match_char(pat, txt):\n return pat == '.' or pat == txt\n\n def match_class(pat, txt):\n if not pat.startswith('[') or not pat.endswith(']'):\n return False\n negated = pat[1] == '^'\n if negated:\n pat = pat[2:]\n else:\n pat = pat[1:]\n match = pat[0] == '-'\n if match:\n pat = pat[1:]\n if pat == '':\n return not negated\n if pat[0] == ']':\n return False\n if match:\n start = pat[0]\n end = pat[1]\n if start == end:\n return negated == (txt[0] == start)\n else:\n return start <= txt[0] <= end\n else:\n return txt[0] in pat[1:-1]\n\n def match_star(pat, txt):\n if not pat.startswith('*'):\n return False\n pat = pat[1:]\n if not pat.startswith('?'):\n return match(pat, txt)\n if pat.startswith('?'):\n pat = pat[1:]\n if not pat.startswith('+'):\n return match(pat, txt) or match(pat, txt[1:])\n if pat.startswith('+'):\n pat = pat[1:]\n while txt and match(pat, txt[0]):\n if fullmatch(pat, txt[1:]):\n return True\n return False\n\n def match_plus(pat, txt):\n if not pat.startswith('+'):\n return False\n pat = pat[1:]\n if not pat.startswith('?'):\n return match(pat, txt)\n if pat.startswith('?'):\n pat = pat[1:]\n if not pat.startswith('*'):\n return match(pat, txt) or match(pat, txt[1:])\n if pat.startswith('*'):\n pat = pat[1:]\n while txt and match(pat, txt[0]):\n if fullmatch(pat, txt[1:]):\n return True\n return False\n\n def match_question(pat, txt):\n if not pat.startswith('?'):\n return match(pat, txt)\n pat = pat[1:]\n if not pat.startswith('*'):\n return match(pat, txt) or match(pat, txt[1:])\n if pat.startswith('*'):\n pat = pat[1:]\n while txt and match(pat, txt[0]):\n if fullmatch(pat, txt[1:]):\n return True\n return False\n\n def match_element(pat, txt):\n if pat.startswith('\\\\'):\n return match_char(pat[1:], txt[0])\n if pat.startswith('['):\n return match_class(pat, txt[0])\n if pat.startswith('*'):\n return match_star(pat, txt)\n if pat.startswith('+'):\n return match_plus(pat, txt)\n if pat.startswith('?'):\n return match_question(pat, txt)\n return match_char(pat, txt)\n\n if not pattern or not text:\n return not pattern and not text\n if pattern[0] == '^':\n return match(pattern[1:], text) and not text\n if pattern[-1] == '$':\n return match(pattern[:-1], text) and not text\n if not text:\n return False\n\n for i in range(len(text)):\n if match_element(pattern, text[i:]):\n if len(pattern) == len(text[i:]) or (len(pattern) < len(text[i:]) and pattern[-1] in '*+?'):\n if fullmatch(pattern, text[i+1:]):\n return True\n return False\n```",
"passed": 3,
"test_error": ""
}
]System prompt
You are an expert programmer. Solve the task exactly as specified. Output your solution as fenced code blocks using the required file name(s) and the exact function/type signatures requested. Prefer correctness; do not include prose outside code unless asked.