This commit is contained in:
IgnBys 2023-11-24 23:19:27 +01:00
parent 18ee7b7212
commit fd6c90a06a
4 changed files with 95 additions and 58 deletions

View File

@ -1,66 +1,39 @@
import sys def run_automaton(file_path):
sys.setrecursionlimit(5000) transitions = {}
def write_answer(answer): # Чтение информации из файла
with open(output_file, 'a') as file: with open(file_path, 'r') as file:
file.write(answer+'\n') 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): if len(parts) == 3:
help_row = row symbol = parts[2]
help_position = position transitions.setdefault(state_from, []).append((symbol, state_to))
next_character_array =[]
bool_value = False
for element in row:
if element=='\n':
if position in what_is_the_ended_positions():
return True
else: else:
return False transitions[state_from] = []
next_character_array = find_next_position(help_position, element)
if next_character_array != []: current_state = 0
if len(next_character_array)>1: word = ""
for element_in_next_character_array in next_character_array:
if bool_value ==True: while current_state in transitions:
possible_transitions = transitions[current_state]
if not possible_transitions:
break 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:
break
return bool_value
transition_symbol, next_state = possible_transitions[0]
word += transition_symbol
current_state = next_state
def find_next_position(position, character): print(f"Достигнуто конечное состояние: {current_state}")
with open(used_table, 'r') as readed_used_table: print(f"Полученное слово: {word}")
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])
return take_all_possible_positions # Указываем путь к файлу
def what_is_the_ended_positions(): file_path = "small.in"
array = []
with open(used_table, 'r') as file: # Запускаем автомат
for row in file: run_automaton(file_path)
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')

4
TaskC06/small.out Normal file
View File

@ -0,0 +1,4 @@
biały
dom
piła
stali

3
TaskC06/small2.out Normal file
View File

@ -0,0 +1,3 @@
biały
piła
stali

57
TaskC06/test.py Normal file
View File

@ -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)