F00
This commit is contained in:
parent
140aea0f03
commit
8a33bb69fa
0
TaskF00/Makefile
Normal file
0
TaskF00/Makefile
Normal file
2
TaskF00/run
Normal file
2
TaskF00/run
Normal file
@ -0,0 +1,2 @@
|
||||
#!/bin/bash
|
||||
python TaskF00/run.py "$@"
|
77
TaskF00/run.py
Normal file
77
TaskF00/run.py
Normal file
@ -0,0 +1,77 @@
|
||||
import sys
|
||||
import os
|
||||
|
||||
#./run test1.arg < test1.in > test1.out
|
||||
|
||||
filename = sys.argv[1]
|
||||
# filename = 'long.arg'
|
||||
path = os.path.join(os.getcwd(), filename)
|
||||
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 = []
|
||||
|
||||
def add_letter_to_alphabet(self, letter):
|
||||
self.alphabet.add(letter)
|
||||
|
||||
def add_transition_to_transition_function(self, input_state, output_state, letter):
|
||||
if len(self.transition_function) > 0:
|
||||
for x, function in enumerate(self.transition_function):
|
||||
if function['in'] == input_state and function['out'] == output_state:
|
||||
self.transition_function[x]['letter'].append(letter)
|
||||
return
|
||||
obj = {'in': input_state, 'letter': [letter], 'out': output_state}
|
||||
self.transition_function.append(obj)
|
||||
else:
|
||||
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.append(end_state)
|
||||
|
||||
def check_text(self, 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 letter in func['letter']:
|
||||
found_function = func
|
||||
if not found_function:
|
||||
return 'NO'
|
||||
self.current_state = found_function['out']
|
||||
|
||||
# print('Input text:{} output text:{} current state:{}'.format(text, output_text, self.current_state))
|
||||
if self.current_state in self.end_state:
|
||||
return 'YES'
|
||||
else:
|
||||
return 'NO'
|
||||
|
||||
|
||||
FSA = FSA()
|
||||
|
||||
for line in file:
|
||||
if '#' in line:
|
||||
continue
|
||||
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 = ['']
|
||||
|
||||
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