diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 89cce22..bf31683 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,8 @@ We własnym repozytorium: ## Zajęcia 1 16.10.2023 Automaty deterministyczne skończone B00 - zadanie wykonywane wspólnie na zajęciach + B01-B04, B06-B09 - po jedno dla każdego + B05 - jedno dla wszystkich diff --git a/TaskB00/run.py b/TaskB00/run.py new file mode 100644 index 0000000..b2a07c7 --- /dev/null +++ b/TaskB00/run.py @@ -0,0 +1,96 @@ +# B00 (2021) +# +import sys + + +class FSA: + + def __init__(self): + self.initial_state = '0' # zakładamy dla uproszczenia, że 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(): + 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_state(self, string): + + current_state = self.initial_state + for symbol in string: + current_state = self.transitions[current_state][symbol] + return current_state + + def accepts(self, string): + + if self.get_final_state(string) in self.final_states: + return True + else: + return False + + +fsa = FSA() + +# def build_fsa(self): #, dfa_desciption +table = open(sys.argv[1]) # dfa_desciption +for line in table: + line = line.rstrip('\n') + if len(line.split('\t')) == 3: + a, b, c = line.split('\t') + fsa.add_transition(a, b, c) #self + fsa.alphabet.add(c) #self + elif len(line.split('\t')) == 1: + fsa.add_final_state(line) #self + else: + assert False + + +# def run_dfa(self): # , input +for line in sys.stdin: # open(input) + line = line.rstrip() + + line_n = list(line) + + for i in range(len(line_n)): + if line_n[i] not in fsa.alphabet: #self + line_n[i] = 'x' + + if fsa.accepts(line_n): #self + print('YES') + else: + print('NO') + + # def run_dfa_and_compare(self, input, expected): + # + # accepts = [] + # + # for line in open(input): # sys.stdin + # line = line.rstrip() + # + # line_n = list(line) + # + # for i in range(len(line_n)): + # if line_n[i] not in self.alphabet: + # line_n[i] = 'x' + # + # if self.accepts(line_n): + # accepts.append('YES') + # else: + # accepts.append('NO') + # + # i = 0 + # for line in open(expected): + # if line.rstrip() != accepts[i]: + # print('Incorrect in line ' + str(i)) + # return + # i = i + 1 + # print('Correct') diff --git a/TaskB01/fsa_description.arg b/TaskB01/fsa_description.arg new file mode 100644 index 0000000..3bf4159 --- /dev/null +++ b/TaskB01/fsa_description.arg @@ -0,0 +1,13 @@ +0 1 0 +0 5 1 +1 5 0 +1 2 1 +2 3 1 +2 4 0 +3 3 1 +3 4 0 +4 4 0 +4 2 1 +5 5 1 +5 5 0 +2 diff --git a/TaskB03/fsa_description.arg b/TaskB03/fsa_description.arg new file mode 100644 index 0000000..05d929e --- /dev/null +++ b/TaskB03/fsa_description.arg @@ -0,0 +1,5 @@ +0 0 1 +0 1 0 +1 1 1 +1 0 0 +0 diff --git a/TaskB03/run.py b/TaskB03/run.py new file mode 100644 index 0000000..b2a07c7 --- /dev/null +++ b/TaskB03/run.py @@ -0,0 +1,96 @@ +# B00 (2021) +# +import sys + + +class FSA: + + def __init__(self): + self.initial_state = '0' # zakładamy dla uproszczenia, że 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(): + 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_state(self, string): + + current_state = self.initial_state + for symbol in string: + current_state = self.transitions[current_state][symbol] + return current_state + + def accepts(self, string): + + if self.get_final_state(string) in self.final_states: + return True + else: + return False + + +fsa = FSA() + +# def build_fsa(self): #, dfa_desciption +table = open(sys.argv[1]) # dfa_desciption +for line in table: + line = line.rstrip('\n') + if len(line.split('\t')) == 3: + a, b, c = line.split('\t') + fsa.add_transition(a, b, c) #self + fsa.alphabet.add(c) #self + elif len(line.split('\t')) == 1: + fsa.add_final_state(line) #self + else: + assert False + + +# def run_dfa(self): # , input +for line in sys.stdin: # open(input) + line = line.rstrip() + + line_n = list(line) + + for i in range(len(line_n)): + if line_n[i] not in fsa.alphabet: #self + line_n[i] = 'x' + + if fsa.accepts(line_n): #self + print('YES') + else: + print('NO') + + # def run_dfa_and_compare(self, input, expected): + # + # accepts = [] + # + # for line in open(input): # sys.stdin + # line = line.rstrip() + # + # line_n = list(line) + # + # for i in range(len(line_n)): + # if line_n[i] not in self.alphabet: + # line_n[i] = 'x' + # + # if self.accepts(line_n): + # accepts.append('YES') + # else: + # accepts.append('NO') + # + # i = 0 + # for line in open(expected): + # if line.rstrip() != accepts[i]: + # print('Incorrect in line ' + str(i)) + # return + # i = i + 1 + # print('Correct') diff --git a/TaskB04/test.out b/TaskB04/test.out new file mode 100644 index 0000000..e69de29 diff --git a/TaskB05/fsa_description.arg b/TaskB05/fsa_description.arg new file mode 100644 index 0000000..9dab448 --- /dev/null +++ b/TaskB05/fsa_description.arg @@ -0,0 +1,56 @@ +0 0 0 +0 1 1 +0 0 2 +0 0 3 +0 0 4 +0 0 5 +0 0 6 +0 0 7 +0 0 8 +0 0 9 +0 0 x +1 0 0 +1 0 1 +1 0 2 +1 0 3 +1 0 4 +1 0 5 +1 0 6 +1 0 7 +1 0 8 +1 2 9 +1 0 x +2 3 0 +2 3 1 +2 3 2 +2 3 3 +2 3 4 +2 3 5 +2 3 6 +2 3 7 +2 3 8 +2 3 9 +2 0 x +3 4 0 +3 4 1 +3 4 2 +3 4 3 +3 4 4 +3 4 5 +3 4 6 +3 4 7 +3 4 8 +3 4 9 +3 0 x +4 4 0 +4 4 1 +4 4 2 +4 4 3 +4 4 4 +4 4 5 +4 4 6 +4 4 7 +4 4 8 +4 4 9 +4 4 x +4 diff --git a/TaskB05/run.py b/TaskB05/run.py new file mode 100644 index 0000000..b2a07c7 --- /dev/null +++ b/TaskB05/run.py @@ -0,0 +1,96 @@ +# B00 (2021) +# +import sys + + +class FSA: + + def __init__(self): + self.initial_state = '0' # zakładamy dla uproszczenia, że 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(): + 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_state(self, string): + + current_state = self.initial_state + for symbol in string: + current_state = self.transitions[current_state][symbol] + return current_state + + def accepts(self, string): + + if self.get_final_state(string) in self.final_states: + return True + else: + return False + + +fsa = FSA() + +# def build_fsa(self): #, dfa_desciption +table = open(sys.argv[1]) # dfa_desciption +for line in table: + line = line.rstrip('\n') + if len(line.split('\t')) == 3: + a, b, c = line.split('\t') + fsa.add_transition(a, b, c) #self + fsa.alphabet.add(c) #self + elif len(line.split('\t')) == 1: + fsa.add_final_state(line) #self + else: + assert False + + +# def run_dfa(self): # , input +for line in sys.stdin: # open(input) + line = line.rstrip() + + line_n = list(line) + + for i in range(len(line_n)): + if line_n[i] not in fsa.alphabet: #self + line_n[i] = 'x' + + if fsa.accepts(line_n): #self + print('YES') + else: + print('NO') + + # def run_dfa_and_compare(self, input, expected): + # + # accepts = [] + # + # for line in open(input): # sys.stdin + # line = line.rstrip() + # + # line_n = list(line) + # + # for i in range(len(line_n)): + # if line_n[i] not in self.alphabet: + # line_n[i] = 'x' + # + # if self.accepts(line_n): + # accepts.append('YES') + # else: + # accepts.append('NO') + # + # i = 0 + # for line in open(expected): + # if line.rstrip() != accepts[i]: + # print('Incorrect in line ' + str(i)) + # return + # i = i + 1 + # print('Correct') diff --git a/TaskB06/shakespeare_ascii_lower.out b/TaskB06/shakespeare_ascii_lower.out new file mode 100644 index 0000000..e69de29 diff --git a/TaskB06/simple.out b/TaskB06/simple.out new file mode 100644 index 0000000..e69de29 diff --git a/TaskX02/run.py b/TaskX02/run.py new file mode 100644 index 0000000..2e43271 --- /dev/null +++ b/TaskX02/run.py @@ -0,0 +1,6 @@ +import sys + +count = 0 +for line in sys.stdin: + count += 1 +print(count) diff --git a/TaskX02/simple.out b/TaskX02/simple.out new file mode 100644 index 0000000..00750ed --- /dev/null +++ b/TaskX02/simple.out @@ -0,0 +1 @@ +3 diff --git a/TaskX03/run.py b/TaskX03/run.py new file mode 100644 index 0000000..8a0eedc --- /dev/null +++ b/TaskX03/run.py @@ -0,0 +1,4 @@ +import sys + +for line in sys.stdin: + print(str(len(line)-1) + ' ' + line, end='')