From f60d42816ef2bc703f09e40c7ab8dee443fa1947 Mon Sep 17 00:00:00 2001 From: Pawel Felcyn Date: Sun, 10 Dec 2023 21:57:29 +0100 Subject: [PATCH] c00 --- TaskC00/long.arg | 1 - TaskC00/run.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++ TaskC00/test1.out | 9 ++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 TaskC00/run.py create mode 100644 TaskC00/test1.out diff --git a/TaskC00/long.arg b/TaskC00/long.arg index 2643dc5..15107e7 100644 --- a/TaskC00/long.arg +++ b/TaskC00/long.arg @@ -1,4 +1,3 @@ -# automat akceptuje dwa napisy - "aaaa" i "a" powielone 4038 razy 0 1 a 1 2 a 2 3 a diff --git a/TaskC00/run.py b/TaskC00/run.py new file mode 100644 index 0000000..537c832 --- /dev/null +++ b/TaskC00/run.py @@ -0,0 +1,54 @@ +import pandas as pd +import os +import sys +import random + +class Automat: + def __init__(self, df: pd.DataFrame, end_states: list): + self._df = df + self._end_states = end_states + + def _consume_character(self, state: int, char: str): + filter = (self._df[0] == state) & (self._df[2] == char) + matching_rows = self._df[filter] + if matching_rows.empty: + return None + end_states = matching_rows.iloc[:,1].tolist() + return random.choice(end_states) + + def consume_word(self, word: str): + state = 0 + for char in word: + state = self._consume_character(state, char) + if state is None: + return False + return state in self._end_states + +def create_automat_from_file(file_path: str): + automat_df = pd.read_csv(file_path, sep='\t', header=None) + + end_states_condition = (automat_df[1].isnull()) & (automat_df[2].isnull()) + end_states_df = automat_df[end_states_condition] + end_states = end_states_df.iloc[:,0].tolist() + + automat_df = automat_df[~end_states_condition] + automat_df[2] = automat_df[2].astype(str) + + return Automat(automat_df, end_states) + +automat_path = os.path.join(os.path.dirname(__file__), 'test1.arg') +automat = create_automat_from_file(automat_path) + +test_path = os.path.join(os.path.dirname(__file__), 'test1.in') + + +results = [] +with open(test_path, 'r') as f: + for line in f: + results.append('YES' if automat.consume_word(line.strip()) else 'NO') + +out_path = os.path.join(os.path.dirname(__file__), 'test1.out') + +with open(out_path, 'w') as f: + for result in results: + f.write(result + '\n') \ No newline at end of file diff --git a/TaskC00/test1.out b/TaskC00/test1.out new file mode 100644 index 0000000..4c1a9bc --- /dev/null +++ b/TaskC00/test1.out @@ -0,0 +1,9 @@ +NO +YES +NO +NO +NO +NO +NO +NO +NO