tasks
This commit is contained in:
parent
5e8f56b5d7
commit
59b85bff18
@ -53,7 +53,7 @@ with open(sys.argv[1]) as table:
|
|||||||
else:
|
else:
|
||||||
AddFinalState(final_states, line)
|
AddFinalState(final_states, line)
|
||||||
|
|
||||||
with open(sys.argv[2], 'r') as input_data, open(sys.argv[3], 'w') as output_data:
|
with open(sys.argv[2], 'r', encoding="utf-8") as input_data, open(sys.argv[3], 'w', encoding="utf-8") as output_data:
|
||||||
for line in input_data:
|
for line in input_data:
|
||||||
line = line.rstrip()
|
line = line.rstrip()
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
# automat akceptuje dwa napisy - "aaaa" i "a" powielone 4038 razy
|
|
||||||
0 1 a
|
0 1 a
|
||||||
1 2 a
|
1 2 a
|
||||||
2 3 a
|
2 3 a
|
||||||
|
75
TaskC04/run.py
Normal file
75
TaskC04/run.py
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
def ReadDescription(file_path):
|
||||||
|
nfa = {}
|
||||||
|
accepting_states = set()
|
||||||
|
|
||||||
|
with open(file_path, 'r', encoding="utf-8") as description:
|
||||||
|
for line in description:
|
||||||
|
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)}
|
||||||
|
|
||||||
|
elif len(split_data) == 2:
|
||||||
|
state, next_state = split_data
|
||||||
|
key = (int(state), '<eps>')
|
||||||
|
if key in nfa:
|
||||||
|
nfa[key].add(int(next_state))
|
||||||
|
else:
|
||||||
|
nfa[key] = {int(next_state)}
|
||||||
|
|
||||||
|
elif len(split_data) == 1:
|
||||||
|
accepting_states.add(int(split_data[0]))
|
||||||
|
|
||||||
|
return nfa, accepting_states
|
||||||
|
|
||||||
|
def Epsilon(nfa, states):
|
||||||
|
epsilon = set(states)
|
||||||
|
stack = list(states)
|
||||||
|
|
||||||
|
while stack:
|
||||||
|
currentState = stack.pop()
|
||||||
|
epsilon_key = (currentState, '<eps>')
|
||||||
|
if epsilon_key in nfa:
|
||||||
|
epsTransitions = nfa[epsilon_key]
|
||||||
|
for nextState in epsTransitions:
|
||||||
|
if nextState not in epsilon:
|
||||||
|
epsilon.add(nextState)
|
||||||
|
stack.append(nextState)
|
||||||
|
|
||||||
|
return epsilon
|
||||||
|
|
||||||
|
def IsAccepted(nfa, inputString, current_states, accepting_states):
|
||||||
|
for symbol in inputString:
|
||||||
|
new_states = set()
|
||||||
|
|
||||||
|
for state in current_states:
|
||||||
|
key = (state, symbol)
|
||||||
|
if key in nfa:
|
||||||
|
new_states.update(nfa[key])
|
||||||
|
|
||||||
|
current_states = Epsilon(nfa, new_states)
|
||||||
|
|
||||||
|
return any(state in accepting_states for state in current_states)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
nfa_file = sys.argv[1]
|
||||||
|
nfa, acceptingStates = ReadDescription(nfa_file)
|
||||||
|
|
||||||
|
for line in sys.stdin:
|
||||||
|
inputString = line.strip()
|
||||||
|
if inputString =='\n':
|
||||||
|
break
|
||||||
|
start_states = Epsilon(nfa, {0})
|
||||||
|
|
||||||
|
if IsAccepted(nfa, inputString, start_states, acceptingStates):
|
||||||
|
print("TRUE", inputString)
|
||||||
|
else:
|
||||||
|
print("FALSE", inputString)
|
@ -1,4 +1,3 @@
|
|||||||
# prosty automat akceptujący tylko napis "abc"
|
|
||||||
0 1 a
|
0 1 a
|
||||||
1 2 b
|
1 2 b
|
||||||
2 3 c
|
2 3 c
|
||||||
|
75
TaskC05/run.py
Normal file
75
TaskC05/run.py
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
sys.setrecursionlimit(4500)
|
||||||
|
|
||||||
|
def AddTransition(transitions, state_from, state_to, symbol):
|
||||||
|
if state_from in transitions:
|
||||||
|
if symbol not in transitions[state_from]:
|
||||||
|
transitions[state_from][symbol] = {state_to}
|
||||||
|
else:
|
||||||
|
transitions[state_from][symbol] |= {state_to}
|
||||||
|
else:
|
||||||
|
transitions[state_from] = {symbol: {state_to}}
|
||||||
|
|
||||||
|
def AddFinalState(final_states, state):
|
||||||
|
final_states.add(state)
|
||||||
|
|
||||||
|
def GetFinalStates(transitions, final_states, string, current_state):
|
||||||
|
if not string:
|
||||||
|
return current_state if current_state in final_states else -1
|
||||||
|
|
||||||
|
symbol, rest = string[0], string[1:]
|
||||||
|
|
||||||
|
if current_state in transitions and symbol in transitions[current_state]:
|
||||||
|
for state in transitions[current_state][symbol]:
|
||||||
|
result = GetFinalStates(transitions, final_states, rest, state)
|
||||||
|
if result != -1:
|
||||||
|
return result
|
||||||
|
return -1
|
||||||
|
|
||||||
|
def IsAccepted(transitions, final_states, string, initial_state):
|
||||||
|
final_state = GetFinalStates(transitions, final_states, string, initial_state)
|
||||||
|
return final_state in final_states
|
||||||
|
|
||||||
|
|
||||||
|
def ChangeInput(transitions, final_states, string, initial_state):
|
||||||
|
possible_ending_parts = [';N', ';V', ';ADJ']
|
||||||
|
valid_results = []
|
||||||
|
|
||||||
|
for part in possible_ending_parts:
|
||||||
|
copy_string = string + part
|
||||||
|
if IsAccepted(transitions, final_states, copy_string, initial_state):
|
||||||
|
valid_results.append(copy_string)
|
||||||
|
|
||||||
|
if not valid_results:
|
||||||
|
valid_results.append(string + ';OOV')
|
||||||
|
|
||||||
|
return valid_results
|
||||||
|
|
||||||
|
|
||||||
|
transitions = {}
|
||||||
|
final_states = set()
|
||||||
|
|
||||||
|
fsaDescr = sys.argv[1] # 'elem.arg'
|
||||||
|
inputFile = sys.argv[2] # 'elem.in'
|
||||||
|
outputFile = sys.argv[3] # 'elem.out'
|
||||||
|
|
||||||
|
with open(fsaDescr, 'r', encoding="utf-8") as description:
|
||||||
|
for line in description:
|
||||||
|
line = line.rstrip()
|
||||||
|
|
||||||
|
if len(line.split('\t')) == 3:
|
||||||
|
a, b, c = line.split('\t')
|
||||||
|
AddTransition(transitions, a, b, c)
|
||||||
|
else:
|
||||||
|
AddFinalState(final_states, line)
|
||||||
|
|
||||||
|
with open(inputFile, 'r', encoding="utf-8") as inFile, open(outputFile, 'w', encoding="utf-8") as outFile:
|
||||||
|
for line in inFile:
|
||||||
|
line = line.rstrip()
|
||||||
|
results = ChangeInput(transitions, final_states, line, '0')
|
||||||
|
results.sort()
|
||||||
|
for result in results:
|
||||||
|
outFile.write(result + '\n')
|
||||||
|
# print(result)
|
||||||
|
|
47
TaskC06/run.py
Normal file
47
TaskC06/run.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def read_fsa_description(file_path):
|
||||||
|
transitions = {}
|
||||||
|
final_states = set()
|
||||||
|
|
||||||
|
with open(file_path, 'r', encoding='utf-8') as file:
|
||||||
|
for line in file:
|
||||||
|
line = line.rstrip()
|
||||||
|
if not line:
|
||||||
|
break
|
||||||
|
|
||||||
|
parts = line.split('\t')
|
||||||
|
if len(parts) == 3:
|
||||||
|
state_from, state_to, symbol = parts[0], parts[1], parts[2]
|
||||||
|
if state_from in transitions:
|
||||||
|
transitions[state_from].append((state_to, symbol))
|
||||||
|
else:
|
||||||
|
transitions[state_from] = [(state_to, symbol)]
|
||||||
|
|
||||||
|
if len(parts)==1:
|
||||||
|
line = line.rstrip()
|
||||||
|
final_states.add(line)
|
||||||
|
|
||||||
|
return transitions, final_states
|
||||||
|
|
||||||
|
def generate_paths(transitions, current_state, current_path, final_states):
|
||||||
|
paths = []
|
||||||
|
if current_state in final_states:
|
||||||
|
paths.append("".join(current_path))
|
||||||
|
|
||||||
|
if current_state in transitions:
|
||||||
|
for next_state, symbol in transitions[current_state]:
|
||||||
|
paths.extend(generate_paths(transitions, next_state, current_path + [symbol], final_states))
|
||||||
|
|
||||||
|
return paths
|
||||||
|
|
||||||
|
|
||||||
|
fsa_description_file = sys.argv[1]#'medium.in'
|
||||||
|
transitions, final_states = read_fsa_description(fsa_description_file)
|
||||||
|
paths = generate_paths(transitions, '0', [], final_states)
|
||||||
|
|
||||||
|
for path in sorted(paths):
|
||||||
|
print(path)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user