diff --git a/TaskC06/run.py b/TaskC06/run.py index ad82a36..75b1a1e 100644 --- a/TaskC06/run.py +++ b/TaskC06/run.py @@ -1,66 +1,39 @@ -import sys -sys.setrecursionlimit(5000) +def run_automaton(file_path): + transitions = {} -def write_answer(answer): - with open(output_file, 'a') as file: - file.write(answer+'\n') + # Чтение информации из файла + with open(file_path, 'r') as file: + lines = file.readlines() -# def remove_first_symbol_from_the_list(): + for line in lines: + if line.strip(): # Пропускаем пустые строки + parts = line.split() + state_from = int(parts[0]) + state_to = int(parts[1]) -def line_checking(row, position): - help_row = row - help_position = position - next_character_array =[] - bool_value = False - for element in row: - if element=='\n': - if position in what_is_the_ended_positions(): - return True + if len(parts) == 3: + symbol = parts[2] + transitions.setdefault(state_from, []).append((symbol, state_to)) else: - return False - next_character_array = find_next_position(help_position, element) - if next_character_array != []: - if len(next_character_array)>1: - for element_in_next_character_array in next_character_array: - if bool_value ==True: - break - else: - bool_value =line_checking(help_row[1:], element_in_next_character_array) - else: - # remove_first_symbol_from_the_list() - help_row = help_row[1:] - bool_value = line_checking(help_row, next_character_array[0]) - else: + transitions[state_from] = [] + + current_state = 0 + word = "" + + while current_state in transitions: + possible_transitions = transitions[current_state] + if not possible_transitions: break - return bool_value + transition_symbol, next_state = possible_transitions[0] + word += transition_symbol + current_state = next_state -def find_next_position(position, character): - with open(used_table, 'r') as readed_used_table: - take_all_possible_positions = [] - for row_used_table in readed_used_table: - line = row_used_table.strip().split(' ') - if len(line) != 1: - if position == line[0] and character == line[2]: - take_all_possible_positions.append(line[1]) + print(f"Достигнуто конечное состояние: {current_state}") + print(f"Полученное слово: {word}") - return take_all_possible_positions -def what_is_the_ended_positions(): - array = [] - with open(used_table, 'r') as file: - for row in file: - line = row.strip().split('\t') - if len(line)==1: - array += line - return array -used_table = 'test.arg' -input_file = 'test.in' -output_file = 'test.out' -# what_is_the_ended_positions() -with open(output_file, 'w') as readed_output_file: - with open(input_file, 'r') as readed_input_file: - for row_input_file in readed_input_file: - if line_checking(row_input_file, '0')==True: - write_answer('YES') - else: - write_answer('NO') +# Указываем путь к файлу +file_path = "small.in" + +# Запускаем автомат +run_automaton(file_path) diff --git a/TaskC06/small.out b/TaskC06/small.out new file mode 100644 index 0000000..5b78cc4 --- /dev/null +++ b/TaskC06/small.out @@ -0,0 +1,4 @@ +biały +dom +piła +stali diff --git a/TaskC06/small2.out b/TaskC06/small2.out new file mode 100644 index 0000000..5aedaf0 --- /dev/null +++ b/TaskC06/small2.out @@ -0,0 +1,3 @@ +biały +piła +stali diff --git a/TaskC06/test.py b/TaskC06/test.py new file mode 100644 index 0000000..f7d2b2b --- /dev/null +++ b/TaskC06/test.py @@ -0,0 +1,57 @@ + + +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)