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..6b01864 --- /dev/null +++ b/TaskC00/run.py @@ -0,0 +1,41 @@ +import sys + +def load_data(file_path): + nfa = {} + with open(file_path, 'r') as file: + for line in file: + split_data = line.strip().split('\t') + if len(split_data) == 3: + state, next_state, symbol = split_data + key = (int(state), symbol) + if key in nfa: + nfa[key].add(int(next_state)) + else: + nfa[key] = {int(next_state)} + + return nfa + +def is_accepted(nfa, input, current_states, accepting_states): + for symbol in input: + new_states = set() + for state in current_states: + key = (state, symbol) + if key in nfa: + new_states.update(nfa[key]) + current_states = new_states + + return any(state in accepting_states for state in current_states) + +# Set of accepting states +accepting_states = {3,4} + +nfa_file = sys.argv[1] +nfa = load_data(nfa_file) + +for line in sys.stdin: + input_str = line.strip() + start_states = {0} + if is_accepted(nfa, input_str, start_states, accepting_states): + print("YES") + else: + print("NO") diff --git a/TaskC00/test.exp b/TaskC00/test.exp new file mode 100644 index 0000000..2c8d678 Binary files /dev/null and b/TaskC00/test.exp differ diff --git a/TaskC01/run.py b/TaskC01/run.py new file mode 100644 index 0000000..d314c11 --- /dev/null +++ b/TaskC01/run.py @@ -0,0 +1,41 @@ +import sys + +def load_data(file_path): + nfa = {} + with open(file_path, 'r') as file: + for line in file: + split_data = line.strip().split(' ') + if len(split_data) == 3: + state, next_state, symbol = split_data + key = (int(state), symbol) + if key in nfa: + nfa[key].add(int(next_state)) + else: + nfa[key] = {int(next_state)} + + return nfa + +def is_accepted(nfa, input, current_states, accepting_states): + for symbol in input: + new_states = set() + for state in current_states: + key = (state, symbol) + if key in nfa: + new_states.update(nfa[key]) + current_states = new_states + + return any(state in accepting_states for state in current_states) + +# Set of accepting states +accepting_states = {2} + +nfa_file = sys.argv[1] +nfa = load_data(nfa_file) + +for line in sys.stdin: + input_str = line.strip() + start_states = {0} + if is_accepted(nfa, input_str, start_states, accepting_states): + print("YES") + else: + print("NO") diff --git a/TaskC01/test.arg b/TaskC01/test.arg new file mode 100644 index 0000000..5624678 --- /dev/null +++ b/TaskC01/test.arg @@ -0,0 +1,7 @@ +0 0 a +0 0 c +0 0 b +0 1 b +1 2 a +1 2 c +1 2 b \ No newline at end of file diff --git a/TaskC01/test.exp b/TaskC01/test.exp index fffc02c..405918c 100644 Binary files a/TaskC01/test.exp and b/TaskC01/test.exp differ diff --git a/TaskC02/run.py b/TaskC02/run.py new file mode 100644 index 0000000..d314c11 --- /dev/null +++ b/TaskC02/run.py @@ -0,0 +1,41 @@ +import sys + +def load_data(file_path): + nfa = {} + with open(file_path, 'r') as file: + for line in file: + split_data = line.strip().split(' ') + if len(split_data) == 3: + state, next_state, symbol = split_data + key = (int(state), symbol) + if key in nfa: + nfa[key].add(int(next_state)) + else: + nfa[key] = {int(next_state)} + + return nfa + +def is_accepted(nfa, input, current_states, accepting_states): + for symbol in input: + new_states = set() + for state in current_states: + key = (state, symbol) + if key in nfa: + new_states.update(nfa[key]) + current_states = new_states + + return any(state in accepting_states for state in current_states) + +# Set of accepting states +accepting_states = {2} + +nfa_file = sys.argv[1] +nfa = load_data(nfa_file) + +for line in sys.stdin: + input_str = line.strip() + start_states = {0} + if is_accepted(nfa, input_str, start_states, accepting_states): + print("YES") + else: + print("NO") diff --git a/TaskC02/test.arg b/TaskC02/test.arg new file mode 100644 index 0000000..1da9aaa --- /dev/null +++ b/TaskC02/test.arg @@ -0,0 +1,5 @@ +0 0 a +0 0 c +0 0 b +0 1 a +1 2 b \ No newline at end of file diff --git a/TaskC02/test.exp b/TaskC02/test.exp index e09bb02..557b5eb 100644 Binary files a/TaskC02/test.exp and b/TaskC02/test.exp differ diff --git a/TaskC03/run.py b/TaskC03/run.py new file mode 100644 index 0000000..5cde8eb --- /dev/null +++ b/TaskC03/run.py @@ -0,0 +1,41 @@ +import sys + +def load_data(file_path): + nfa = {} + with open(file_path, 'r') as file: + for line in file: + split_data = line.strip().split(' ') + if len(split_data) == 3: + state, next_state, symbol = split_data + key = (int(state), symbol) + if key in nfa: + nfa[key].add(int(next_state)) + else: + nfa[key] = {int(next_state)} + + return nfa + +def is_accepted(nfa, input, current_states, accepting_states): + for symbol in input: + new_states = set() + for state in current_states: + key = (state, symbol) + if key in nfa: + new_states.update(nfa[key]) + current_states = new_states + + return any(state in accepting_states for state in current_states) + +# Set of accepting states +accepting_states = {3} + +nfa_file = sys.argv[1] +nfa = load_data(nfa_file) + +for line in sys.stdin: + input_str = line.strip() + start_states = {0} + if is_accepted(nfa, input_str, start_states, accepting_states): + print("YES") + else: + print("NO") diff --git a/TaskC03/test.arg b/TaskC03/test.arg new file mode 100644 index 0000000..9a401b9 --- /dev/null +++ b/TaskC03/test.arg @@ -0,0 +1,9 @@ +0 0 a +0 0 c +0 0 b +0 1 a +1 2 b +2 3 c +3 3 a +3 3 b +3 3 c \ No newline at end of file diff --git a/TaskC03/test.exp b/TaskC03/test.exp index 29c072b..68e7beb 100644 Binary files a/TaskC03/test.exp and b/TaskC03/test.exp differ diff --git a/TaskC04/run.py b/TaskC04/run.py new file mode 100644 index 0000000..87225df --- /dev/null +++ b/TaskC04/run.py @@ -0,0 +1,48 @@ +import sys + +def load_data(file_path): + dfa = {} + with open(file_path, 'r') as file: + for line in file: + split_data = line.strip().split('\t') + if len(split_data) == 3: + state, next_state, symbol = split_data + key = (int(state), symbol) + dfa[key] = int(next_state) + + return dfa + +def is_accepted(dfa, input_str, current_state, accepting_states): + for i, symbol in enumerate(input_str): + key = (current_state, symbol) + if key in dfa: + current_state = dfa[key] + elif (current_state, '') in dfa: # Check for transition + current_state = dfa[(current_state, '')] + else: + # If there is no transition for the current symbol or , reject the string + return False + + # If it's the last symbol and the current state is not accepting, check for transition + if i == len(input_str) - 1 and current_state not in accepting_states: + if (current_state, '') in dfa: + current_state = dfa[(current_state, '')] + if current_state in accepting_states: + return True + + return current_state in accepting_states + + +# Set of accepting states +accepting_states = {2,5} + +dfa_file = sys.argv[1] +dfa = load_data(dfa_file) + +for line in sys.stdin: + input_str = line.strip() + start_state = 0 + if is_accepted(dfa, input_str, start_state, accepting_states): + print("YES", input_str) + else: + print("NO", input_str) diff --git a/TaskC04/test.exp b/TaskC04/test.exp new file mode 100644 index 0000000..b86ec6d Binary files /dev/null and b/TaskC04/test.exp differ diff --git a/TaskC05/run.py b/TaskC05/run.py new file mode 100644 index 0000000..89394a9 --- /dev/null +++ b/TaskC05/run.py @@ -0,0 +1,67 @@ +import codecs +import sys + +def process_input(fsa, input_string): + current_state = 0 + output = "" + for symbol in input_string: + if (current_state, symbol) in fsa: + current_state = fsa[(current_state, symbol)] + else: + return f"{input_string};OOV" + + if (current_state, ";") in fsa: + current_state = fsa[(current_state, ";")] + code_paths = check_transition(fsa, current_state, 24) + + if code_paths is None: + return f"{input_string};OOV" + + for code in sorted(code_paths): + code_str = "".join(code) + output += f"{input_string};{code_str}\n" + + return output + +def check_transition(fsa, start_state, target_state): + def find_transitions(state, target_state, current_path, all_paths): + if state == target_state: + all_paths.append(current_path.copy()) + return + + for (s, symbol), next_state in fsa.items(): + if s == state: + current_path.append(symbol) + find_transitions(next_state, target_state, current_path, all_paths) + current_path.pop() + + current_state = start_state + all_paths = [] + find_transitions(current_state, target_state, [], all_paths) + + if not all_paths: + return None + + return all_paths + +fsa_file = "./multi.arg" # FSA FILE +fsa = {} + +with open(fsa_file, 'r', encoding='utf-8') as f: + for line in f: + parts = line.strip().split('\t') + if len(parts) == 3: + state, next_state, symbol = parts + fsa[(int(state), symbol)] = int(next_state) + +input_file_path = "./multi.in" # INPUT STRING +output_file_path = "test.exp" # OUTPUT FILE + +with open(input_file_path, 'r', encoding='utf-8') as file, open(output_file_path, 'w', encoding='utf-8') as output_file: + for line in file: + input_string = line.strip() + result = process_input(fsa, input_string) + output_file.write(result) + +# POCZĄTKOWO WSZYSTKIE ŚCIEŻKI DO PLIKÓW BYŁY PRZESYŁANE POPRZEZ TERMINAL ALE PRZEZ WZGLĄD NA POLSKIE ZNAKI MUSIAŁAM ZAMIESZCZAĆ ŚCIEŻKI DO PLIKÓW W KODZIE +# NIESTETY NIE UDAŁO MI SIĘ ZNALEŹĆ W JAKI SPOSÓB PRZESYŁAĆ PLIKI BY UŻYWAĆ UTF-8 \ No newline at end of file diff --git a/TaskC05/test.exp b/TaskC05/test.exp new file mode 100644 index 0000000..9179d8e --- /dev/null +++ b/TaskC05/test.exp @@ -0,0 +1,2 @@ +piła;N +piła;V diff --git a/TaskC06/run.py b/TaskC06/run.py new file mode 100644 index 0000000..f15b83a --- /dev/null +++ b/TaskC06/run.py @@ -0,0 +1,32 @@ +import sys + +fsa_file = "./small2.in" # FSA FILE +fsa = {} + +with open(fsa_file, 'r', encoding='utf-8') as f: + for line in f: + parts = line.strip().split('\t') + if len(parts) == 3: + state, next_state, symbol = parts + state, next_state = int(state), int(next_state) + if (state, '') not in fsa: + fsa[(state, '')] = {} + fsa[(state, '')][symbol] = next_state + +output_file_path = "./test.txt" # OUTPUT FILE + +def get_paths(current_state, current_path): + if (current_state, '') not in fsa: + return [current_path] + + paths = [] + for symbol, next_state in fsa[(current_state, '')].items(): + paths += get_paths(next_state, current_path + symbol) + + return paths + +with open(output_file_path, 'w', encoding='utf-8') as output_file: + start_state = min(state for state, _ in fsa) + paths = get_paths(start_state, '') + for path in paths: + output_file.write(path + '\n') diff --git a/TaskC06/test.txt b/TaskC06/test.txt new file mode 100644 index 0000000..5b78cc4 --- /dev/null +++ b/TaskC06/test.txt @@ -0,0 +1,4 @@ +biały +dom +piła +stali