From bfafa6fa3d698a5242c56a91ea499d6b7b9a39ad Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 20 Dec 2020 11:04:30 +0100 Subject: [PATCH] E08 done2 --- TaskE08/Makefile | 0 TaskE08/run | 2 ++ TaskE08/run.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 TaskE08/Makefile create mode 100644 TaskE08/run create mode 100644 TaskE08/run.py diff --git a/TaskE08/Makefile b/TaskE08/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/TaskE08/run b/TaskE08/run new file mode 100644 index 0000000..7174f9b --- /dev/null +++ b/TaskE08/run @@ -0,0 +1,2 @@ +#!/bin/bash +python TaskE08/run.py "$@" \ No newline at end of file diff --git a/TaskE08/run.py b/TaskE08/run.py new file mode 100644 index 0000000..584ab73 --- /dev/null +++ b/TaskE08/run.py @@ -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.acceptance_state = '0' + self.alphabet = set() + self.transition_function = [{'in': '0', 'letter': '0', 'out': '1'}, + {'in': '1', 'letter': '1', 'out': '1'}, + {'in': '1', 'letter': '0', 'out': '0'}, + {'in': '0', 'letter': '1', '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): + if text == '': + return 'YES' + + 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: + 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 == text and self.current_state == self.acceptance_state: + return 'YES' + + return 'NO' + + +FSA = FSA() +FSA.alphabet.add('0') +FSA.alphabet.add('1') +# lines = ['10','', '0101'] + +for line in sys.stdin: + line = line.rstrip('\n') + FSA.current_state = FSA.initial_state + print(FSA.check_text(line))