jfz-2023-s464983/TaskB05/run.py

45 lines
1.4 KiB
Python
Raw Normal View History

2023-11-11 20:54:01 +01:00
import sys
def read_aut(fsa_path):
with open(fsa_path, "r", encoding="utf8") as file:
transisions = {}
accepting_states = set()
for line in file:
parts = line.strip().split()
#print(parts)
if len(parts) == 1:
accepting_states.add(int(parts[0]))
#print(accepting_states)
if len(parts) == 3:
state_from = int(parts[0])
state_to = int(parts[1])
symbol = parts[2]
transisions[(state_from, symbol)] = state_to
#print(transisions)
return transisions, accepting_states
#print(transisions)
def is_accepting(transitions, accepting_states, line):
current_state = 0 #zakładam, że zawsze stan początkowy to 0 dla uproszczenia
for symbol in line.strip():
if (current_state, symbol) in transitions:
current_state = transitions[(current_state, symbol)]
#print(current_state)
else:
return "NO"
if current_state in accepting_states:
return "YES"
else:
return "NO"
fsa_path = sys.argv[1]
transitions, accepting_states = read_aut(fsa_path)
for line in sys.stdin:
result = is_accepting(transitions, accepting_states, line)
#sys.stdout.write(result + '\n')
print(result)
#python run.py fsa_description.arg < test1.in > test1.out