Zadanie E00
This commit is contained in:
parent
ef5eeefee0
commit
4a5223c08b
73
TaskE00/E00.py
Normal file
73
TaskE00/E00.py
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
class DFA:
|
||||||
|
current_state = None
|
||||||
|
|
||||||
|
def __init__(self, states, alphabet, transition_function, start_state, accept_states):
|
||||||
|
self.states = states
|
||||||
|
self.alphabet = alphabet
|
||||||
|
self.transition_function = transition_function
|
||||||
|
self.start_state = start_state
|
||||||
|
self.accept_states = accept_states
|
||||||
|
self.current_state = start_state
|
||||||
|
return
|
||||||
|
|
||||||
|
def transition_to_state_with_input(self, input_value):
|
||||||
|
if ((self.current_state, input_value) not in self.transition_function.keys()):
|
||||||
|
print((self.current_state, input_value) not in self.transition_function.keys())
|
||||||
|
self.current_state = None
|
||||||
|
return
|
||||||
|
self.current_state = self.transition_function[(self.current_state, input_value)];
|
||||||
|
return
|
||||||
|
|
||||||
|
def in_accept_state(self):
|
||||||
|
return self.current_state in accept_states
|
||||||
|
|
||||||
|
def go_to_initial_state(self):
|
||||||
|
self.current_state = self.start_state
|
||||||
|
return
|
||||||
|
|
||||||
|
def run_with_input_list(self, input_list):
|
||||||
|
self.go_to_initial_state()
|
||||||
|
for inp in input_list:
|
||||||
|
self.transition_to_state_with_input(inp)
|
||||||
|
continue
|
||||||
|
return self.in_accept_state()
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
states = []
|
||||||
|
alphabet = []
|
||||||
|
start_state = 0
|
||||||
|
accept_states = []
|
||||||
|
tf = dict()
|
||||||
|
|
||||||
|
|
||||||
|
def makeTf():
|
||||||
|
with open(sys.argv[1], 'r') as my_file:
|
||||||
|
for line in my_file:
|
||||||
|
arg = line.split(' ')
|
||||||
|
if len(arg) > 2:
|
||||||
|
a = arg[2].rstrip("\n")
|
||||||
|
if a not in alphabet:
|
||||||
|
alphabet.append(a)
|
||||||
|
tf[(int(arg[0]), arg[2].rstrip("\n"))] = int(arg[1])
|
||||||
|
else:
|
||||||
|
accept_states.append(int(arg[0].rstrip("\n")))
|
||||||
|
a = int(arg[0].rstrip("\n"))
|
||||||
|
if a not in states:
|
||||||
|
states.append(a)
|
||||||
|
|
||||||
|
|
||||||
|
makeTf()
|
||||||
|
|
||||||
|
d = DFA(states, alphabet, tf, start_state, accept_states)
|
||||||
|
|
||||||
|
for line in sys.stdin:
|
||||||
|
inp_program = list(line.rstrip("\n"))
|
||||||
|
if d.run_with_input_list(inp_program):
|
||||||
|
print("YES")
|
||||||
|
else:
|
||||||
|
print("NO")
|
0
TaskE00/Makefile
Normal file
0
TaskE00/Makefile
Normal file
@ -1,4 +1,4 @@
|
|||||||
Read a description of a deterministic finite-state automaton in the AT&T format
|
Read a description of a deterministic finite-state automaton in the c
|
||||||
(without weights) from the file in the first argument.
|
(without weights) from the file in the first argument.
|
||||||
|
|
||||||
Read strings from the standard input.
|
Read strings from the standard input.
|
||||||
|
2
TaskE00/run
Executable file
2
TaskE00/run
Executable file
@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
python3 TaskE00/E00.py "$@"
|
Loading…
Reference in New Issue
Block a user