From cc0e65af0f1cf18aeebbb3d649c27ed810a446e4 Mon Sep 17 00:00:00 2001 From: TreshMom <113770358+TreshMom@users.noreply.github.com> Date: Sat, 2 Mar 2024 15:46:39 +0300 Subject: [PATCH] Revert "Task 2" --- project/__init__.py | 8 +---- project/task_2.py | 33 -------------------- tests/test_task_2.py | 73 -------------------------------------------- 3 files changed, 1 insertion(+), 113 deletions(-) delete mode 100644 project/task_2.py delete mode 100644 tests/test_task_2.py diff --git a/project/__init__.py b/project/__init__.py index 210448df0..0db7b35dd 100644 --- a/project/__init__.py +++ b/project/__init__.py @@ -1,9 +1,3 @@ -from .task_2 import regex_to_min_dfa, create_nfa from .task_1 import graph_info, create_labeled_two_cycle_graph -__all__ = [ - "graph_info", - "create_labeled_two_cycle_graph", - "regex_to_min_dfa", - "create_nfa", -] +__all__ = ["graph_info", "create_labeled_two_cycle_graph"] diff --git a/project/task_2.py b/project/task_2.py deleted file mode 100644 index 50506ea1f..000000000 --- a/project/task_2.py +++ /dev/null @@ -1,33 +0,0 @@ -from pyformlang.regular_expression import * -from pyformlang.finite_automaton import * -from networkx import MultiDiGraph -from typing import Set - - -def regex_to_min_dfa(regex: str) -> DeterministicFiniteAutomaton: - return Regex(regex).to_epsilon_nfa().minimize() - - -def create_nfa( - graph: MultiDiGraph, start_states: Set[int] = None, final_states: Set[int] = None -) -> NondeterministicFiniteAutomaton: - - nfa = NondeterministicFiniteAutomaton() - - for edge in graph.edges(data=True): - nfa.add_transition(State(edge[0]), Symbol(edge[2]["label"]), State(edge[1])) - - if start_states is None and final_states is None: - for state in nfa.states: - nfa.add_start_state(state) - nfa.add_final_state(state) - - if start_states is not None: - for state in start_states: - nfa.add_start_state(State(state)) - - if final_states is not None: - for state in final_states: - nfa.add_final_state(State(state)) - - return nfa diff --git a/tests/test_task_2.py b/tests/test_task_2.py deleted file mode 100644 index 73d2c0ce7..000000000 --- a/tests/test_task_2.py +++ /dev/null @@ -1,73 +0,0 @@ -import pytest -from pyformlang.regular_expression import Regex -from networkx import MultiDiGraph -from pyformlang.finite_automaton import State, Symbol -from project import regex_to_min_dfa, create_nfa -import random -import itertools - - -@pytest.fixture -def sample_graph(): - graph = MultiDiGraph() - graph.add_edge(0, 1, label="a") - graph.add_edge(1, 2, label="b") - graph.add_edge(2, 3, label="c") - graph.add_edge(3, 4, label="d") - return graph - - -REGEX_TO_TEST = [ - "(aa)*", - "a | a", - "a* | a", - "(ab) | (ac)", - "(ab) | (abc)", - "(abd) | (abc)", - "(abd*) | (abc*)", - "(abd)* | (abc)*", - "((abd) | (abc))*", - "a*a*", - "a*a*b", - "a* | (a | b)*", - "a*(a | b)*", - "(a | c)*(a | b)*", -] - - -class TestRegexToDfa: - @pytest.mark.parametrize("regex_str", REGEX_TO_TEST, ids=lambda regex: regex) - def test(self, regex_str: str) -> None: - regex = Regex(regex_str) - regex_cfg = regex.to_cfg() - regex_words = regex_cfg.get_words() - - if regex_cfg.is_finite(): - all_word_parts = list(regex_words) - word_parts = random.choice(all_word_parts) - else: - index = random.randint(0, 2**9) - word_parts = next(itertools.islice(regex_words, index, None)) - - word = list(map(lambda x: x.value, word_parts)) - - dfa = regex_to_min_dfa(regex_str) - - minimized_dfa = dfa.minimize() - assert dfa.is_deterministic() - assert dfa.is_equivalent_to(minimized_dfa) - assert dfa.accepts(word) - - -def test_create_nfa(sample_graph): - start_states = {0} - final_states = {4} - nfa = create_nfa(sample_graph, start_states, final_states) - assert all(isinstance(state, State) for state in nfa.states) - assert all(isinstance(symbol, Symbol) for symbol in nfa.symbols) - assert all(state in nfa.states for state in start_states) - assert all(state in nfa.states for state in final_states) - assert nfa.accepts("abcd") == True - assert nfa.accepts("abc") == False - assert nfa.accepts("ab") == False - assert nfa.accepts("a") == False