task C00-D04 \ C05,C06

This commit is contained in:
Wojtek 2023-12-09 21:52:30 +01:00
parent 45dfcfe4fd
commit 3908ac94e6
22 changed files with 443 additions and 0 deletions

7
TaskC00/long.out Normal file
View File

@ -0,0 +1,7 @@
NO
NO
YES
YES
NO
NO
NO

52
TaskC00/run.py Normal file
View File

@ -0,0 +1,52 @@
import sys
def read_aut_nfa(nfsa_path):
with open(nfsa_path, "r", encoding="utf8") as file:
transisions = {}
accepting_states = set()
for line in file:
if line.startswith("#"):
continue
parts = line.strip().split()
if len(parts) == 1:
accepting_states.add(int(parts[0]))
else:
state_from = int(parts[0])
state_to = int(parts[1])
symbol = ' ' if len(parts) == 2 else parts[2]
if(state_from, symbol) not in transisions:
transisions[(state_from, symbol)] = []
transisions[(state_from, symbol)].append(state_to)
#print(transisions)
return transisions, accepting_states
def is_accepted_byNFA(transisions, accepting_states, line):
current_states = set([0])
for symbol in line.strip():
next_states = set()
for state in current_states:
if (state, symbol) in transisions:
next_states.update(transisions[(state,symbol)])
current_states = next_states
if len(current_states) == 0:
return "NO"
if any(state in accepting_states for state in current_states):
return "YES"
else:
return "NO"
def process_input(transisions, accepting_states, input_file, output_file):
with open(input_file, "r") as input, open(output_file, "w") as output:
for line in input:
result = is_accepted_byNFA(transisions, accepting_states, line)
output.write(result + '\n')
nfa_file, input_file, output_file = sys.argv[1], sys.argv[2], sys.argv[3]
transisions, accepting_states = read_aut_nfa(nfa_file)
process_input(transisions, accepting_states, input_file, output_file)

9
TaskC00/test1.out Normal file
View File

@ -0,0 +1,9 @@
NO
YES
NO
NO
NO
NO
NO
NO
NO

8
TaskC00/testnfa.out Normal file
View File

@ -0,0 +1,8 @@
YES
YES
NO
NO
NO
YES
NO
YES

50
TaskC01/run.py Normal file
View File

@ -0,0 +1,50 @@
import sys
def read_aut_nfa(nfsa_path):
with open(nfsa_path, "r", encoding="utf8") as file:
transisions = {}
accepting_states = set()
for line in file:
parts = line.strip().split()
if len(parts) == 1:
accepting_states.add(int(parts[0]))
else:
state_from = int(parts[0])
state_to = int(parts[1])
symbol = ' ' if len(parts) == 2 else parts[2]
if(state_from, symbol) not in transisions:
transisions[(state_from, symbol)] = []
transisions[(state_from, symbol)].append(state_to)
#print(transisions)
return transisions, accepting_states
def is_accepted_byNFA(transisions, accepting_states, line):
current_states = set([0])
for symbol in line.strip():
next_states = set()
for state in current_states:
if (state, symbol) in transisions:
next_states.update(transisions[(state,symbol)])
current_states = next_states
if len(current_states) == 0:
return "NO"
if any(state in accepting_states for state in current_states):
return "YES"
else:
return "NO"
def process_input(transisions, accepting_states, input_file, output_file):
with open(input_file, "r") as input, open(output_file, "w") as output:
for line in input:
result = is_accepted_byNFA(transisions, accepting_states, line)
output.write(result + '\n')
nfa_file, input_file, output_file = sys.argv[1], sys.argv[2], sys.argv[3]
transisions, accepting_states = read_aut_nfa(nfa_file)
process_input(transisions, accepting_states, input_file, output_file)

8
TaskC01/test.arg Normal file
View File

@ -0,0 +1,8 @@
0 0 a
0 0 b
0 0 c
0 1 b
1 2 a
1 2 b
1 2 c
2

6
TaskC01/test.out Normal file
View File

@ -0,0 +1,6 @@
YES
YES
NO
NO
NO
YES

50
TaskC02/run.py Normal file
View File

@ -0,0 +1,50 @@
import sys
def read_aut_nfa(nfsa_path):
with open(nfsa_path, "r", encoding="utf8") as file:
transisions = {}
accepting_states = set()
for line in file:
parts = line.strip().split()
if len(parts) == 1:
accepting_states.add(int(parts[0]))
else:
state_from = int(parts[0])
state_to = int(parts[1])
symbol = ' ' if len(parts) == 2 else parts[2]
if(state_from, symbol) not in transisions:
transisions[(state_from, symbol)] = []
transisions[(state_from, symbol)].append(state_to)
#print(transisions)
return transisions, accepting_states
def is_accepted_byNFA(transisions, accepting_states, line):
current_states = set([0])
for symbol in line.strip():
next_states = set()
for state in current_states:
if (state, symbol) in transisions:
next_states.update(transisions[(state,symbol)])
current_states = next_states
if len(current_states) == 0:
return "NO"
if any(state in accepting_states for state in current_states):
return "YES"
else:
return "NO"
def process_input(transisions, accepting_states, input_file, output_file):
with open(input_file, "r") as input, open(output_file, "w") as output:
for line in input:
result = is_accepted_byNFA(transisions, accepting_states, line)
output.write(result + '\n')
nfa_file, input_file, output_file = sys.argv[1], sys.argv[2], sys.argv[3]
transisions, accepting_states = read_aut_nfa(nfa_file)
process_input(transisions, accepting_states, input_file, output_file)

6
TaskC02/test.arg Normal file
View File

@ -0,0 +1,6 @@
0 0 a
0 0 b
0 0 c
0 1 a
1 2 b
2

6
TaskC02/test.out Normal file
View File

@ -0,0 +1,6 @@
YES
NO
YES
NO
YES
NO

50
TaskC03/run.py Normal file
View File

@ -0,0 +1,50 @@
import sys
def read_aut_nfa(nfsa_path):
with open(nfsa_path, "r", encoding="utf8") as file:
transisions = {}
accepting_states = set()
for line in file:
parts = line.strip().split()
if len(parts) == 1:
accepting_states.add(int(parts[0]))
else:
state_from = int(parts[0])
state_to = int(parts[1])
symbol = ' ' if len(parts) == 2 else parts[2]
if(state_from, symbol) not in transisions:
transisions[(state_from, symbol)] = []
transisions[(state_from, symbol)].append(state_to)
#print(transisions)
return transisions, accepting_states
def is_accepted_byNFA(transisions, accepting_states, line):
current_states = set([0])
for symbol in line.strip():
next_states = set()
for state in current_states:
if (state, symbol) in transisions:
next_states.update(transisions[(state,symbol)])
current_states = next_states
if len(current_states) == 0:
return "NO"
if any(state in accepting_states for state in current_states):
return "YES"
else:
return "NO"
def process_input(transisions, accepting_states, input_file, output_file):
with open(input_file, "r") as input, open(output_file, "w") as output:
for line in input:
result = is_accepted_byNFA(transisions, accepting_states, line)
output.write(result + '\n')
nfa_file, input_file, output_file = sys.argv[1], sys.argv[2], sys.argv[3]
transisions, accepting_states = read_aut_nfa(nfa_file)
process_input(transisions, accepting_states, input_file, output_file)

10
TaskC03/test.arg Normal file
View File

@ -0,0 +1,10 @@
0 0 a
0 0 b
0 0 c
0 1 a
1 2 b
2 3 c
3 3 a
3 3 b
3 3 c
3

7
TaskC03/test.out Normal file
View File

@ -0,0 +1,7 @@
YES
YES
YES
N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aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
FALSE xyz
FALSE aba
FALSE a

78
TaskC04/run.py Normal file
View File

@ -0,0 +1,78 @@
import sys
def read_aut_nfa(nfsa_path):
with open(nfsa_path, "r", encoding="utf8") as file:
transisions = {}
accepting_states = set()
for line in file:
if line.startswith("#"):
continue
parts = line.strip().split()
if len(parts) == 1:
accepting_states.add(int(parts[0]))
else:
state_from = int(parts[0])
state_to = int(parts[1])
#symbol = ' ' if len(parts) == 2 else parts[2]
symbol = parts[2] if len(parts) == 3 else "<eps>"
if(state_from, symbol) not in transisions:
transisions[(state_from, symbol)] = []
transisions[(state_from, symbol)].append(state_to)
#print(transisions)
return transisions, accepting_states
def is_accepted_byNFA(transisions, accepting_states, line):
current_states = set([0])
for symbol in line.strip():
#print(f"Obecny symbol: {symbol}")
next_states = set()
for state in current_states:
if (state, symbol) in transisions:
next_states.update(transisions[(state,symbol)])
#print(next_states)
#print(f"Przejście przez '{symbol}' ze stanu {state}: {transisions[(state, symbol)]}")
if len(next_states) == 0:
for state in current_states:
if (state, "<eps>") in transisions:
next_states.update(transisions[(state, "<eps>")])
#print(next_states)
#print(f"Epsilonowe przejście ze stanu {state}: {transisions[(state, '<eps>')]}")
if len(next_states) == 0:
#print("Brak dostępnych przejść")
return "FALSE"
current_states = next_states
#print(f"Obecny stan po przejsciu petli ------------------------------------------------ '{symbol}': {current_states}")
#print(current_states)
#kod powyżej nie uwzględni epsilonowych przejść na końcu słowa. Tutaj to uwzględniam
flaga = True
while flaga:
flaga = False
next_states = set()
for state in current_states:
if(state, "<eps>") in transisions:
next_states.update(transisions[(state, "<eps>")])
flaga = True
current_states = next_states
if any(state in accepting_states for state in current_states):
return "TRUE"
else:
return "FALSE"
def process_input(transisions, accepting_states, input_file, output_file):
with open(input_file, "r") as input, open(output_file, "w") as output:
for line in input:
result = is_accepted_byNFA(transisions, accepting_states, line)
output.write(result + ' ' + line)
nfa_file, input_file, output_file = sys.argv[1], sys.argv[2], sys.argv[3]
transisions, accepting_states = read_aut_nfa(nfa_file)
process_input(transisions, accepting_states, input_file, output_file)

8
TaskC04/simple1.out Normal file
View File

@ -0,0 +1,8 @@
FALSE a
FALSE ab
TRUE abc
FALSE abcd
FALSE aaaaab
TRUE abc
FALSE xyz
FALSE 0

9
TaskC04/simple2.out Normal file
View File

@ -0,0 +1,9 @@
TRUE kot
TRUE ac
TRUE abc
TRUE abbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbc
FALSE abcd
FALSE abbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccc
FALSE kotek
FALSE kotabc
TRUE kot

12
TaskD01/run.py Normal file
View File

@ -0,0 +1,12 @@
import re
def hamlet_reg(file_path):
licznik = 0
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
licznik += 1
if (re.search("'.*Hamlet.*", line)):
print(f"{licznik}: {line.strip()}")
file_path = 'TaskD01\simple.in'
hamlet_reg(file_path)

13
TaskD02/run.py Normal file
View File

@ -0,0 +1,13 @@
import re
def pies_reg(file_path):
licznik = 0
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
licznik += 1
if (re.search("(^|\s)pies($|\s)", line, re.IGNORECASE)):
print(f"{licznik}: {line.strip()}")
file_path = 'TaskD02\simple.in'
pies_reg(file_path)

18
TaskD03/run.py Normal file
View File

@ -0,0 +1,18 @@
import re
#Write a program to find lines containing date from 1900 to 1999 in format '19XX r.' no matter what on the left or right of the expression.
#Note that part ' r.' is obligatory.
#Do use regular expressions.
def data_reg(file_path):
licznik = 0
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
licznik += 1
if (re.search(".*19\d{2} r\..*", line)):
print(f"{licznik}: {line.strip()}")
file_path = 'TaskD03\simple.in'
data_reg(file_path)

19
TaskD04/run.py Normal file
View File

@ -0,0 +1,19 @@
import re
#Write a program to find all maximum substrings of digits.
#Return only these substrings separated by spaces in their order.
#Do use regular expressions.
def liczby_reg(file_path):
licznik = 0
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
licznik += 1
number = (re.findall(r'\d+', line))
if number:
print(f"{licznik}: {' '.join(number)}")
file_path = 'TaskD04\simple.in'
liczby_reg(file_path)