This commit is contained in:
Eryk Miszczuk 2019-12-13 17:56:57 +01:00
parent 6ae119a40b
commit 21beea4542

View File

@ -4,13 +4,11 @@
import re import re
import sys import sys
# Regex pattern for reading atandt format # Regex pattern for reading atandt format
atandt_desc_pattern = re.compile(r"^([0-9]+)\t([0-9]+)\t(\S+)$") #^([0-9]+)\t([0-9]+)\t(\S+)$ regex for tab separated columns atandt_desc_pattern = re.compile(r"^([0-9]+)[ \t]([0-9]+)[ \t](\S+)") #^([0-9]+)\t([0-9]+)\t(\S+)$ regex for tab separated columns
atandt_accepted_state_pattern = re.compile(r"^[0-9]+$") atandt_accepted_state_pattern = re.compile(r"^[0-9]+$")
current_state = 0 current_state = 0
accept_states = set() accept_states = set()
visited_nodes = []
state_move = set() state_move = set()
# function that filters set by starting node and returns all edges that starts in that node # function that filters set by starting node and returns all edges that starts in that node
@ -20,29 +18,33 @@ def filter_set(xs, filtered_value):
def dfs(graph, start): def dfs(graph, start):
visited = set() visited = set()
stack = set() stack = []
for el in filter_set(state_move, 0): for el in filter_set(state_move, 0):
stack.add(el) stack.append(el)
while stack: while stack:
edge = stack.pop() edge = stack.pop()
#print('Current edge: {edge}'.format(edge = edge)) #print('Current edge: {edge}'.format(edge = edge))
#return immediately if cycle is detected #return immediately if cycle is detected
if edge[0] == edge[2]: if edge[0] == edge[2]:
return [-1] return [-1]
if edge[2] in accept_states:
visited = set()
if edge not in visited: if edge not in visited:
visited.add(edge) visited.add(edge)
children = set() children = set()
for val in filter_set(state_move, edge[2]): for val in filter_set(state_move, edge[2]):
stack.add(val) stack.append(val)
#print('Stack: {stack}'.format(stack = stack)) #print('Stack: {stack}'.format(stack = stack))
#print('Visited: {visited}'.format(visited = visited)) #print('Visited: {visited}'.format(visited = visited))
else: else:
return [-1] return [-2]
return visited return visited
#print("Start program")
# load atand format from stdin # load atand format from stdin
for line in sys.stdin: for line in sys.stdin:
line = line.replace('\n', '') line = line.replace('\n', '')
#print(line)
move = atandt_desc_pattern.match(line) move = atandt_desc_pattern.match(line)
succes = atandt_accepted_state_pattern.match(line) succes = atandt_accepted_state_pattern.match(line)
if move: if move:
@ -54,7 +56,12 @@ for line in sys.stdin:
#print('Accept states: {accept_states}'.format(accept_states = accept_states)) #print('Accept states: {accept_states}'.format(accept_states = accept_states))
vis = dfs(state_move, 0) vis = dfs(state_move, 0)
#print(vis)
# if edge start and end node is the same
if -1 in vis: if -1 in vis:
print("TAK") print("TAK")
# if edge is in visited
elif -2 in vis:
print("TAK")
else: else:
print("NIE") print("NIE")