zadania c
This commit is contained in:
parent
cd74536d74
commit
798ea9440e
@ -1,4 +1,3 @@
|
||||
# automat akceptuje dwa napisy - "aaaa" i "a" powielone 4038 razy
|
||||
0 1 a
|
||||
1 2 a
|
||||
2 3 a
|
||||
|
41
TaskC00/run.py
Normal file
41
TaskC00/run.py
Normal file
@ -0,0 +1,41 @@
|
||||
import sys
|
||||
|
||||
def load_data(file_path):
|
||||
nfa = {}
|
||||
with open(file_path, 'r') as file:
|
||||
for line in file:
|
||||
split_data = line.strip().split('\t')
|
||||
if len(split_data) == 3:
|
||||
state, next_state, symbol = split_data
|
||||
key = (int(state), symbol)
|
||||
if key in nfa:
|
||||
nfa[key].add(int(next_state))
|
||||
else:
|
||||
nfa[key] = {int(next_state)}
|
||||
|
||||
return nfa
|
||||
|
||||
def is_accepted(nfa, input, current_states, accepting_states):
|
||||
for symbol in input:
|
||||
new_states = set()
|
||||
for state in current_states:
|
||||
key = (state, symbol)
|
||||
if key in nfa:
|
||||
new_states.update(nfa[key])
|
||||
current_states = new_states
|
||||
|
||||
return any(state in accepting_states for state in current_states)
|
||||
|
||||
# Set of accepting states
|
||||
accepting_states = {3,4}
|
||||
|
||||
nfa_file = sys.argv[1]
|
||||
nfa = load_data(nfa_file)
|
||||
|
||||
for line in sys.stdin:
|
||||
input_str = line.strip()
|
||||
start_states = {0}
|
||||
if is_accepted(nfa, input_str, start_states, accepting_states):
|
||||
print("YES")
|
||||
else:
|
||||
print("NO")
|
BIN
TaskC00/test.exp
Normal file
BIN
TaskC00/test.exp
Normal file
Binary file not shown.
41
TaskC01/run.py
Normal file
41
TaskC01/run.py
Normal file
@ -0,0 +1,41 @@
|
||||
import sys
|
||||
|
||||
def load_data(file_path):
|
||||
nfa = {}
|
||||
with open(file_path, 'r') as file:
|
||||
for line in file:
|
||||
split_data = line.strip().split(' ')
|
||||
if len(split_data) == 3:
|
||||
state, next_state, symbol = split_data
|
||||
key = (int(state), symbol)
|
||||
if key in nfa:
|
||||
nfa[key].add(int(next_state))
|
||||
else:
|
||||
nfa[key] = {int(next_state)}
|
||||
|
||||
return nfa
|
||||
|
||||
def is_accepted(nfa, input, current_states, accepting_states):
|
||||
for symbol in input:
|
||||
new_states = set()
|
||||
for state in current_states:
|
||||
key = (state, symbol)
|
||||
if key in nfa:
|
||||
new_states.update(nfa[key])
|
||||
current_states = new_states
|
||||
|
||||
return any(state in accepting_states for state in current_states)
|
||||
|
||||
# Set of accepting states
|
||||
accepting_states = {2}
|
||||
|
||||
nfa_file = sys.argv[1]
|
||||
nfa = load_data(nfa_file)
|
||||
|
||||
for line in sys.stdin:
|
||||
input_str = line.strip()
|
||||
start_states = {0}
|
||||
if is_accepted(nfa, input_str, start_states, accepting_states):
|
||||
print("YES")
|
||||
else:
|
||||
print("NO")
|
7
TaskC01/test.arg
Normal file
7
TaskC01/test.arg
Normal file
@ -0,0 +1,7 @@
|
||||
0 0 a
|
||||
0 0 c
|
||||
0 0 b
|
||||
0 1 b
|
||||
1 2 a
|
||||
1 2 c
|
||||
1 2 b
|
BIN
TaskC01/test.exp
BIN
TaskC01/test.exp
Binary file not shown.
41
TaskC02/run.py
Normal file
41
TaskC02/run.py
Normal file
@ -0,0 +1,41 @@
|
||||
import sys
|
||||
|
||||
def load_data(file_path):
|
||||
nfa = {}
|
||||
with open(file_path, 'r') as file:
|
||||
for line in file:
|
||||
split_data = line.strip().split(' ')
|
||||
if len(split_data) == 3:
|
||||
state, next_state, symbol = split_data
|
||||
key = (int(state), symbol)
|
||||
if key in nfa:
|
||||
nfa[key].add(int(next_state))
|
||||
else:
|
||||
nfa[key] = {int(next_state)}
|
||||
|
||||
return nfa
|
||||
|
||||
def is_accepted(nfa, input, current_states, accepting_states):
|
||||
for symbol in input:
|
||||
new_states = set()
|
||||
for state in current_states:
|
||||
key = (state, symbol)
|
||||
if key in nfa:
|
||||
new_states.update(nfa[key])
|
||||
current_states = new_states
|
||||
|
||||
return any(state in accepting_states for state in current_states)
|
||||
|
||||
# Set of accepting states
|
||||
accepting_states = {2}
|
||||
|
||||
nfa_file = sys.argv[1]
|
||||
nfa = load_data(nfa_file)
|
||||
|
||||
for line in sys.stdin:
|
||||
input_str = line.strip()
|
||||
start_states = {0}
|
||||
if is_accepted(nfa, input_str, start_states, accepting_states):
|
||||
print("YES")
|
||||
else:
|
||||
print("NO")
|
5
TaskC02/test.arg
Normal file
5
TaskC02/test.arg
Normal file
@ -0,0 +1,5 @@
|
||||
0 0 a
|
||||
0 0 c
|
||||
0 0 b
|
||||
0 1 a
|
||||
1 2 b
|
BIN
TaskC02/test.exp
BIN
TaskC02/test.exp
Binary file not shown.
41
TaskC03/run.py
Normal file
41
TaskC03/run.py
Normal file
@ -0,0 +1,41 @@
|
||||
import sys
|
||||
|
||||
def load_data(file_path):
|
||||
nfa = {}
|
||||
with open(file_path, 'r') as file:
|
||||
for line in file:
|
||||
split_data = line.strip().split(' ')
|
||||
if len(split_data) == 3:
|
||||
state, next_state, symbol = split_data
|
||||
key = (int(state), symbol)
|
||||
if key in nfa:
|
||||
nfa[key].add(int(next_state))
|
||||
else:
|
||||
nfa[key] = {int(next_state)}
|
||||
|
||||
return nfa
|
||||
|
||||
def is_accepted(nfa, input, current_states, accepting_states):
|
||||
for symbol in input:
|
||||
new_states = set()
|
||||
for state in current_states:
|
||||
key = (state, symbol)
|
||||
if key in nfa:
|
||||
new_states.update(nfa[key])
|
||||
current_states = new_states
|
||||
|
||||
return any(state in accepting_states for state in current_states)
|
||||
|
||||
# Set of accepting states
|
||||
accepting_states = {3}
|
||||
|
||||
nfa_file = sys.argv[1]
|
||||
nfa = load_data(nfa_file)
|
||||
|
||||
for line in sys.stdin:
|
||||
input_str = line.strip()
|
||||
start_states = {0}
|
||||
if is_accepted(nfa, input_str, start_states, accepting_states):
|
||||
print("YES")
|
||||
else:
|
||||
print("NO")
|
9
TaskC03/test.arg
Normal file
9
TaskC03/test.arg
Normal file
@ -0,0 +1,9 @@
|
||||
0 0 a
|
||||
0 0 c
|
||||
0 0 b
|
||||
0 1 a
|
||||
1 2 b
|
||||
2 3 c
|
||||
3 3 a
|
||||
3 3 b
|
||||
3 3 c
|
BIN
TaskC03/test.exp
BIN
TaskC03/test.exp
Binary file not shown.
48
TaskC04/run.py
Normal file
48
TaskC04/run.py
Normal file
@ -0,0 +1,48 @@
|
||||
import sys
|
||||
|
||||
def load_data(file_path):
|
||||
dfa = {}
|
||||
with open(file_path, 'r') as file:
|
||||
for line in file:
|
||||
split_data = line.strip().split('\t')
|
||||
if len(split_data) == 3:
|
||||
state, next_state, symbol = split_data
|
||||
key = (int(state), symbol)
|
||||
dfa[key] = int(next_state)
|
||||
|
||||
return dfa
|
||||
|
||||
def is_accepted(dfa, input_str, current_state, accepting_states):
|
||||
for i, symbol in enumerate(input_str):
|
||||
key = (current_state, symbol)
|
||||
if key in dfa:
|
||||
current_state = dfa[key]
|
||||
elif (current_state, '<eps>') in dfa: # Check for <eps> transition
|
||||
current_state = dfa[(current_state, '<eps>')]
|
||||
else:
|
||||
# If there is no transition for the current symbol or <eps>, reject the string
|
||||
return False
|
||||
|
||||
# If it's the last symbol and the current state is not accepting, check for <eps> transition
|
||||
if i == len(input_str) - 1 and current_state not in accepting_states:
|
||||
if (current_state, '<eps>') in dfa:
|
||||
current_state = dfa[(current_state, '<eps>')]
|
||||
if current_state in accepting_states:
|
||||
return True
|
||||
|
||||
return current_state in accepting_states
|
||||
|
||||
|
||||
# Set of accepting states
|
||||
accepting_states = {2,5}
|
||||
|
||||
dfa_file = sys.argv[1]
|
||||
dfa = load_data(dfa_file)
|
||||
|
||||
for line in sys.stdin:
|
||||
input_str = line.strip()
|
||||
start_state = 0
|
||||
if is_accepted(dfa, input_str, start_state, accepting_states):
|
||||
print("YES", input_str)
|
||||
else:
|
||||
print("NO", input_str)
|
BIN
TaskC04/test.exp
Normal file
BIN
TaskC04/test.exp
Normal file
Binary file not shown.
67
TaskC05/run.py
Normal file
67
TaskC05/run.py
Normal file
@ -0,0 +1,67 @@
|
||||
import codecs
|
||||
import sys
|
||||
|
||||
def process_input(fsa, input_string):
|
||||
current_state = 0
|
||||
output = ""
|
||||
for symbol in input_string:
|
||||
if (current_state, symbol) in fsa:
|
||||
current_state = fsa[(current_state, symbol)]
|
||||
else:
|
||||
return f"{input_string};OOV"
|
||||
|
||||
if (current_state, ";") in fsa:
|
||||
current_state = fsa[(current_state, ";")]
|
||||
code_paths = check_transition(fsa, current_state, 24)
|
||||
|
||||
if code_paths is None:
|
||||
return f"{input_string};OOV"
|
||||
|
||||
for code in sorted(code_paths):
|
||||
code_str = "".join(code)
|
||||
output += f"{input_string};{code_str}\n"
|
||||
|
||||
return output
|
||||
|
||||
def check_transition(fsa, start_state, target_state):
|
||||
def find_transitions(state, target_state, current_path, all_paths):
|
||||
if state == target_state:
|
||||
all_paths.append(current_path.copy())
|
||||
return
|
||||
|
||||
for (s, symbol), next_state in fsa.items():
|
||||
if s == state:
|
||||
current_path.append(symbol)
|
||||
find_transitions(next_state, target_state, current_path, all_paths)
|
||||
current_path.pop()
|
||||
|
||||
current_state = start_state
|
||||
all_paths = []
|
||||
find_transitions(current_state, target_state, [], all_paths)
|
||||
|
||||
if not all_paths:
|
||||
return None
|
||||
|
||||
return all_paths
|
||||
|
||||
fsa_file = "./multi.arg" # FSA FILE
|
||||
fsa = {}
|
||||
|
||||
with open(fsa_file, 'r', encoding='utf-8') as f:
|
||||
for line in f:
|
||||
parts = line.strip().split('\t')
|
||||
if len(parts) == 3:
|
||||
state, next_state, symbol = parts
|
||||
fsa[(int(state), symbol)] = int(next_state)
|
||||
|
||||
input_file_path = "./multi.in" # INPUT STRING
|
||||
output_file_path = "test.exp" # OUTPUT FILE
|
||||
|
||||
with open(input_file_path, 'r', encoding='utf-8') as file, open(output_file_path, 'w', encoding='utf-8') as output_file:
|
||||
for line in file:
|
||||
input_string = line.strip()
|
||||
result = process_input(fsa, input_string)
|
||||
output_file.write(result)
|
||||
|
||||
# POCZĄTKOWO WSZYSTKIE ŚCIEŻKI DO PLIKÓW BYŁY PRZESYŁANE POPRZEZ TERMINAL ALE PRZEZ WZGLĄD NA POLSKIE ZNAKI MUSIAŁAM ZAMIESZCZAĆ ŚCIEŻKI DO PLIKÓW W KODZIE
|
||||
# NIESTETY NIE UDAŁO MI SIĘ ZNALEŹĆ W JAKI SPOSÓB PRZESYŁAĆ PLIKI BY UŻYWAĆ UTF-8
|
2
TaskC05/test.exp
Normal file
2
TaskC05/test.exp
Normal file
@ -0,0 +1,2 @@
|
||||
piła;N
|
||||
piła;V
|
32
TaskC06/run.py
Normal file
32
TaskC06/run.py
Normal file
@ -0,0 +1,32 @@
|
||||
import sys
|
||||
|
||||
fsa_file = "./small2.in" # FSA FILE
|
||||
fsa = {}
|
||||
|
||||
with open(fsa_file, 'r', encoding='utf-8') as f:
|
||||
for line in f:
|
||||
parts = line.strip().split('\t')
|
||||
if len(parts) == 3:
|
||||
state, next_state, symbol = parts
|
||||
state, next_state = int(state), int(next_state)
|
||||
if (state, '') not in fsa:
|
||||
fsa[(state, '')] = {}
|
||||
fsa[(state, '')][symbol] = next_state
|
||||
|
||||
output_file_path = "./test.txt" # OUTPUT FILE
|
||||
|
||||
def get_paths(current_state, current_path):
|
||||
if (current_state, '') not in fsa:
|
||||
return [current_path]
|
||||
|
||||
paths = []
|
||||
for symbol, next_state in fsa[(current_state, '')].items():
|
||||
paths += get_paths(next_state, current_path + symbol)
|
||||
|
||||
return paths
|
||||
|
||||
with open(output_file_path, 'w', encoding='utf-8') as output_file:
|
||||
start_state = min(state for state, _ in fsa)
|
||||
paths = get_paths(start_state, '')
|
||||
for path in paths:
|
||||
output_file.write(path + '\n')
|
4
TaskC06/test.txt
Normal file
4
TaskC06/test.txt
Normal file
@ -0,0 +1,4 @@
|
||||
biały
|
||||
dom
|
||||
piła
|
||||
stali
|
Loading…
Reference in New Issue
Block a user