Zmiana C-Task

This commit is contained in:
Veronika Polevara 2023-12-10 23:02:50 +01:00
parent 8e3bdb1cc6
commit 013cf7f822
2 changed files with 74 additions and 0 deletions

68
TaskC02/run.py Normal file
View File

@ -0,0 +1,68 @@
import sys
import copy
class FSA:
def __init__(self):
self.initial_state = '0'
self.final_states = set()
self.transitions = dict()
self.alphabet = set()
def add_transition(self, state_from, state_to, symbol):
if state_from in self.transitions.keys():
if symbol not in self.transitions[state_from].keys():
self.transitions[state_from][symbol] = {state_to}
else:
self.transitions[state_from][symbol] |= {state_to}
else:
self.transitions[state_from] = dict()
self.transitions[state_from][symbol] = {state_to}
def add_final_state(self, state):
self.final_states.add(state)
def get_final_states(self, string):
current_states = {self.initial_state}
for symbol in string:
new_current_states = set()
for current_state in current_states:
try:
new_current_states |= self.transitions[current_state][symbol]
except:
pass
current_states = copy.deepcopy(new_current_states)
return current_states
def accepts(self, string):
final_states = self.get_final_states(string)
for final_state in final_states:
if final_state in self.final_states:
return True
return False
fsa = FSA()
table = open(sys.argv[1])
for line in table:
line = line.rstrip()
if len(line.split('\t')) == 3:
a, b, c = line.split('\t')
fsa.add_transition(a, b, c)
fsa.alphabet.add(c)
else:
fsa.add_final_state(line)
for line in sys.stdin:
line = line.rstrip()
if fsa.accepts(line):
print('YES')
else:
print('NO')

6
TaskC02/test.out Normal file
View File

@ -0,0 +1,6 @@
YES
NO
YES
NO
YES
NO