From 214e81592dba548f4abdd1c0abe0c13fd53825a3 Mon Sep 17 00:00:00 2001 From: HOME-VM-TOSCHOOL Date: Wed, 1 Nov 2023 23:35:11 +0100 Subject: [PATCH] B07 --- TaskB07/fsa_description.arg | 22 ++++++++ TaskB07/run.py | 103 ++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 TaskB07/fsa_description.arg create mode 100644 TaskB07/run.py diff --git a/TaskB07/fsa_description.arg b/TaskB07/fsa_description.arg new file mode 100644 index 0000000..d841632 --- /dev/null +++ b/TaskB07/fsa_description.arg @@ -0,0 +1,22 @@ +0 1 o +1 2 p +2 3 h +3 4 e +4 5 l +5 6 i +6 7 a +1 1 o +2 1 o +3 1 o +4 1 o +5 1 o +6 1 o +7 7 abcdefghijklmnopqrstuvwxyz +0 0 abcdefghijklmnpqrstuvwxyz +1 0 abcdefghijklmnqrstuvwxyz +2 0 abcdefgijklmnpqrstuvwxyz +3 0 abcdfghijklmnpqrstuvwxyz +4 0 abcdefghijkmnpqrstuvwxyz +5 0 abcdefghjklmnpqrstuvwxyz +6 0 bcdefghijklmnpqrstuvwxyz +7 \ No newline at end of file diff --git a/TaskB07/run.py b/TaskB07/run.py new file mode 100644 index 0000000..b4c899e --- /dev/null +++ b/TaskB07/run.py @@ -0,0 +1,103 @@ +from re import T +import string +import sys +import csv +import os +from more_itertools import difference + +from numpy import append + +DIR = os.path.dirname(__file__) +alfabet = "abcdefghijklmnopqrstuvwxyz " + + +def join_path(filename: str) -> str: + return os.path.join(DIR, filename) + + +if len(sys.argv) == 1: + print("Default arguments parsed\n") + sys.argv.append("fsa_description.arg") + sys.argv.append("simple.in") + sys.argv.append("simple.exp") + +with open(join_path(sys.argv[1]), "r", newline="", encoding="utf8") as csvfile: + filereader = csv.reader(csvfile, delimiter="\t", quotechar="|") + fsa_description = list(filereader) + accepting_state = int(fsa_description[-1][0]) + fsa_description = fsa_description[:-1] + fsa_description_map = {} + is_error = False + for num, item in enumerate(fsa_description): + for letter in item[2]: + if letter not in alfabet: + print("ERROR - letter not in alfabet: ", letter, "| line:", num + 1) + exit(-1) + if (int(item[0]), letter) in fsa_description_map: + print("ERROR - duplicate letter:", letter, "| line:", num + 1) + is_error = True + tuple = (int(item[0]), letter) + fsa_description_map[tuple] = int(item[1]) + + if is_error: + exit(-1) + +states = {} +for item in fsa_description_map: + states[item[0]] = states[item[0]] + item[1] if item[0] in states else item[1] + + +from collections import Counter + + +# check if all letters are used once +def is_permutation(str1, str2): + return Counter(str1) == Counter(str2) + + +def find_missing_letters(str1, str2) -> str: + missing_letters = "" + for char in str2: + if char not in str1: + missing_letters += char + return missing_letters + + +for state in states: + if not is_permutation(states[state], alfabet): + print( + f"ERROR - state {state} doesn't match: {states[state]} | {alfabet} | diff - {find_missing_letters(states[state], alfabet)}" + ) + exit(-1) + + +with open(join_path(sys.argv[2]), "r", newline="", encoding="utf8") as csvfile: + filereader = csv.reader(csvfile, delimiter="\t", quotechar="|") + test_in = list(filereader) + +with open(join_path(sys.argv[3]), "r", newline="", encoding="utf8") as csvfile: + filereader = csv.reader(csvfile, delimiter="\t", quotechar="|") + test_out = list(filereader) + + +difference = [] +for i, word in enumerate(test_in): + current_state = 0 + if len(word) != 0: + for letter in word[0]: + current_state = fsa_description_map[(current_state, letter)] + + print(str(i + 1) + "\t", end="") + if current_state == accepting_state: + print("YES\t", end="") + if "YES" != test_out[i][0]: + print("ERROR", word, end="") + difference.append(i) + else: + print("NO\t", end="") + if "NO" != test_out[i][0]: + print("ERROR", word, end="") + difference.append(i) + print() + +print(difference)