From eb3db4869e151b183f1edb43be60ad9aca2d9351 Mon Sep 17 00:00:00 2001 From: IgnBys Date: Sat, 25 Nov 2023 23:47:56 +0100 Subject: [PATCH] C06 fixed --- TaskC06/run.py | 72 +++++++++++++++++++++++++++++-------------------- TaskC06/test.py | 57 --------------------------------------- 2 files changed, 43 insertions(+), 86 deletions(-) delete mode 100644 TaskC06/test.py diff --git a/TaskC06/run.py b/TaskC06/run.py index 75b1a1e..6b35197 100644 --- a/TaskC06/run.py +++ b/TaskC06/run.py @@ -1,39 +1,53 @@ -def run_automaton(file_path): +import sys + +def write_answer(answer, output_file): + with open(output_file, 'a', encoding='utf-8') as file: + file.write(answer+'\n') +def run_automaton(file_path, end_positions, output_file): transitions = {} - # Чтение информации из файла - with open(file_path, 'r') as file: - lines = file.readlines() + with open(file_path, 'r', encoding='utf-8') as file: + with open(output_file, 'w', encoding='utf-8') as file_writed: - for line in lines: - if line.strip(): # Пропускаем пустые строки - parts = line.split() - state_from = int(parts[0]) - state_to = int(parts[1]) + lines = file.readlines() - if len(parts) == 3: - symbol = parts[2] - transitions.setdefault(state_from, []).append((symbol, state_to)) - else: - transitions[state_from] = [] + for line in lines: + if line.strip(): # Пропускаем пустые строки + parts = line.split() + state_from = int(parts[0]) - current_state = 0 - word = "" + if len(parts) > 1: + state_to = int(parts[1]) - while current_state in transitions: - possible_transitions = transitions[current_state] - if not possible_transitions: - break + if len(parts) == 3: + symbol = parts[2] + transitions.setdefault(state_from, []).append((symbol, state_to)) + else: + transitions[state_from] = [] - transition_symbol, next_state = possible_transitions[0] - word += transition_symbol - current_state = next_state + def explore_paths(current_state, current_word): + if current_state not in transitions: + if str(current_state) in end_positions: + write_answer(current_word, output_file) + return - print(f"Достигнуто конечное состояние: {current_state}") - print(f"Полученное слово: {word}") + possible_transitions = transitions[current_state] + for transition_symbol, next_state in possible_transitions: + explore_paths(next_state, current_word + transition_symbol) -# Указываем путь к файлу -file_path = "small.in" + explore_paths(0, "") +def what_is_the_ended_positions(file_path): + array = [] + with open(file_path, 'r', encoding='utf-8') as file: + for row in file: + line = row.strip().split('\t') + if len(line) == 1: + array += line + return array +input_file = sys.argv[1] +output_file = sys.argv[2] +# file_path = "small.in" +# output_file = "small.out" +end_positions = what_is_the_ended_positions(input_file) -# Запускаем автомат -run_automaton(file_path) +run_automaton(input_file, end_positions, output_file) diff --git a/TaskC06/test.py b/TaskC06/test.py deleted file mode 100644 index f7d2b2b..0000000 --- a/TaskC06/test.py +++ /dev/null @@ -1,57 +0,0 @@ - - -def write_answer(answer, output_file): - with open(output_file, 'a', encoding='utf-8') as file: - file.write(answer+'\n') -def run_automaton(file_path, end_positions, output_file): - transitions = {} - - # Чтение информации из файла - with open(file_path, 'r', encoding='utf-8') as file: - with open(output_file, 'w', encoding='utf-8') as file_writed: - - lines = file.readlines() - - for line in lines: - if line.strip(): # Пропускаем пустые строки - parts = line.split() - state_from = int(parts[0]) - - if len(parts) > 1: - state_to = int(parts[1]) - - if len(parts) == 3: - symbol = parts[2] - transitions.setdefault(state_from, []).append((symbol, state_to)) - else: - transitions[state_from] = [] - - def explore_paths(current_state, current_word): - if current_state not in transitions: - if str(current_state) in end_positions: - write_answer(current_word, output_file) - return - - possible_transitions = transitions[current_state] - for transition_symbol, next_state in possible_transitions: - explore_paths(next_state, current_word + transition_symbol) - - # Начинаем с начального состояния - explore_paths(0, "") -def what_is_the_ended_positions(file_path): - array = [] - with open(file_path, 'r', encoding='utf-8') as file: - for row in file: - line = row.strip().split('\t') - if len(line) == 1: - array += line - return array - -# Указываем путь к файлу -file_path = "small.in" -output_file = "small.out" -# Получаем конечные позиции из файла -end_positions = what_is_the_ended_positions(file_path) - -# Запускаем автомат -run_automaton(file_path, end_positions, output_file)