jfz-2023-s473579/TaskC06/run.py

40 lines
1.2 KiB
Python
Raw Normal View History

2023-11-24 23:19:27 +01:00
def run_automaton(file_path):
transitions = {}
2023-11-16 21:40:35 +01:00
2023-11-24 23:19:27 +01:00
# Чтение информации из файла
with open(file_path, 'r') as file:
lines = file.readlines()
2023-11-16 21:40:35 +01:00
2023-11-24 23:19:27 +01:00
for line in lines:
if line.strip(): # Пропускаем пустые строки
parts = line.split()
state_from = int(parts[0])
state_to = int(parts[1])
2023-11-16 21:40:35 +01:00
2023-11-24 23:19:27 +01:00
if len(parts) == 3:
symbol = parts[2]
transitions.setdefault(state_from, []).append((symbol, state_to))
2023-11-16 21:40:35 +01:00
else:
2023-11-24 23:19:27 +01:00
transitions[state_from] = []
current_state = 0
word = ""
while current_state in transitions:
possible_transitions = transitions[current_state]
if not possible_transitions:
2023-11-16 21:40:35 +01:00
break
2023-11-24 23:19:27 +01:00
transition_symbol, next_state = possible_transitions[0]
word += transition_symbol
current_state = next_state
print(f"Достигнуто конечное состояние: {current_state}")
print(f"Полученное слово: {word}")
2023-11-16 21:40:35 +01:00
2023-11-24 23:19:27 +01:00
# Указываем путь к файлу
file_path = "small.in"
2023-11-16 21:40:35 +01:00
2023-11-24 23:19:27 +01:00
# Запускаем автомат
run_automaton(file_path)