E04 ready

This commit is contained in:
Adam 2020-12-19 22:57:41 +01:00
parent e050f7f9ef
commit 4eb9c43592
4 changed files with 83 additions and 4 deletions

View File

@ -1,9 +1,9 @@
import sys
import os
filename = sys.argv[1]
path = os.path.join(os.getcwd(), filename)
# path = os.path.join(os.getcwd(), 'test1.arg')
# filename = sys.argv[1]
# path = os.path.join(os.getcwd(), filename)
path = os.path.join(os.getcwd(), 'test1.arg')
file = open(path, 'r').readlines()
@ -65,7 +65,7 @@ for line in file:
lines = ['xxyz', 'xyz']
for line in sys.stdin:
for line in lines:
line = line.rstrip('\n')
FSA.current_state = FSA.initial_state
print(FSA.check_text(line))

0
TaskE04/Makefile Normal file
View File

2
TaskE04/run Normal file
View File

@ -0,0 +1,2 @@
#!/bin/bash
python ./run.py "$@"

77
TaskE04/run.py Normal file
View File

@ -0,0 +1,77 @@
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.acceptance_state = '7'
self.alphabet = set()
self.transition_function = [{'in': '0', 'letter': 'M', 'out': '1'},
{'in': '1', 'letter': 'a', 'out': '2'},
{'in': '2', 'letter': 'c', 'out': '3'},
{'in': '3', 'letter': 'b', 'out': '4'},
{'in': '4', 'letter': 'e', 'out': '5'},
{'in': '5', 'letter': 't', 'out': '6'},
{'in': '6', 'letter': 'h', 'out': '7'},
{'in': ['1', '2', '3', '4', '5', '6'], 'letter': 'x', 'out': '8'}]
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:
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:
self.current_state = '0'
else:
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 == 'Macbeth' and self.current_state == self.acceptance_state:
return 'YES'
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 = ['Scene V. Inverness. A Room in Macbeths Castle']
for line in sys.stdin:
line = line.rstrip('\n')
FSA.current_state = FSA.initial_state
print(FSA.check_text(line))