Last active
May 4, 2022 07:11
-
-
Save CCXXXI/13e61e63a40a4f48607c77ed7a77c654 to your computer and use it in GitHub Desktop.
To check my automata homework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
from itertools import product | |
from random import sample | |
import pytest | |
from automata.fa.dfa import DFA | |
dfa = DFA( | |
states=set("pqrs"), | |
input_symbols=set("01"), | |
transitions={ | |
"p": {"0": "s", "1": "p"}, | |
"q": {"0": "p", "1": "s"}, | |
"r": {"0": "r", "1": "q"}, | |
"s": {"0": "q", "1": "r"}, | |
}, | |
initial_state="p", | |
final_states={"p"}, | |
) | |
dfa_min = dfa.minify(False) | |
p = re.compile(r"(1|(00|010*1)(10|110*1)*0)*") | |
@pytest.mark.parametrize( | |
"s", | |
( | |
"".join(it) | |
for i in range(16) | |
for it in sample(tuple(product("01", repeat=i)), min(2**i, 16)) | |
), | |
) | |
def test_dfa(s: str): | |
assert ( | |
dfa.accepts_input(s) == dfa_min.accepts_input(s) == (p.fullmatch(s) is not None) | |
) | |
if __name__ == "__main__": | |
dfa.show_diagram("dfa.png") | |
dfa_min.show_diagram("dfa_min.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment