djfz-2019/TaskB01/run

53 lines
1.3 KiB
Plaintext
Raw Normal View History

2019-11-24 21:22:07 +01:00
#!/usr/bin/python3
import sys
import re
2019-11-24 21:27:57 +01:00
class automata:
def __init__(self):
self.storage = []
self.accepting_states = []
self.state = '0'
2019-11-24 21:49:36 +01:00
def __repr__(self):
2019-11-24 21:50:57 +01:00
return('%s\n\n%s\n\n%s\n\n' % (self.storage, self.accepting_states, self.state))
2019-11-24 21:49:36 +01:00
2019-11-24 21:22:07 +01:00
def add_node(self, line):
2019-11-24 21:52:39 +01:00
node = line.replace('\n', '').split(' ')
2019-11-24 21:22:07 +01:00
if len(node) == 3:
self.storage.append([[node[0], node[2]], node[1]])
elif len(node) == 1:
2019-11-24 21:55:19 +01:00
self.accepting_states.append(node[0])
2019-11-24 21:22:07 +01:00
def test_string(self, text):
2019-11-24 21:50:57 +01:00
self.state = '0'
2019-11-24 22:40:55 +01:00
text = text.replace('\n', '')
2019-11-24 21:22:07 +01:00
for i in text:
self.state = self.get_node_transition([self.state, i])
if self.state == None:
2019-11-24 21:31:39 +01:00
return 'FALSE none %s' % text
2019-11-24 21:27:57 +01:00
2019-11-24 21:22:07 +01:00
if self.state in self.accepting_states:
2019-11-24 21:31:39 +01:00
return 'TRUE %s' % text
2019-11-24 21:22:07 +01:00
else:
2019-11-24 21:31:39 +01:00
return 'FALSE %s' % text
2019-11-24 21:22:07 +01:00
def get_node_transition(self, query):
2019-11-24 22:37:26 +01:00
print(query)
2019-11-24 21:22:07 +01:00
for i in self.storage:
if i[0] == query:
return i[1]
return None
2019-11-24 21:27:57 +01:00
auto = automata()
2019-11-24 21:22:07 +01:00
for line in sys.stdin:
auto.add_node(line)
f = open(sys.argv[1], 'r')
for line in f:
print(auto.test_string(line))
2019-11-24 21:50:57 +01:00
print(auto)