jfz-2023-s473555/TaskB04/run.py

79 lines
2.2 KiB
Python
Raw Normal View History

2023-11-01 22:24:46 +01:00
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("test.in")
sys.argv.append("test.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 = {}
for item in fsa_description:
for letter in item[2]:
if letter not in alfabet:
print("ERROR - letter not in alfabet: ", letter)
exit(-1)
tuple = (int(item[0]), letter)
fsa_description_map[tuple] = int(item[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)
for state in states:
if not is_permutation(states[state], alfabet):
print("ERROR - alfabet doesn't match: ", 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)
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", end="")
else:
print("NO", end="")
if "NO" != test_out[i][0]:
print(" ERROR", end="")
print()