diff --git a/TaskE00/E00.py b/TaskE00/E00.py new file mode 100644 index 0000000..ba7d1a7 --- /dev/null +++ b/TaskE00/E00.py @@ -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") diff --git a/TaskE00/Makefile b/TaskE00/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/TaskE00/description.txt b/TaskE00/description.txt index e1ced0f..1b5e16b 100644 --- a/TaskE00/description.txt +++ b/TaskE00/description.txt @@ -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. Read strings from the standard input. diff --git a/TaskE00/run b/TaskE00/run new file mode 100755 index 0000000..d85fa72 --- /dev/null +++ b/TaskE00/run @@ -0,0 +1,2 @@ +#!/bin/bash +python3 TaskE00/E00.py "$@" \ No newline at end of file