djfz-2019/TaskB01/run
deadsmond 4f253b4866 'hm'
2019-11-25 21:30:04 +01:00

51 lines
1.2 KiB
Python

#!/usr/bin/python3
import sys
import re
class automata:
def __init__(self):
self.storage = []
self.accepting_states = []
self.state = '0'
def __repr__(self):
return('%s\n\n%s\n\n%s\n\n' % (self.storage, self.accepting_states, self.state))
def add_node(self, line):
node = line.replace('\n', '').split(' ')
if len(node) == 3:
self.storage.append([[node[0], node[2]], node[1]])
elif len(node) == 1:
self.accepting_states.append(node[0])
def test_string(self, text):
self.state = '0'
text = text.replace('\n', '')
for i in text:
self.state = self.get_node_transition([self.state, i])
if self.state == None:
return 'FALSE %s' % text
if self.state in self.accepting_states:
return 'TRUE %s' % text
else:
return 'FALSE %s' % text
def get_node_transition(self, query):
for i in self.storage:
if i[0] == query:
return i[1]
return None
auto = automata()
for line in sys.stdin:
auto.add_node(line)
f = open(sys.argv[1], 'r')
for line in f:
print(auto.test_string(line))