From 056892ad6add13f93fa656277c202dfc38795a7a Mon Sep 17 00:00:00 2001 From: deadsmond <01.lewicki@gmail.com> Date: Tue, 3 Dec 2019 13:09:51 +0100 Subject: [PATCH] 'hm' --- TaskB04/Makefile | 0 TaskB04/description.txt | 2 +- TaskX04/Makefile | 0 automate.class.py | 66 ++++++++++++++++++++++++++++++ automate.nondet.class.py | 86 ++++++++++++++++++++++++++++++++++++++++ lista_zadań.txt | 4 +- shortcut.bat | 2 +- 7 files changed, 156 insertions(+), 4 deletions(-) create mode 100644 TaskB04/Makefile create mode 100644 TaskX04/Makefile create mode 100644 automate.class.py create mode 100644 automate.nondet.class.py diff --git a/TaskB04/Makefile b/TaskB04/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/TaskB04/description.txt b/TaskB04/description.txt index 3f9243c..38dd349 100644 --- a/TaskB04/description.txt +++ b/TaskB04/description.txt @@ -1,7 +1,7 @@ Ścieżki ======= -Program powinien wczytać automat skończeniestanowy (bez wag) ze +Program powinien wczytać automat skończenie stanowy (bez wag) ze standardowego wejścia. Zakładamy, że automat jest deterministyczny i nie zawiera cykli. Alfabet automatu stanowią litery języka polskiego. diff --git a/TaskX04/Makefile b/TaskX04/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/automate.class.py b/automate.class.py new file mode 100644 index 0000000..f906ba0 --- /dev/null +++ b/automate.class.py @@ -0,0 +1,66 @@ +#!/usr/bin/python3 +import sys +import re + + +class automata: + # class variables init + def __init__(self): + # dictionary of connections between nodes + self.storage = {} + # list of accepting states + self.accepting_states = [] + # list of current states + self.state = ['0'] + + # print for debug purposes + def __repr__(self): + return('%s\n\n%s\n\n%s\n\n' % (self.storage, self.accepting_states, self.state)) + + # add node in open fst format + def add_node(self, line): + node = line.replace('\n', '').split(' ') + if len(node) == 3: + if node[0] in self.storage: + # add value to existing node + self.storage[node[0]].append({node[2]: node[1]}) + else: + # create new node + self.storage[node[0]] = [{node[2]: node[1]}] + elif len(node) == 1: + # add accepting state + self.accepting_states.append(node[0]) + + # check if string is accepted by automate + def test_string(self, text): + self.state = ['0'] + text = text.replace('\n', '') + for i in text: + for q in self.state: + self.state = self.get_node_transition(q, i) + if not self.state: + return False + return self.check_if_accepted() + + def check_if_accepted(self): + return not set(self.state).isdisjoint(self.accepting_states) + + def get_node_transition(self, query): + for i in self.storage: + if i[0] == query: + return i[1] + return [] + + def sort(self): + self.storage = sorted(self.storage, key = lambda x: (x[0][0], x[0][1])) + + +auto = automata() + +for line in sys.stdin: + auto.add_node(line) + +f = open(sys.argv[1], 'r') + +for line in f: + print(auto.test_string(line)) diff --git a/automate.nondet.class.py b/automate.nondet.class.py new file mode 100644 index 0000000..1b9110e --- /dev/null +++ b/automate.nondet.class.py @@ -0,0 +1,86 @@ +#!/usr/bin/python3 +import sys +import re + + +class automata: + # class variables init + def __init__(self): + # dictionary of connections between nodes + self.graph = {} + # list of accepting states + self.accepting_states = [] + # list of current states + self.state = ['0'] + # word variable + self.word = '' + + # print for debug purposes + def __repr__(self): + return('%s\n\n%s\n\n%s\n\n' % (self.graph, self.accepting_states, self.state)) + + # add node in open fst format + def add_node(self, line): + node = line.replace('\n', '').split(' ') + if len(node) == 3: + if node[0] in self.graph: + # add value to existing node + self.graph[node[0]].append({node[2]: node[1]}) + else: + # create new node + self.graph[node[0]] = [{node[2]: node[1]}] + elif len(node) == 1: + # add accepting state + self.accepting_states.append(node[0]) + + # check if string is accepted by automate + def test_string(self, word): + self.state = ['0'] + self.word = word.replace('\n', '') + # for all values in word + for i in self.word: + # for all actual states + result = [] + for q in self.state: + # move state to its transition + result = self.get_node_transition(q, i) + # if the list is empty, return false + if not self.state: + return False + # flatten list of states + self.state = [item for sublist in result for item in sublist] + # check if automata is in accepting state + return self.check_if_accepted() + + # check if there is common part between states of automata and accepting states + def check_if_accepted(self): + return not set(self.state).isdisjoint(self.accepting_states) + + def get_node_transition(self, q, i): + result = [] + # if the node exists + try: + # search through all its connections to find value + for connections in self.graph[q]: + for value in connections: + if value == i: + # append next node + result.append(connections[value]) + except KeyError: + return result + # return list of next nodes + return result + + +auto = automata() + +for line in sys.stdin: + auto.add_node(line) + +f = open(sys.argv[1], 'r') + +for line in f: + if auto.test_string(line): + print("TRUE %s" % auto.word) + else: + print("FALSE %s" % auto.word) diff --git a/lista_zadań.txt b/lista_zadań.txt index cf1d1d3..077d9ef 100644 --- a/lista_zadań.txt +++ b/lista_zadań.txt @@ -1,2 +1,2 @@ -2: A00 A13 A42 A45 -2: A00 A13 A40 A45 \ No newline at end of file +2: A00 A13 A40 A45 +3: B01 B03 diff --git a/shortcut.bat b/shortcut.bat index dadf5ca..c0d589b 100644 --- a/shortcut.bat +++ b/shortcut.bat @@ -1,3 +1,3 @@ -git add TaskA%1/run +git add Task%1/run git commit -m 'hm' git push origin master \ No newline at end of file