69 lines
2.6 KiB
Python
69 lines
2.6 KiB
Python
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 = ['1','2','3','4','5','6','7','8','9','0', '.',' ', 'x', 'r']
|
|
self.transition_function = [{'in': '0', 'letter': '1', 'out': '1'},
|
|
{'in': '1', 'letter': '9', 'out': '2'},
|
|
{'in': '2', 'letter': ['1','2','3','4','5','6','7','8','9','0'], 'out': '3'},
|
|
{'in': '3', 'letter': ['1','2','3','4','5','6','7','8','9','0'], 'out': '4'},
|
|
{'in': '4', 'letter': ' ', 'out': '5'},
|
|
{'in': '5', 'letter': 'r', 'out': '6'},
|
|
{'in': '6', 'letter': '.', 'out': '7'},
|
|
{'in': ['0', '1', '2', '3', '4', '5', '6'], 'letter': 'x', 'out': '0'}]
|
|
|
|
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):
|
|
for letter in text:
|
|
if letter not in self.alphabet:
|
|
text = text.replace(letter, 'x')
|
|
|
|
for letter in text:
|
|
found_function = None
|
|
|
|
for func in self.transition_function:
|
|
if self.current_state in func['in'] and letter in func['letter']:
|
|
found_function = func
|
|
|
|
if not found_function:
|
|
self.current_state = '0'
|
|
else:
|
|
self.current_state = found_function['out']
|
|
|
|
if self.current_state == self.acceptance_state:
|
|
return 'YES'
|
|
|
|
return 'NO'
|
|
|
|
FSA = FSA()
|
|
|
|
lines = ['Firma powstała w 1992 r., z połączenia Authorware, Inc. (twórców pakietu Authorware) i MacroMind-Paracomp (producenta Macromind Director). W 1999 r. Macromedia zakupiła firmę Allaire i jej biznesowe oprogramowanie sieciowe, w tym edytor HomeSite. W 2003 zakupiono eHelp Corporation, produkującą oprogramowanie do tworzenia systemów pomocy, jak RoboHelp, RoboDemo (obecnie Captivate) i RoboInfo.']
|
|
for line in sys.stdin:
|
|
line = line.rstrip('\n')
|
|
FSA.current_state = FSA.initial_state
|
|
print(FSA.check_text(line))
|
|
|
|
|