add deterministic fsa
This commit is contained in:
parent
904af64f6f
commit
43b3aa2b1c
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
*out
|
@ -34,3 +34,10 @@ Do repo proszę dodawać wyłącznie plik `run.py` w odpowiednim katalogu, chyba
|
||||
Proszę również nie modyfikować innych plików.
|
||||
|
||||
Wszystkie zadania należy robić w terminie zaznaczonym w `description.txt`. Po terminie będę podawał punktację za pomocą USUSa w "sprawdziany".
|
||||
|
||||
|
||||
#### Aktualizacja repozytorium
|
||||
|
||||
We własnym repozytorium:
|
||||
|
||||
`git pull git@git.wmi.amu.edu.pl:kubapok/djfz-2021.git`
|
||||
|
11
TaskB00/description.txt
Normal file
11
TaskB00/description.txt
Normal file
@ -0,0 +1,11 @@
|
||||
Read a description of a 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: ./run fsa_description.arg < test1.in > test1.out
|
||||
|
||||
POINTS: 3
|
||||
DEADLINE: 2020-11-07 23:59:00
|
16
TaskB00/fsa_description.arg
Normal file
16
TaskB00/fsa_description.arg
Normal file
@ -0,0 +1,16 @@
|
||||
0 1 x
|
||||
1 2 y
|
||||
2 3 z
|
||||
0 4 y
|
||||
0 4 z
|
||||
1 4 x
|
||||
1 4 z
|
||||
2 4 x
|
||||
2 4 y
|
||||
3 4 x
|
||||
3 4 y
|
||||
3 4 z
|
||||
4 4 x
|
||||
4 4 y
|
||||
4 4 z
|
||||
3
|
79
TaskB00/run.py
Executable file
79
TaskB00/run.py
Executable file
@ -0,0 +1,79 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import sys
|
||||
|
||||
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():
|
||||
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()
|
||||
# fsa.add_transition(0, 1, 'a')
|
||||
# fsa.add_transition(1, 2, 'b')
|
||||
# fsa.add_transition(2, 3, 'c')
|
||||
# fsa.add_final_state(3)
|
||||
# fsa.add_final_state(2)
|
||||
#
|
||||
# print(fsa.accepts('abc'))
|
||||
# print(fsa.accepts('ab'))
|
||||
# print(fsa.accepts('abd'))
|
||||
|
||||
table = open(sys.argv[1])
|
||||
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)
|
||||
fsa.alphabet.add(c)
|
||||
elif len(line.split('\t')) == 1:
|
||||
fsa.add_final_state(line)
|
||||
else:
|
||||
assert False
|
||||
|
||||
|
||||
for line in sys.stdin:
|
||||
line = line.rstrip()
|
||||
|
||||
line_n = list(line)
|
||||
|
||||
for i in range(len(line_n)):
|
||||
if line_n[i] not in fsa.alphabet:
|
||||
line_n[i] = 'x'
|
||||
|
||||
if fsa.accepts(line_n):
|
||||
print('YES')
|
||||
else:
|
||||
print('NO')
|
||||
|
9
TaskB00/test1.exp
Normal file
9
TaskB00/test1.exp
Normal file
@ -0,0 +1,9 @@
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
NO
|
||||
NO
|
||||
NO
|
||||
NO
|
||||
NO
|
||||
NO
|
9
TaskB00/test1.in
Normal file
9
TaskB00/test1.in
Normal file
@ -0,0 +1,9 @@
|
||||
xxyz
|
||||
xyz
|
||||
xy
|
||||
zz
|
||||
xxy
|
||||
yzx
|
||||
|
||||
x
|
||||
xyzz
|
13
TaskB01/description.txt
Normal file
13
TaskB01/description.txt
Normal file
@ -0,0 +1,13 @@
|
||||
Use a deterministic finite-state automaton (FSA) engine from the TaskE00.
|
||||
Create your own FSA description to check whether the string starts with "01" and ends with "01.
|
||||
Save it to fsa_description.arg file.
|
||||
|
||||
The alphabet is "0", "1".
|
||||
|
||||
Read strings from the standard input.
|
||||
If a string is accepted by the
|
||||
automaton, write YES, otherwise- write NO.
|
||||
|
||||
POINTS: 2
|
||||
DEADLINE: 2020-11-07 23:59:00
|
||||
REMAINDER: 1/4
|
14
TaskB01/test.exp
Normal file
14
TaskB01/test.exp
Normal file
@ -0,0 +1,14 @@
|
||||
YES
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
NO
|
||||
NO
|
||||
NO
|
||||
NO
|
||||
NO
|
14
TaskB01/test.in
Normal file
14
TaskB01/test.in
Normal file
@ -0,0 +1,14 @@
|
||||
01
|
||||
10
|
||||
0101
|
||||
1010
|
||||
011101
|
||||
101010
|
||||
100010
|
||||
0100001
|
||||
|
||||
00110
|
||||
0000
|
||||
10101
|
||||
0
|
||||
1
|
13
TaskB02/description.txt
Normal file
13
TaskB02/description.txt
Normal file
@ -0,0 +1,13 @@
|
||||
Use a deterministic finite-state automaton (FSA) engine from the TaskE00.
|
||||
Create your own FSA description to check whether the string starts with "10" and ends with "10.
|
||||
Save it to fsa_description.arg file.
|
||||
|
||||
The alphabet is "0", "1".
|
||||
|
||||
Read strings from the standard input.
|
||||
If a string is accepted by the
|
||||
automaton, write YES, otherwise- write NO.
|
||||
|
||||
POINTS: 2
|
||||
DEADLINE: 2020-11-07 23:59:00
|
||||
REMAINDER: 2/4
|
14
TaskB02/test.exp
Normal file
14
TaskB02/test.exp
Normal file
@ -0,0 +1,14 @@
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
YES
|
||||
YES
|
||||
NO
|
||||
NO
|
||||
NO
|
||||
NO
|
||||
NO
|
||||
NO
|
||||
NO
|
14
TaskB02/test.in
Normal file
14
TaskB02/test.in
Normal file
@ -0,0 +1,14 @@
|
||||
01
|
||||
10
|
||||
0101
|
||||
1010
|
||||
011101
|
||||
101010
|
||||
100010
|
||||
0100001
|
||||
|
||||
00110
|
||||
0000
|
||||
10101
|
||||
0
|
||||
1
|
14
TaskB03/description.txt
Normal file
14
TaskB03/description.txt
Normal file
@ -0,0 +1,14 @@
|
||||
Use a deterministic finite-state automaton (FSA) engine from the TaskE00.
|
||||
Create your own FSA description to check whether the string contains "0"
|
||||
even number of times.
|
||||
Save it to fsa_description.arg file.
|
||||
|
||||
The alphabet is "0", "1".
|
||||
|
||||
Read strings from the standard input.
|
||||
If a string is accepted by the
|
||||
automaton, write YES, otherwise- write NO.
|
||||
|
||||
POINTS: 2
|
||||
DEADLINE: 2020-11-07 23:59:00
|
||||
REMAINDER: 3/4
|
14
TaskB03/test.exp
Normal file
14
TaskB03/test.exp
Normal file
@ -0,0 +1,14 @@
|
||||
NO
|
||||
NO
|
||||
YES
|
||||
YES
|
||||
YES
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
YES
|
||||
YES
|
||||
NO
|
||||
YES
|
14
TaskB03/test.in
Normal file
14
TaskB03/test.in
Normal file
@ -0,0 +1,14 @@
|
||||
01
|
||||
10
|
||||
0101
|
||||
1010
|
||||
011101
|
||||
101010
|
||||
100010
|
||||
0100001
|
||||
|
||||
00110
|
||||
0000
|
||||
10101
|
||||
0
|
||||
1
|
14
TaskB04/description.txt
Normal file
14
TaskB04/description.txt
Normal file
@ -0,0 +1,14 @@
|
||||
Use a deterministic finite-state automaton (FSA) engine from the TaskE00.
|
||||
Create your own FSA description to check whether the string contains "0"
|
||||
odd number of times.
|
||||
Save it to fsa_description.arg file.
|
||||
|
||||
The alphabet is "0", "1".
|
||||
|
||||
Read strings from the standard input.
|
||||
If a string is accepted by the
|
||||
automaton, write YES, otherwise- write NO.
|
||||
|
||||
POINTS: 2
|
||||
DEADLINE: 2020-11-07 23:59:00
|
||||
REMAINDER: 0/4
|
14
TaskB04/test.exp
Normal file
14
TaskB04/test.exp
Normal file
@ -0,0 +1,14 @@
|
||||
YES
|
||||
YES
|
||||
NO
|
||||
NO
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
NO
|
||||
YES
|
||||
NO
|
14
TaskB04/test.in
Normal file
14
TaskB04/test.in
Normal file
@ -0,0 +1,14 @@
|
||||
01
|
||||
10
|
||||
0101
|
||||
1010
|
||||
011101
|
||||
101010
|
||||
100010
|
||||
0100001
|
||||
|
||||
00110
|
||||
0000
|
||||
10101
|
||||
0
|
||||
1
|
12
TaskB05/description.txt
Normal file
12
TaskB05/description.txt
Normal file
@ -0,0 +1,12 @@
|
||||
Use a deterministic finite-state automaton (FSA) engine from the TaskB00.
|
||||
Create your own FSA description to check whether the line contains string '19DD', where D is a digit.
|
||||
Save it to fsa_description.arg file.
|
||||
|
||||
FSA alphabet is '0123456789x'.
|
||||
|
||||
Read strings from the standard input.
|
||||
If a string is accepted by the
|
||||
automaton, write YES, otherwise- write NO.
|
||||
|
||||
POINTS: 2
|
||||
DEADLINE: 2020-11-07 23:59:00
|
50000
TaskB05/polish_wiki_excerpt_only_digits.exp
Normal file
50000
TaskB05/polish_wiki_excerpt_only_digits.exp
Normal file
File diff suppressed because it is too large
Load Diff
50000
TaskB05/polish_wiki_excerpt_only_digits.in
Normal file
50000
TaskB05/polish_wiki_excerpt_only_digits.in
Normal file
File diff suppressed because one or more lines are too long
6
TaskB05/simple.exp
Normal file
6
TaskB05/simple.exp
Normal file
@ -0,0 +1,6 @@
|
||||
NO
|
||||
YES
|
||||
NO
|
||||
NO
|
||||
YES
|
||||
YES
|
6
TaskB05/simple.in
Normal file
6
TaskB05/simple.in
Normal file
@ -0,0 +1,6 @@
|
||||
3214545443
|
||||
1910
|
||||
19
|
||||
xxx2190x
|
||||
xxx21905x
|
||||
1905x54545
|
13
TaskB06/description.txt
Normal file
13
TaskB06/description.txt
Normal file
@ -0,0 +1,13 @@
|
||||
Use a deterministic finite-state automaton (FSA) engine from the previous task.
|
||||
Create your own FSA description to check whether the word "hamlet" is in the given line.
|
||||
Save it to fsa_description.arg file.
|
||||
|
||||
FSA alphabet is 'abcdefghijklmnopqrstuvwxyz '.
|
||||
|
||||
Read strings from the standard input.
|
||||
If a string is accepted by the
|
||||
automaton, write YES, otherwise- write NO.
|
||||
|
||||
POINTS: 3
|
||||
DEADLINE: 2020-11-07 23:59:00
|
||||
REMAINDER: 0/4
|
169442
TaskB06/shakespeare_ascii_lower.exp
Normal file
169442
TaskB06/shakespeare_ascii_lower.exp
Normal file
File diff suppressed because it is too large
Load Diff
169442
TaskB06/shakespeare_ascii_lower.in
Normal file
169442
TaskB06/shakespeare_ascii_lower.in
Normal file
File diff suppressed because it is too large
Load Diff
3
TaskB06/simple.exp
Normal file
3
TaskB06/simple.exp
Normal file
@ -0,0 +1,3 @@
|
||||
NO
|
||||
YES
|
||||
YES
|
3
TaskB06/simple.in
Normal file
3
TaskB06/simple.in
Normal file
@ -0,0 +1,3 @@
|
||||
haml
|
||||
hamlet
|
||||
aaahamletbbb
|
13
TaskB07/description.txt
Normal file
13
TaskB07/description.txt
Normal file
@ -0,0 +1,13 @@
|
||||
Use a deterministic finite-state automaton (FSA) engine from the previous task.
|
||||
Create your own FSA description to check whether the word "ophelia" is in the given line.
|
||||
Save it to fsa_description.arg file.
|
||||
|
||||
FSA alphabet is 'abcdefghijklmnopqrstuvwxyz '.
|
||||
|
||||
Read strings from the standard input.
|
||||
If a string is accepted by the
|
||||
automaton, write YES, otherwise- write NO.
|
||||
|
||||
POINTS: 3
|
||||
DEADLINE: 2020-11-07 23:59:00
|
||||
REMAINDER: 1/4
|
169442
TaskB07/shakespeare_ascii_lower.exp
Normal file
169442
TaskB07/shakespeare_ascii_lower.exp
Normal file
File diff suppressed because it is too large
Load Diff
1
TaskB07/shakespeare_ascii_lower.in
Symbolic link
1
TaskB07/shakespeare_ascii_lower.in
Symbolic link
@ -0,0 +1 @@
|
||||
../TaskB06/shakespeare_ascii_lower.in
|
3
TaskB07/simple.exp
Normal file
3
TaskB07/simple.exp
Normal file
@ -0,0 +1,3 @@
|
||||
NO
|
||||
YES
|
||||
YES
|
3
TaskB07/simple.in
Normal file
3
TaskB07/simple.in
Normal file
@ -0,0 +1,3 @@
|
||||
oph
|
||||
ophelia
|
||||
xfdfdopheliafff
|
13
TaskB08/description.txt
Normal file
13
TaskB08/description.txt
Normal file
@ -0,0 +1,13 @@
|
||||
Use a deterministic finite-state automaton (FSA) engine from the previous task.
|
||||
Create your own FSA description to check whether the word "juliet" is in the given line.
|
||||
Save it to fsa_description.arg file.
|
||||
|
||||
FSA alphabet is 'abcdefghijklmnopqrstuvwxyz '.
|
||||
|
||||
Read strings from the standard input.
|
||||
If a string is accepted by the
|
||||
automaton, write YES, otherwise- write NO.
|
||||
|
||||
POINTS: 3
|
||||
DEADLINE: 2020-11-07 23:59:00
|
||||
REMAINDER: 2/4
|
169442
TaskB08/shakespeare_ascii_lower.exp
Normal file
169442
TaskB08/shakespeare_ascii_lower.exp
Normal file
File diff suppressed because it is too large
Load Diff
1
TaskB08/shakespeare_ascii_lower.in
Symbolic link
1
TaskB08/shakespeare_ascii_lower.in
Symbolic link
@ -0,0 +1 @@
|
||||
../TaskB06/shakespeare_ascii_lower.in
|
3
TaskB08/simple.exp
Normal file
3
TaskB08/simple.exp
Normal file
@ -0,0 +1,3 @@
|
||||
NO
|
||||
YES
|
||||
YES
|
3
TaskB08/simple.in
Normal file
3
TaskB08/simple.in
Normal file
@ -0,0 +1,3 @@
|
||||
juli
|
||||
juliet
|
||||
dgfdgjulietaaa
|
13
TaskB09/description.txt
Normal file
13
TaskB09/description.txt
Normal file
@ -0,0 +1,13 @@
|
||||
Use a deterministic finite-state automaton (FSA) engine from the previous task.
|
||||
Create your own FSA description to check whether the word "macbeth" is in the given line.
|
||||
Save it to fsa_description.arg file.
|
||||
|
||||
FSA alphabet is 'abcdefghijklmnopqrstuvwxyz '.
|
||||
|
||||
Read strings from the standard input.
|
||||
If a string is accepted by the
|
||||
automaton, write YES, otherwise- write NO.
|
||||
|
||||
POINTS: 3
|
||||
DEADLINE: 2020-11-07 23:59:00
|
||||
REMAINDER: 3/4
|
79
TaskB09/run.py
Normal file
79
TaskB09/run.py
Normal file
@ -0,0 +1,79 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import sys
|
||||
|
||||
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():
|
||||
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()
|
||||
# fsa.add_transition(0, 1, 'a')
|
||||
# fsa.add_transition(1, 2, 'b')
|
||||
# fsa.add_transition(2, 3, 'c')
|
||||
# fsa.add_final_state(3)
|
||||
# fsa.add_final_state(2)
|
||||
#
|
||||
# print(fsa.accepts('abc'))
|
||||
# print(fsa.accepts('ab'))
|
||||
# print(fsa.accepts('abd'))
|
||||
|
||||
table = open(sys.argv[1])
|
||||
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)
|
||||
fsa.alphabet.add(c)
|
||||
elif len(line.split('\t')) == 1:
|
||||
fsa.add_final_state(line)
|
||||
else:
|
||||
assert False
|
||||
|
||||
|
||||
for line in sys.stdin:
|
||||
line = line.rstrip()
|
||||
|
||||
line_n = list(line)
|
||||
|
||||
for i in range(len(line_n)):
|
||||
if line_n[i] not in fsa.alphabet:
|
||||
line_n[i] = 'x'
|
||||
|
||||
if fsa.accepts(line_n):
|
||||
print('YES')
|
||||
else:
|
||||
print('NO')
|
||||
|
169442
TaskB09/shakespeare_ascii_lower.exp
Normal file
169442
TaskB09/shakespeare_ascii_lower.exp
Normal file
File diff suppressed because it is too large
Load Diff
1
TaskB09/shakespeare_ascii_lower.in
Symbolic link
1
TaskB09/shakespeare_ascii_lower.in
Symbolic link
@ -0,0 +1 @@
|
||||
../TaskB06/shakespeare_ascii_lower.in
|
3
TaskB09/simple.exp
Normal file
3
TaskB09/simple.exp
Normal file
@ -0,0 +1,3 @@
|
||||
NO
|
||||
YES
|
||||
YES
|
3
TaskB09/simple.in
Normal file
3
TaskB09/simple.in
Normal file
@ -0,0 +1,3 @@
|
||||
macb
|
||||
macbeth
|
||||
xadadamacbethrff
|
@ -9,7 +9,11 @@ def execute_task(dir):
|
||||
for task_set in task_sets:
|
||||
try:
|
||||
with open(Path(dir,f'{task_set}.in')) as f_in, open(Path(dir,f'{task_set}.out'), 'w') as f_out:
|
||||
process = subprocess.Popen(['python3' ,Path(dir,'run.py')],
|
||||
arg = [x for x in dir.iterdir() if '.arg' == x.suffix]
|
||||
command = ['python3' ,Path(dir,'run.py')]
|
||||
if len(arg) != 0:
|
||||
command.append(arg[0])
|
||||
process = subprocess.Popen(command,
|
||||
stdin=f_in,
|
||||
stdout=f_out,
|
||||
stderr=subprocess.DEVNULL)
|
||||
|
Loading…
Reference in New Issue
Block a user