djfz-2019/TaskB01/run

83 lines
2.5 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:
2019-11-25 16:39:57 +01:00
# class variables init
2019-11-24 21:27:57 +01:00
def __init__(self):
2019-11-25 16:39:57 +01:00
# dictionary of connections between nodes
2019-11-25 19:43:20 +01:00
self.graph = {}
2019-11-25 16:39:57 +01:00
# list of accepting states
2019-11-24 21:27:57 +01:00
self.accepting_states = []
2019-11-25 16:39:57 +01:00
# list of current states
self.state = ['0']
# print for debug purposes
2019-11-24 21:49:36 +01:00
def __repr__(self):
2019-11-25 19:43:20 +01:00
return('%s\n\n%s\n\n%s\n\n' % (self.graph, self.accepting_states, self.state))
2019-11-25 16:39:57 +01:00
# add node in open fst format
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:
2019-11-25 19:43:20 +01:00
if node[0] in self.graph:
2019-11-25 16:39:57 +01:00
# add value to existing node
2019-11-25 19:43:20 +01:00
self.graph[node[0]].append({node[2]: node[1]})
2019-11-25 16:39:57 +01:00
else:
# create new node
2019-11-25 19:43:20 +01:00
self.graph[node[0]] = [{node[2]: node[1]}]
2019-11-24 21:22:07 +01:00
elif len(node) == 1:
2019-11-25 16:39:57 +01:00
# add accepting state
2019-11-24 21:55:19 +01:00
self.accepting_states.append(node[0])
2019-11-24 21:22:07 +01:00
2019-11-25 16:39:57 +01:00
# check if string is accepted by automate
2019-11-24 21:22:07 +01:00
def test_string(self, text):
2019-11-25 16:39:57 +01:00
self.state = ['0']
2019-11-24 22:41:40 +01:00
text = text.replace('\n', '')
2019-11-25 19:10:41 +01:00
# for all values in text
2019-11-24 21:22:07 +01:00
for i in text:
2019-11-25 19:10:41 +01:00
# for all actual states
2019-11-25 16:39:57 +01:00
for q in self.state:
2019-11-25 19:10:41 +01:00
# move state to its transition
q = self.get_node_transition(q, i)
2019-11-25 19:55:46 +01:00
# if the list is empty, return false
2019-11-25 16:39:57 +01:00
if not self.state:
return False
2019-11-25 19:55:46 +01:00
# flatten list of states
self.state = [item for sublist in self.state for item in sublist]
# check if automata is in accepting state
2019-11-25 16:39:57 +01:00
return self.check_if_accepted()
2019-11-24 21:27:57 +01:00
2019-11-25 19:10:41 +01:00
# check if there is common part between states of automata and accepting states
2019-11-25 16:39:57 +01:00
def check_if_accepted(self):
return not set(self.state).isdisjoint(self.accepting_states)
2019-11-24 21:22:07 +01:00
2019-11-25 19:05:16 +01:00
def get_node_transition(self, q, i):
2019-11-25 19:43:20 +01:00
result = []
# if the node exists
if self.graph[q]:
# search through all its connections to find value
for transition in self.graph[q]:
# if value is like searched for
2019-11-25 19:55:46 +01:00
for key in transition:
if key == i:
# append next node
result.append(transition[key])
2019-11-25 19:43:20 +01:00
# return list of next nodes
return result
2019-11-24 21:22:07 +01:00
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)
2019-11-25 16:39:57 +01:00
print(auto)
2019-11-25 19:16:19 +01:00
f = open(sys.argv[1], 'r')
2019-11-24 21:22:07 +01:00
2019-11-25 19:44:00 +01:00
for line in f:
print(auto.test_string(line))
2019-11-25 19:59:24 +01:00
print(auto)