From d1bf33f80923974830c1ffa15eb42a70a0e880ed Mon Sep 17 00:00:00 2001 From: Polevara Veronika Date: Thu, 9 Nov 2023 14:16:19 +0100 Subject: [PATCH] wykonane zadania z tasku C --- TaskC00/description.txt | 14 +++++++++ TaskC00/run.py | 64 +++++++++++++++++++++++++++++++++++++++++ TaskC00/testnfa.arg | 7 +++++ TaskC00/testnfa.exp | 8 ++++++ TaskC00/testnfa.in | 8 ++++++ TaskC00/testnfa.out | 8 ++++++ TaskC02/description.txt | 14 +++++++++ TaskC02/run.py | 64 +++++++++++++++++++++++++++++++++++++++++ TaskC02/test.arg | 6 ++++ TaskC02/test.exp | 6 ++++ TaskC02/test.in | 6 ++++ TaskC02/test.out | 6 ++++ 12 files changed, 211 insertions(+) create mode 100644 TaskC00/description.txt create mode 100644 TaskC00/run.py create mode 100644 TaskC00/testnfa.arg create mode 100644 TaskC00/testnfa.exp create mode 100644 TaskC00/testnfa.in create mode 100644 TaskC00/testnfa.out create mode 100644 TaskC02/description.txt create mode 100644 TaskC02/run.py create mode 100644 TaskC02/test.arg create mode 100644 TaskC02/test.exp create mode 100644 TaskC02/test.in create mode 100644 TaskC02/test.out diff --git a/TaskC00/description.txt b/TaskC00/description.txt new file mode 100644 index 0000000..218e0f9 --- /dev/null +++ b/TaskC00/description.txt @@ -0,0 +1,14 @@ +Read a description of a non-deterministic finite-state automaton in the AT&T format +(without weights) from the file in the first argument. + +Read strings from the standard input. +If a string is accepted by the +automaton, write YES, otherwise- write NO. + +The program is invoked like this: python run.py test1.arg < test1.in > test1.out + +Note that not all transitions must be included in description. +If no transition is given for the given state and letter, write NO. + +POINTS: 3 +DEADLINE: 2023-11-12 23:59:59 diff --git a/TaskC00/run.py b/TaskC00/run.py new file mode 100644 index 0000000..d69b60b --- /dev/null +++ b/TaskC00/run.py @@ -0,0 +1,64 @@ +import sys +import copy + +class FSA: + + def __init__(self): + self.initial_state = '0' + self.final_states = set() + + self.transitions = dict() + self.alphabet = set() + + def add_transition(self, state_from, state_to, symbol): + + if state_from in self.transitions.keys(): + if symbol not in self.transitions[state_from].keys(): + self.transitions[state_from][symbol] = {state_to} + else: + self.transitions[state_from][symbol] |= {state_to} + else: + self.transitions[state_from] = dict() + self.transitions[state_from][symbol] = {state_to} + + def add_final_state(self, state): + self.final_states.add(state) + def get_final_states(self, string): + current_states = {self.initial_state} + for symbol in string: + new_current_states = set() + for current_state in current_states: + try: + new_current_states |= self.transitions[current_state][symbol] + except: + pass + current_states = copy.deepcopy(new_current_states) + return current_states + + def accepts(self, string): + for final_state in self.get_final_states(string): + if final_state in self.final_states: + return True + return False + +fsa = FSA() + +table = open(sys.argv[1]) +for line in table: + line = line.rstrip() + + if len(line.split('\t')) == 3: + a, b, c = line.split('\t') + fsa.add_transition(a, b, c) + fsa.alphabet.add(c) + else: + fsa.add_final_state(line) + + +for line in sys.stdin: + line = line.rstrip() + + if fsa.accepts(line): + print('YES') + else: + print('NO') \ No newline at end of file diff --git a/TaskC00/testnfa.arg b/TaskC00/testnfa.arg new file mode 100644 index 0000000..75dc56c --- /dev/null +++ b/TaskC00/testnfa.arg @@ -0,0 +1,7 @@ +0 1 a +1 0 a +1 2 b +2 4 c +1 3 b +3 +4 diff --git a/TaskC00/testnfa.exp b/TaskC00/testnfa.exp new file mode 100644 index 0000000..c98399b --- /dev/null +++ b/TaskC00/testnfa.exp @@ -0,0 +1,8 @@ +YES +YES +NO +NO +NO +YES +NO +YES diff --git a/TaskC00/testnfa.in b/TaskC00/testnfa.in new file mode 100644 index 0000000..91bb05a --- /dev/null +++ b/TaskC00/testnfa.in @@ -0,0 +1,8 @@ +abc +ab +abcd +aaaabc +aaaaaaaabc +aaaaaaabc +zzz +aaaaaaabc diff --git a/TaskC00/testnfa.out b/TaskC00/testnfa.out new file mode 100644 index 0000000..e9d26f6 --- /dev/null +++ b/TaskC00/testnfa.out @@ -0,0 +1,8 @@ +YES +YES +NO +NO +NO +YES +NO +YES diff --git a/TaskC02/description.txt b/TaskC02/description.txt new file mode 100644 index 0000000..55ac747 --- /dev/null +++ b/TaskC02/description.txt @@ -0,0 +1,14 @@ +Use a non deterministic finite-state automaton (FSA) engine from the TaskC00. +Create your own non deterministic FSA description to check whether the string +ends with "ab" +Don't use external files like in TaskF00 (description should be included in run file). + +The alphabet is "a", "b", "C" + +Read strings from the standard input. +If a string is accepted by the +automaton, write YES, otherwise- write NO. + +POINTS: 3 +DEADLINE: 2023-11-12 23:59:59 +REMAINDER: 1/3 diff --git a/TaskC02/run.py b/TaskC02/run.py new file mode 100644 index 0000000..d69b60b --- /dev/null +++ b/TaskC02/run.py @@ -0,0 +1,64 @@ +import sys +import copy + +class FSA: + + def __init__(self): + self.initial_state = '0' + self.final_states = set() + + self.transitions = dict() + self.alphabet = set() + + def add_transition(self, state_from, state_to, symbol): + + if state_from in self.transitions.keys(): + if symbol not in self.transitions[state_from].keys(): + self.transitions[state_from][symbol] = {state_to} + else: + self.transitions[state_from][symbol] |= {state_to} + else: + self.transitions[state_from] = dict() + self.transitions[state_from][symbol] = {state_to} + + def add_final_state(self, state): + self.final_states.add(state) + def get_final_states(self, string): + current_states = {self.initial_state} + for symbol in string: + new_current_states = set() + for current_state in current_states: + try: + new_current_states |= self.transitions[current_state][symbol] + except: + pass + current_states = copy.deepcopy(new_current_states) + return current_states + + def accepts(self, string): + for final_state in self.get_final_states(string): + if final_state in self.final_states: + return True + return False + +fsa = FSA() + +table = open(sys.argv[1]) +for line in table: + line = line.rstrip() + + if len(line.split('\t')) == 3: + a, b, c = line.split('\t') + fsa.add_transition(a, b, c) + fsa.alphabet.add(c) + else: + fsa.add_final_state(line) + + +for line in sys.stdin: + line = line.rstrip() + + if fsa.accepts(line): + print('YES') + else: + print('NO') \ No newline at end of file diff --git a/TaskC02/test.arg b/TaskC02/test.arg new file mode 100644 index 0000000..ca71b55 --- /dev/null +++ b/TaskC02/test.arg @@ -0,0 +1,6 @@ +0 0 a +0 0 b +0 0 c +0 1 a +1 2 b +2 diff --git a/TaskC02/test.exp b/TaskC02/test.exp new file mode 100644 index 0000000..e09bb02 --- /dev/null +++ b/TaskC02/test.exp @@ -0,0 +1,6 @@ +YES +NO +YES +NO +YES +NO diff --git a/TaskC02/test.in b/TaskC02/test.in new file mode 100644 index 0000000..84f5cfd --- /dev/null +++ b/TaskC02/test.in @@ -0,0 +1,6 @@ +ab +a +abbab +bbbbb +ababaab +b diff --git a/TaskC02/test.out b/TaskC02/test.out new file mode 100644 index 0000000..e7f6515 --- /dev/null +++ b/TaskC02/test.out @@ -0,0 +1,6 @@ +YES +NO +YES +NO +YES +NO