diff --git a/cw-2/fsa_description.arg b/cw-2/fsa_description.arg new file mode 100644 index 0000000..07020cc --- /dev/null +++ b/cw-2/fsa_description.arg @@ -0,0 +1,13 @@ +01 02 0 +01 12 1 +02 01 0 +02 21 1 +11 12 0 +11 22 1 +12 11 0 +12 01 1 +21 22 0 +21 02 1 +22 21 0 +22 11 1 +01 02 \ No newline at end of file diff --git a/cw-2/run.py b/cw-2/run.py new file mode 100644 index 0000000..de7eba6 --- /dev/null +++ b/cw-2/run.py @@ -0,0 +1,101 @@ +import sys +import csv +import os + +from numpy import append + +DIR = os.path.dirname(__file__) +alfabet = "01" + + +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 = fsa_description[-1] + 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 (item[0], letter) in fsa_description_map: + print("ERROR - duplicate letter:", letter, "| line:", num + 1) + is_error = True + tuple = (item[0], letter) + fsa_description_map[tuple] = 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) + + +is_difference = [] +for i, word in enumerate(test_in): + current_state = "01" + 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 in accepting_state: + print("YES\t", end="") + if "YES" != test_out[i][0]: + print("ERROR", word, end="") + is_difference.append(i + 1) + else: + print("NO\t", end="") + if "NO" != test_out[i][0]: + print("ERROR", word, end="") + is_difference.append(i + 1) + print() + +if len(is_difference) != 0: + print(is_difference) diff --git a/cw-2/simple.exp b/cw-2/simple.exp new file mode 100644 index 0000000..8c86730 --- /dev/null +++ b/cw-2/simple.exp @@ -0,0 +1,64 @@ +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES +NO +NO +YES diff --git a/cw-2/simple.in b/cw-2/simple.in new file mode 100644 index 0000000..32f8b3b --- /dev/null +++ b/cw-2/simple.in @@ -0,0 +1,64 @@ +00000 +00001 +00010 +00011 +00100 +00101 +00110 +00111 +01000 +01001 +01010 +01011 +01100 +01101 +01110 +01111 +10000 +10001 +10010 +10011 +10100 +10101 +10110 +10111 +11000 +11001 +11010 +11011 +11100 +11101 +11110 +11111 +100000 +100001 +100010 +100011 +100100 +100101 +100110 +100111 +101000 +101001 +101010 +101011 +101100 +101101 +101110 +101111 +110000 +110001 +110010 +110011 +110100 +110101 +110110 +110111 +111000 +111001 +111010 +111011 +111100 +111101 +111110 +111111