E00 working
This commit is contained in:
parent
a3ff17b549
commit
e050f7f9ef
0
TaskE00/Makefile
Normal file
0
TaskE00/Makefile
Normal file
2
TaskE00/run
Normal file
2
TaskE00/run
Normal file
@ -0,0 +1,2 @@
|
||||
#!/bin/bash
|
||||
python ./run.py "$@"
|
71
TaskE00/run.py
Normal file
71
TaskE00/run.py
Normal file
@ -0,0 +1,71 @@
|
||||
import sys
|
||||
import os
|
||||
|
||||
filename = sys.argv[1]
|
||||
path = os.path.join(os.getcwd(), filename)
|
||||
# path = os.path.join(os.getcwd(), 'test1.arg')
|
||||
file = open(path, 'r').readlines()
|
||||
|
||||
|
||||
class FSA():
|
||||
def __init__(self):
|
||||
self.initial_state = '0'
|
||||
self.current_letter = None
|
||||
self.current_state = self.initial_state
|
||||
self.alphabet = set()
|
||||
self.transition_function = []
|
||||
self.end_state = None
|
||||
|
||||
def add_letter_to_alphabet(self, letter):
|
||||
self.alphabet.add(letter)
|
||||
|
||||
def add_transition_to_transition_function(self, input_state, output_state, letter):
|
||||
obj = {'in': input_state, 'letter': letter, 'out': output_state}
|
||||
self.transition_function.append(obj)
|
||||
# add letter to alphabet
|
||||
|
||||
def specify_end_state(self, end_state):
|
||||
self.end_state = end_state
|
||||
|
||||
def check_text(self, text):
|
||||
output_text = ""
|
||||
|
||||
for letter in text:
|
||||
if letter not in self.alphabet:
|
||||
return 'NO'
|
||||
|
||||
found_function = None
|
||||
|
||||
for func in self.transition_function:
|
||||
if func['in'] == self.current_state and func['letter'] == letter:
|
||||
found_function = func
|
||||
|
||||
if not found_function:
|
||||
return 'NO'
|
||||
self.current_state = found_function['out']
|
||||
output_text += found_function['letter']
|
||||
|
||||
# print('Input text:{} output text:{} current state:{}'.format(text, output_text, self.current_state))
|
||||
if output_text == text and self.current_state == self.end_state:
|
||||
return 'YES'
|
||||
else:
|
||||
return 'NO'
|
||||
|
||||
|
||||
FSA = FSA()
|
||||
|
||||
for line in file:
|
||||
line = line.rstrip('\n').split(' ')
|
||||
if len(line) > 1:
|
||||
FSA.add_transition_to_transition_function(line[0], line[1], line[2])
|
||||
FSA.add_letter_to_alphabet(line[2])
|
||||
else:
|
||||
FSA.specify_end_state(line[0])
|
||||
|
||||
|
||||
lines = ['xxyz', 'xyz']
|
||||
|
||||
for line in sys.stdin:
|
||||
line = line.rstrip('\n')
|
||||
FSA.current_state = FSA.initial_state
|
||||
print(FSA.check_text(line))
|
Loading…
Reference in New Issue
Block a user