Compare commits
No commits in common. "6fbb0059728932cf30f6c398f497bb0cfca7eeb4" and "1820fc5e148842722614caaa2d97b986c1151719" have entirely different histories.
6fbb005972
...
1820fc5e14
@ -1,3 +1,4 @@
|
|||||||
|
# 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
|
||||||
|
@ -1,56 +0,0 @@
|
|||||||
import pandas as pd
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import random
|
|
||||||
|
|
||||||
class Automat:
|
|
||||||
def __init__(self, df: pd.DataFrame, end_states: list):
|
|
||||||
self._df = df
|
|
||||||
self._end_states = end_states
|
|
||||||
|
|
||||||
def _consume_character(self, state: int, char: str):
|
|
||||||
filter = (self._df[0] == state) & (self._df[2] == char)
|
|
||||||
matching_rows = self._df[filter]
|
|
||||||
if matching_rows.empty:
|
|
||||||
return None
|
|
||||||
end_states = matching_rows.iloc[:,1].tolist()
|
|
||||||
return random.choice(end_states)
|
|
||||||
|
|
||||||
def consume_word(self, word: str):
|
|
||||||
prev_state = 0
|
|
||||||
state = 0
|
|
||||||
for char in word:
|
|
||||||
prev_state = state
|
|
||||||
state = self._consume_character(state, char)
|
|
||||||
if state is None:
|
|
||||||
return prev_state in self._end_states
|
|
||||||
return state in self._end_states
|
|
||||||
|
|
||||||
def create_automat_from_file(file_path: str):
|
|
||||||
automat_df = pd.read_csv(file_path, sep='\t', header=None)
|
|
||||||
|
|
||||||
end_states_condition = (automat_df[1].isnull()) & (automat_df[2].isnull())
|
|
||||||
end_states_df = automat_df[end_states_condition]
|
|
||||||
end_states = end_states_df.iloc[:,0].tolist()
|
|
||||||
|
|
||||||
automat_df = automat_df[~end_states_condition]
|
|
||||||
automat_df[2] = automat_df[2].astype(str)
|
|
||||||
|
|
||||||
return Automat(automat_df, end_states)
|
|
||||||
|
|
||||||
automat_path = os.path.join(os.path.dirname(__file__), 'test1.arg')
|
|
||||||
automat = create_automat_from_file(automat_path)
|
|
||||||
|
|
||||||
test_path = os.path.join(os.path.dirname(__file__), 'test1.in')
|
|
||||||
|
|
||||||
|
|
||||||
results = []
|
|
||||||
with open(test_path, 'r') as f:
|
|
||||||
for line in f:
|
|
||||||
results.append('YES' if automat.consume_word(line.strip()) else 'NO')
|
|
||||||
|
|
||||||
out_path = os.path.join(os.path.dirname(__file__), 'test1.out')
|
|
||||||
|
|
||||||
with open(out_path, 'w') as f:
|
|
||||||
for result in results:
|
|
||||||
f.write(result + '\n')
|
|
@ -1,9 +0,0 @@
|
|||||||
NO
|
|
||||||
YES
|
|
||||||
NO
|
|
||||||
NO
|
|
||||||
NO
|
|
||||||
NO
|
|
||||||
NO
|
|
||||||
NO
|
|
||||||
NO
|
|
@ -1,48 +0,0 @@
|
|||||||
import pandas as pd
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import random
|
|
||||||
|
|
||||||
class Automat:
|
|
||||||
def __init__(self, df: pd.DataFrame, end_states: list):
|
|
||||||
self._df = df
|
|
||||||
self._end_states = end_states
|
|
||||||
|
|
||||||
def _consume_character(self, state: int, char: str):
|
|
||||||
filter = (self._df[0] == state) & (self._df[2] == char)
|
|
||||||
matching_rows = self._df[filter]
|
|
||||||
if matching_rows.empty:
|
|
||||||
return None
|
|
||||||
end_states = matching_rows.iloc[:,1].tolist()
|
|
||||||
return random.choice(end_states)
|
|
||||||
|
|
||||||
def consume_word(self, word: str):
|
|
||||||
prev_state = 0
|
|
||||||
state = 0
|
|
||||||
for char in word:
|
|
||||||
prev_state = state
|
|
||||||
state = self._consume_character(state, char)
|
|
||||||
if state is None:
|
|
||||||
return prev_state in self._end_states
|
|
||||||
return state in self._end_states
|
|
||||||
|
|
||||||
def create_automat_from_file(file_path: str):
|
|
||||||
automat_df = pd.read_csv(file_path, sep=' ', header=None)
|
|
||||||
end_states_condition = (automat_df[1].isnull()) & (automat_df[2].isnull())
|
|
||||||
end_states_df = automat_df[end_states_condition]
|
|
||||||
end_states = end_states_df.iloc[:,0].tolist()
|
|
||||||
|
|
||||||
automat_df = automat_df[~end_states_condition]
|
|
||||||
automat_df[2] = automat_df[2].astype(str)
|
|
||||||
|
|
||||||
return Automat(automat_df, end_states)
|
|
||||||
|
|
||||||
automat_path = os.path.join(os.path.dirname(__file__), 'test.arg')
|
|
||||||
automat = create_automat_from_file(automat_path)
|
|
||||||
|
|
||||||
test_path = os.path.join(os.path.dirname(__file__), 'test.in')
|
|
||||||
|
|
||||||
txt = input('Write valid input: ')
|
|
||||||
while txt != 'exit':
|
|
||||||
print('YES' if automat.consume_word(txt) else 'NO')
|
|
||||||
txt = input('Write valid input: ')
|
|
@ -1,5 +0,0 @@
|
|||||||
0 1 a
|
|
||||||
0 1 b
|
|
||||||
0 1 c
|
|
||||||
1 2 b
|
|
||||||
2
|
|
@ -1,6 +0,0 @@
|
|||||||
YES
|
|
||||||
YES
|
|
||||||
NO
|
|
||||||
NO
|
|
||||||
YES
|
|
||||||
NO
|
|
@ -1,46 +0,0 @@
|
|||||||
import pandas as pd
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import random
|
|
||||||
|
|
||||||
class Automat:
|
|
||||||
def __init__(self, df: pd.DataFrame, end_states: list):
|
|
||||||
self._df = df
|
|
||||||
self._end_states = end_states
|
|
||||||
|
|
||||||
def _consume_character(self, state: int, char: str):
|
|
||||||
filter = (self._df[0] == state) & (self._df[2] == char)
|
|
||||||
matching_rows = self._df[filter]
|
|
||||||
if matching_rows.empty:
|
|
||||||
return None
|
|
||||||
end_states = matching_rows.iloc[:,1].tolist()
|
|
||||||
return random.choice(end_states)
|
|
||||||
|
|
||||||
def consume_word(self, word: str):
|
|
||||||
prev_state = 0
|
|
||||||
state = 0
|
|
||||||
for char in word:
|
|
||||||
prev_state = state
|
|
||||||
state = self._consume_character(state, char)
|
|
||||||
if state is None:
|
|
||||||
return prev_state in self._end_states
|
|
||||||
return state in self._end_states
|
|
||||||
|
|
||||||
def create_automat_from_file(file_path: str):
|
|
||||||
automat_df = pd.read_csv(file_path, sep=' ', header=None)
|
|
||||||
end_states_condition = (automat_df[1].isnull()) & (automat_df[2].isnull())
|
|
||||||
end_states_df = automat_df[end_states_condition]
|
|
||||||
end_states = end_states_df.iloc[:,0].tolist()
|
|
||||||
|
|
||||||
automat_df = automat_df[~end_states_condition]
|
|
||||||
automat_df[2] = automat_df[2].astype(str)
|
|
||||||
|
|
||||||
return Automat(automat_df, end_states)
|
|
||||||
|
|
||||||
automat_path = os.path.join(os.path.dirname(__file__), 'test.arg')
|
|
||||||
automat = create_automat_from_file(automat_path)
|
|
||||||
|
|
||||||
txt = input('Write valid input: ')
|
|
||||||
while txt != 'exit':
|
|
||||||
print('YES' if automat.consume_word(txt) else 'NO')
|
|
||||||
txt = input('Write valid input: ')
|
|
@ -1,6 +0,0 @@
|
|||||||
0 0 a
|
|
||||||
0 0 b
|
|
||||||
0 0 c
|
|
||||||
0 1 a
|
|
||||||
1 2 b
|
|
||||||
2
|
|
@ -1,6 +0,0 @@
|
|||||||
NO
|
|
||||||
NO
|
|
||||||
NO
|
|
||||||
NO
|
|
||||||
YES
|
|
||||||
NO
|
|
@ -1,46 +0,0 @@
|
|||||||
import pandas as pd
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import random
|
|
||||||
|
|
||||||
class Automat:
|
|
||||||
def __init__(self, df: pd.DataFrame, end_states: list):
|
|
||||||
self._df = df
|
|
||||||
self._end_states = end_states
|
|
||||||
|
|
||||||
def _consume_character(self, state: int, char: str):
|
|
||||||
filter = (self._df[0] == state) & (self._df[2] == char)
|
|
||||||
matching_rows = self._df[filter]
|
|
||||||
if matching_rows.empty:
|
|
||||||
return None
|
|
||||||
end_states = matching_rows.iloc[:,1].tolist()
|
|
||||||
return random.choice(end_states)
|
|
||||||
|
|
||||||
def consume_word(self, word: str):
|
|
||||||
prev_state = 0
|
|
||||||
state = 0
|
|
||||||
for char in word:
|
|
||||||
prev_state = state
|
|
||||||
state = self._consume_character(state, char)
|
|
||||||
if state is None:
|
|
||||||
return prev_state in self._end_states
|
|
||||||
return state in self._end_states
|
|
||||||
|
|
||||||
def create_automat_from_file(file_path: str):
|
|
||||||
automat_df = pd.read_csv(file_path, sep=' ', header=None)
|
|
||||||
end_states_condition = (automat_df[1].isnull()) & (automat_df[2].isnull())
|
|
||||||
end_states_df = automat_df[end_states_condition]
|
|
||||||
end_states = end_states_df.iloc[:,0].tolist()
|
|
||||||
|
|
||||||
automat_df = automat_df[~end_states_condition]
|
|
||||||
automat_df[2] = automat_df[2].astype(str)
|
|
||||||
|
|
||||||
return Automat(automat_df, end_states)
|
|
||||||
|
|
||||||
automat_path = os.path.join(os.path.dirname(__file__), 'test.arg')
|
|
||||||
automat = create_automat_from_file(automat_path)
|
|
||||||
|
|
||||||
txt = input('Write valid input: ')
|
|
||||||
while txt != 'exit':
|
|
||||||
print('YES' if automat.consume_word(txt) else 'NO')
|
|
||||||
txt = input('Write valid input: ')
|
|
@ -1,13 +0,0 @@
|
|||||||
0 0 a
|
|
||||||
0 0 b
|
|
||||||
0 0 c
|
|
||||||
0 1 a
|
|
||||||
1 1 a
|
|
||||||
1 1 b
|
|
||||||
1 1 c
|
|
||||||
1 2 b
|
|
||||||
2 2 a
|
|
||||||
2 2 b
|
|
||||||
2 2 c
|
|
||||||
2 3 c
|
|
||||||
3
|
|
Loading…
Reference in New Issue
Block a user