Compare commits

...

6 Commits

Author SHA1 Message Date
HOME-VM-TOSCHOOL
398fa20843 C04 - code cleanup 2023-11-18 01:23:28 +01:00
HOME-VM-TOSCHOOL
2f2fb0d770 Task C04 2023-11-18 00:24:23 +01:00
HOME-VM-TOSCHOOL
24d9d79d4c Task C04-eps 2023-11-17 23:33:16 +01:00
HOME-VM-TOSCHOOL
38771a1a34 Task C03 2023-11-17 18:43:26 +01:00
HOME-VM-TOSCHOOL
122fd7df61 task C02 2023-11-17 18:40:41 +01:00
HOME-VM-TOSCHOOL
680f38034e task C01 2023-11-17 18:39:28 +01:00
7 changed files with 604 additions and 0 deletions

139
TaskC01/run.py Normal file
View File

@ -0,0 +1,139 @@
import sys
import csv
from tokenize import Comment
from numpy import append
DEBUG = False
alfabet = "abc"
sys.setrecursionlimit(4100)
def print_debug(*args, **kwargs):
if DEBUG:
print(*args, **kwargs)
if len(sys.argv) == 1:
print("Default arguments parsed\n")
sys.argv.append("test.arg")
sys.argv.append("test.in")
sys.argv.append("test.exp")
with open(sys.argv[1], mode="r", newline="", encoding="utf8") as csvfile:
filereader = csv.reader(csvfile, delimiter="\t", quotechar="|")
fsa_description = list(filereader)
# skip first line with comment
if fsa_description[0][0][0] == "#":
fsa_description = fsa_description[1:]
# get accepting states
accepting_state = []
for item in reversed(fsa_description):
if len(item) == 1:
accepting_state.append(item[0])
else:
break
fsa_description = fsa_description[: -len(accepting_state)]
fsa_description_map = {}
is_error = False
for num, item in enumerate(fsa_description):
for letter in item[2]:
if letter not in alfabet:
print_debug(
"WARNING - letter not in alfabet: ", letter, "| line:", num + 1
)
tuple = (item[0], letter)
if (item[0], letter) in fsa_description_map:
print_debug(
f"WARNING - duplicate letter: key = ({item[0]} | {letter}) | line: {num + 1}"
)
# append to description map or create it if it doesn't exist
fsa_description_map[tuple] = fsa_description_map.get(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_debug(
f"WARNING - state {state} doesn't match full alphabet: {states[state]} | {alfabet} | diff - {find_missing_letters(states[state], alfabet)}"
)
# exit(-1)
with open(sys.argv[2], mode="r", newline="", encoding="utf8") as file:
content = file.read()
test_in = content.splitlines()
with open(sys.argv[3], mode="r", newline="", encoding="utf8") as file:
content = file.read()
test_out = content.splitlines()
def is_correct(current_state: str) -> bool:
if current_state in accepting_state:
return True
return False
def traverse_states(current_state: str, word: str) -> str:
for letter in word:
if (current_state, letter) in fsa_description_map:
next_states = fsa_description_map[(current_state, letter)]
for next_state in next_states:
ending_state = traverse_states(next_state, word[1:])
if is_correct(ending_state):
return ending_state # found a correct state
return "-2" # no correct state found
else:
return "-1" # unspecified next state
return current_state # end of the word
is_difference = []
for i, word in enumerate(test_in):
current_state = "0"
if len(word) != 0:
current_state = traverse_states(current_state, word)
print(str(i + 1) + "\t", end="")
if current_state in accepting_state:
print("YES\t", end="")
if "YES" != test_out[i]:
print("ERROR", word, end="")
is_difference.append(i + 1)
else:
print("NO\t", end="")
if "NO" != test_out[i]:
print("ERROR", word, end="")
is_difference.append(i + 1)
print()
if len(is_difference) != 0:
print(is_difference)

4
TaskC01/test.arg Normal file
View File

@ -0,0 +1,4 @@
0 0 abc
0 1 b
1 2 abc
2

139
TaskC02/run.py Normal file
View File

@ -0,0 +1,139 @@
import sys
import csv
from tokenize import Comment
from numpy import append
DEBUG = False
alfabet = "abc"
sys.setrecursionlimit(4100)
def print_debug(*args, **kwargs):
if DEBUG:
print(*args, **kwargs)
if len(sys.argv) == 1:
print("Default arguments parsed\n")
sys.argv.append("test.arg")
sys.argv.append("test.in")
sys.argv.append("test.exp")
with open(sys.argv[1], mode="r", newline="", encoding="utf8") as csvfile:
filereader = csv.reader(csvfile, delimiter="\t", quotechar="|")
fsa_description = list(filereader)
# skip first line with comment
if fsa_description[0][0][0] == "#":
fsa_description = fsa_description[1:]
# get accepting states
accepting_state = []
for item in reversed(fsa_description):
if len(item) == 1:
accepting_state.append(item[0])
else:
break
fsa_description = fsa_description[: -len(accepting_state)]
fsa_description_map = {}
is_error = False
for num, item in enumerate(fsa_description):
for letter in item[2]:
if letter not in alfabet:
print_debug(
"WARNING - letter not in alfabet: ", letter, "| line:", num + 1
)
tuple = (item[0], letter)
if (item[0], letter) in fsa_description_map:
print_debug(
f"WARNING - duplicate letter: key = ({item[0]} | {letter}) | line: {num + 1}"
)
# append to description map or create it if it doesn't exist
fsa_description_map[tuple] = fsa_description_map.get(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_debug(
f"WARNING - state {state} doesn't match full alphabet: {states[state]} | {alfabet} | diff - {find_missing_letters(states[state], alfabet)}"
)
# exit(-1)
with open(sys.argv[2], mode="r", newline="", encoding="utf8") as file:
content = file.read()
test_in = content.splitlines()
with open(sys.argv[3], mode="r", newline="", encoding="utf8") as file:
content = file.read()
test_out = content.splitlines()
def is_correct(current_state: str) -> bool:
if current_state in accepting_state:
return True
return False
def traverse_states(current_state: str, word: str) -> str:
for letter in word:
if (current_state, letter) in fsa_description_map:
next_states = fsa_description_map[(current_state, letter)]
for next_state in next_states:
ending_state = traverse_states(next_state, word[1:])
if is_correct(ending_state):
return ending_state # found a correct state
return "-2" # no correct state found
else:
return "-1" # unspecified next state
return current_state # end of the word
is_difference = []
for i, word in enumerate(test_in):
current_state = "0"
if len(word) != 0:
current_state = traverse_states(current_state, word)
print(str(i + 1) + "\t", end="")
if current_state in accepting_state:
print("YES\t", end="")
if "YES" != test_out[i]:
print("ERROR", word, end="")
is_difference.append(i + 1)
else:
print("NO\t", end="")
if "NO" != test_out[i]:
print("ERROR", word, end="")
is_difference.append(i + 1)
print()
if len(is_difference) != 0:
print(is_difference)

4
TaskC02/test.arg Normal file
View File

@ -0,0 +1,4 @@
0 0 abc
0 1 a
1 2 b
2

139
TaskC03/run.py Normal file
View File

@ -0,0 +1,139 @@
import sys
import csv
from tokenize import Comment
from numpy import append
DEBUG = False
alfabet = "abc"
sys.setrecursionlimit(4100)
def print_debug(*args, **kwargs):
if DEBUG:
print(*args, **kwargs)
if len(sys.argv) == 1:
print("Default arguments parsed\n")
sys.argv.append("test.arg")
sys.argv.append("test.in")
sys.argv.append("test.exp")
with open(sys.argv[1], mode="r", newline="", encoding="utf8") as csvfile:
filereader = csv.reader(csvfile, delimiter="\t", quotechar="|")
fsa_description = list(filereader)
# skip first line with comment
if fsa_description[0][0][0] == "#":
fsa_description = fsa_description[1:]
# get accepting states
accepting_state = []
for item in reversed(fsa_description):
if len(item) == 1:
accepting_state.append(item[0])
else:
break
fsa_description = fsa_description[: -len(accepting_state)]
fsa_description_map = {}
is_error = False
for num, item in enumerate(fsa_description):
for letter in item[2]:
if letter not in alfabet:
print_debug(
"WARNING - letter not in alfabet: ", letter, "| line:", num + 1
)
tuple = (item[0], letter)
if (item[0], letter) in fsa_description_map:
print_debug(
f"WARNING - duplicate letter: key = ({item[0]} | {letter}) | line: {num + 1}"
)
# append to description map or create it if it doesn't exist
fsa_description_map[tuple] = fsa_description_map.get(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_debug(
f"WARNING - state {state} doesn't match full alphabet: {states[state]} | {alfabet} | diff - {find_missing_letters(states[state], alfabet)}"
)
# exit(-1)
with open(sys.argv[2], mode="r", newline="", encoding="utf8") as file:
content = file.read()
test_in = content.splitlines()
with open(sys.argv[3], mode="r", newline="", encoding="utf8") as file:
content = file.read()
test_out = content.splitlines()
def is_correct(current_state: str) -> bool:
if current_state in accepting_state:
return True
return False
def traverse_states(current_state: str, word: str) -> str:
for letter in word:
if (current_state, letter) in fsa_description_map:
next_states = fsa_description_map[(current_state, letter)]
for next_state in next_states:
ending_state = traverse_states(next_state, word[1:])
if is_correct(ending_state):
return ending_state # found a correct state
return "-2" # no correct state found
else:
return "-1" # unspecified next state
return current_state # end of the word
is_difference = []
for i, word in enumerate(test_in):
current_state = "0"
if len(word) != 0:
current_state = traverse_states(current_state, word)
print(str(i + 1) + "\t", end="")
if current_state in accepting_state:
print("YES\t", end="")
if "YES" != test_out[i]:
print("ERROR", word, end="")
is_difference.append(i + 1)
else:
print("NO\t", end="")
if "NO" != test_out[i]:
print("ERROR", word, end="")
is_difference.append(i + 1)
print()
if len(is_difference) != 0:
print(is_difference)

6
TaskC03/test.arg Normal file
View File

@ -0,0 +1,6 @@
0 0 abc
0 1 a
1 2 b
2 3 c
3 3 abc
3

173
TaskC04/run.py Normal file
View File

@ -0,0 +1,173 @@
import sys
import csv
from tokenize import Comment
from numpy import append
DEBUG = False
alfabet = "abc"
EPS = "<eps>"
# sys.setrecursionlimit(4100)
def print_debug(*args, **kwargs):
if DEBUG:
print(*args, **kwargs)
if len(sys.argv) == 1:
print("Default arguments parsed\n")
sys.argv.append("simple2.arg")
sys.argv.append("simple2.in")
sys.argv.append("simple2.exp")
with open(sys.argv[1], mode="r", newline="", encoding="utf8") as csvfile:
filereader = csv.reader(csvfile, delimiter="\t", quotechar="|")
fsa_description = list(filereader)
# skip first line with comment
if fsa_description[0][0][0] == "#":
fsa_description = fsa_description[1:]
# get accepting states
accepting_state = []
for item in reversed(fsa_description):
if len(item) == 1:
accepting_state.append(item[0])
else:
break
fsa_description = fsa_description[: -len(accepting_state)]
fsa_description_map: dict[tuple[str, str], list[str]] = {}
is_error = False
for num, item in enumerate(fsa_description):
if EPS in item[2]:
item[2] = item[2].replace(EPS, "")
tuple = (item[0], EPS)
if (item[0], EPS) in fsa_description_map:
print_debug(
f"WARNING - duplicate letter: key = ({item[0]} | {EPS}) | line: {num + 1}"
)
# append to description map or create it if it doesn't exist
fsa_description_map[tuple] = fsa_description_map.get(tuple, []) + [item[1]]
for letter in item[2]:
if letter not in alfabet:
print_debug(
"WARNING - letter not in alfabet: ", letter, "| line:", num + 1
)
tuple = (item[0], letter)
if (item[0], letter) in fsa_description_map:
print_debug(
f"WARNING - duplicate letter: key = ({item[0]} | {letter}) | line: {num + 1}"
)
# append to description map or create it if it doesn't exist
fsa_description_map[tuple] = fsa_description_map.get(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_debug(
f"WARNING - state {state} doesn't match full alphabet: {states[state]} | {alfabet} | diff - {find_missing_letters(states[state], alfabet)}"
)
# exit(-1)
with open(sys.argv[2], mode="r", newline="", encoding="utf8") as file:
content = file.read()
test_in = content.splitlines()
with open(sys.argv[3], mode="r", newline="", encoding="utf8") as file:
content = file.read()
test_out = content.splitlines()
for i, _ in enumerate(test_out):
test_out[i] = test_out[i].split(" ")[0]
def is_correct(current_state: str) -> bool:
if current_state in accepting_state:
return True
return False
def find_final_state(current_state: str, word: str, letter: str) -> str:
next_states = fsa_description_map[(current_state, letter)]
for next_state in next_states:
if letter != EPS:
word = word[1:]
current_state = traverse_states(next_state, word)
if is_correct(current_state):
return current_state # found a correct state
return current_state
def traverse_states(current_state: str, word: str) -> str:
if word == "" and (current_state, EPS) not in fsa_description_map:
return current_state
if (current_state, EPS) in fsa_description_map:
final_state = find_final_state(current_state, word, EPS)
if is_correct(final_state):
return final_state # found a correct state
letter = word[0]
if (current_state, letter) not in fsa_description_map:
return "-1" # unspecified next state
final_state = find_final_state(current_state, word, letter)
if is_correct(final_state):
return final_state # found a correct state
return "-2" # no correct state found
is_difference = []
for i, word in enumerate(test_in):
current_state = "0"
if len(word) != 0:
current_state = traverse_states(current_state, word)
print(str(i + 1) + "\t", end="")
if current_state == "-3":
print("FATAL ERROR")
exit(-1)
if current_state in accepting_state:
print("YES\t", end="")
if "YES" != test_out[i] and "TRUE" != test_out[i]:
print("ERROR", word, end="")
is_difference.append(i + 1)
else:
print("NO\t", end="")
if "NO" != test_out[i] and "FALSE" != test_out[i]:
print("ERROR", word, end="")
is_difference.append(i + 1)
print()
if len(is_difference) != 0:
print(is_difference)