From 59b85bff1887f9f1d8cdc46958b642476d99620a Mon Sep 17 00:00:00 2001 From: doasdsk Date: Sat, 25 Nov 2023 22:29:42 +0100 Subject: [PATCH] tasks --- TaskC00/run.py | 2 +- TaskC04/long.arg | 1 - TaskC04/run.py | 75 +++++++++++++++++++++++++++++++++++++++++++++ TaskC04/simple1.arg | 1 - TaskC05/run.py | 75 +++++++++++++++++++++++++++++++++++++++++++++ TaskC06/medium2.in | 2 +- TaskC06/run.py | 47 ++++++++++++++++++++++++++++ 7 files changed, 199 insertions(+), 4 deletions(-) create mode 100644 TaskC04/run.py create mode 100644 TaskC05/run.py create mode 100644 TaskC06/run.py diff --git a/TaskC00/run.py b/TaskC00/run.py index df85d58..b86e656 100644 --- a/TaskC00/run.py +++ b/TaskC00/run.py @@ -53,7 +53,7 @@ with open(sys.argv[1]) as table: else: AddFinalState(final_states, line) -with open(sys.argv[2], 'r') as input_data, open(sys.argv[3], 'w') as output_data: +with open(sys.argv[2], 'r', encoding="utf-8") as input_data, open(sys.argv[3], 'w', encoding="utf-8") as output_data: for line in input_data: line = line.rstrip() diff --git a/TaskC04/long.arg b/TaskC04/long.arg index 2643dc5..15107e7 100644 --- a/TaskC04/long.arg +++ b/TaskC04/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/TaskC04/run.py b/TaskC04/run.py new file mode 100644 index 0000000..7a3d637 --- /dev/null +++ b/TaskC04/run.py @@ -0,0 +1,75 @@ +import sys + +def ReadDescription(file_path): + nfa = {} + accepting_states = set() + + with open(file_path, 'r', encoding="utf-8") as description: + for line in description: + 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)} + + elif len(split_data) == 2: + state, next_state = split_data + key = (int(state), '') + if key in nfa: + nfa[key].add(int(next_state)) + else: + nfa[key] = {int(next_state)} + + elif len(split_data) == 1: + accepting_states.add(int(split_data[0])) + + return nfa, accepting_states + +def Epsilon(nfa, states): + epsilon = set(states) + stack = list(states) + + while stack: + currentState = stack.pop() + epsilon_key = (currentState, '') + if epsilon_key in nfa: + epsTransitions = nfa[epsilon_key] + for nextState in epsTransitions: + if nextState not in epsilon: + epsilon.add(nextState) + stack.append(nextState) + + return epsilon + +def IsAccepted(nfa, inputString, current_states, accepting_states): + for symbol in inputString: + new_states = set() + + for state in current_states: + key = (state, symbol) + if key in nfa: + new_states.update(nfa[key]) + + current_states = Epsilon(nfa, new_states) + + return any(state in accepting_states for state in current_states) + + + +nfa_file = sys.argv[1] +nfa, acceptingStates = ReadDescription(nfa_file) + +for line in sys.stdin: + inputString = line.strip() + if inputString =='\n': + break + start_states = Epsilon(nfa, {0}) + + if IsAccepted(nfa, inputString, start_states, acceptingStates): + print("TRUE", inputString) + else: + print("FALSE", inputString) diff --git a/TaskC04/simple1.arg b/TaskC04/simple1.arg index da827d3..f62f489 100644 --- a/TaskC04/simple1.arg +++ b/TaskC04/simple1.arg @@ -1,4 +1,3 @@ -# prosty automat akceptujący tylko napis "abc" 0 1 a 1 2 b 2 3 c diff --git a/TaskC05/run.py b/TaskC05/run.py new file mode 100644 index 0000000..ba50873 --- /dev/null +++ b/TaskC05/run.py @@ -0,0 +1,75 @@ +import sys + +sys.setrecursionlimit(4500) + +def AddTransition(transitions, state_from, state_to, symbol): + if state_from in transitions: + if symbol not in transitions[state_from]: + transitions[state_from][symbol] = {state_to} + else: + transitions[state_from][symbol] |= {state_to} + else: + transitions[state_from] = {symbol: {state_to}} + +def AddFinalState(final_states, state): + final_states.add(state) + +def GetFinalStates(transitions, final_states, string, current_state): + if not string: + return current_state if current_state in final_states else -1 + + symbol, rest = string[0], string[1:] + + if current_state in transitions and symbol in transitions[current_state]: + for state in transitions[current_state][symbol]: + result = GetFinalStates(transitions, final_states, rest, state) + if result != -1: + return result + return -1 + +def IsAccepted(transitions, final_states, string, initial_state): + final_state = GetFinalStates(transitions, final_states, string, initial_state) + return final_state in final_states + + +def ChangeInput(transitions, final_states, string, initial_state): + possible_ending_parts = [';N', ';V', ';ADJ'] + valid_results = [] + + for part in possible_ending_parts: + copy_string = string + part + if IsAccepted(transitions, final_states, copy_string, initial_state): + valid_results.append(copy_string) + + if not valid_results: + valid_results.append(string + ';OOV') + + return valid_results + + +transitions = {} +final_states = set() + +fsaDescr = sys.argv[1] # 'elem.arg' +inputFile = sys.argv[2] # 'elem.in' +outputFile = sys.argv[3] # 'elem.out' + +with open(fsaDescr, 'r', encoding="utf-8") as description: + for line in description: + line = line.rstrip() + + if len(line.split('\t')) == 3: + a, b, c = line.split('\t') + AddTransition(transitions, a, b, c) + else: + AddFinalState(final_states, line) + +with open(inputFile, 'r', encoding="utf-8") as inFile, open(outputFile, 'w', encoding="utf-8") as outFile: + for line in inFile: + line = line.rstrip() + results = ChangeInput(transitions, final_states, line, '0') + results.sort() + for result in results: + outFile.write(result + '\n') + # print(result) + diff --git a/TaskC06/medium2.in b/TaskC06/medium2.in index a95b242..d1ec9c4 100644 --- a/TaskC06/medium2.in +++ b/TaskC06/medium2.in @@ -13984,6 +13984,6 @@ 6322 6325 i 6322 6323 y 6322 6325 ą -6323 873 c +6323 873 c 6323 6324 m 6324 6325 i diff --git a/TaskC06/run.py b/TaskC06/run.py new file mode 100644 index 0000000..1e56550 --- /dev/null +++ b/TaskC06/run.py @@ -0,0 +1,47 @@ +import sys + + +def read_fsa_description(file_path): + transitions = {} + final_states = set() + + with open(file_path, 'r', encoding='utf-8') as file: + for line in file: + line = line.rstrip() + if not line: + break + + parts = line.split('\t') + if len(parts) == 3: + state_from, state_to, symbol = parts[0], parts[1], parts[2] + if state_from in transitions: + transitions[state_from].append((state_to, symbol)) + else: + transitions[state_from] = [(state_to, symbol)] + + if len(parts)==1: + line = line.rstrip() + final_states.add(line) + + return transitions, final_states + +def generate_paths(transitions, current_state, current_path, final_states): + paths = [] + if current_state in final_states: + paths.append("".join(current_path)) + + if current_state in transitions: + for next_state, symbol in transitions[current_state]: + paths.extend(generate_paths(transitions, next_state, current_path + [symbol], final_states)) + + return paths + + +fsa_description_file = sys.argv[1]#'medium.in' +transitions, final_states = read_fsa_description(fsa_description_file) +paths = generate_paths(transitions, '0', [], final_states) + +for path in sorted(paths): + print(path) + +