From 1649b19c4c4149773493bd297a5ec00eb5258cf0 Mon Sep 17 00:00:00 2001 From: s450026 Date: Sun, 20 Dec 2020 19:13:25 +0100 Subject: [PATCH] E05 --- TaskE05/E05.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ TaskE05/Makefile | 0 TaskE05/run | 2 ++ 3 files changed, 96 insertions(+) create mode 100644 TaskE05/E05.py create mode 100644 TaskE05/Makefile create mode 100755 TaskE05/run diff --git a/TaskE05/E05.py b/TaskE05/E05.py new file mode 100644 index 0000000..f88ea98 --- /dev/null +++ b/TaskE05/E05.py @@ -0,0 +1,94 @@ +import sys + + +class DFA: + current_state = None + + def __init__(self, states, alphabet, transition_function, start_state, accept_states): + self.states = states + self.alphabet = alphabet + self.transition_function = transition_function + self.start_state = start_state + self.accept_states = accept_states + self.current_state = start_state + return + + def transition_to_state_with_input(self, input_value): + if ((self.current_state, input_value) not in self.transition_function.keys()): + self.current_state = None + return + self.current_state = self.transition_function[(self.current_state, input_value)]; + return + + def in_accept_state(self): + return self.current_state in accept_states + + def go_to_initial_state(self): + self.current_state = self.start_state + return + + def run_with_input_list(self, input_list): + self.go_to_initial_state() + for inp in input_list: + self.transition_to_state_with_input(inp) + continue + return self.in_accept_state() + + def accept(self, data): + exit = {7: 'END'} + state = 0 + + for letter in data: + if (state, letter) in self.transition_function: + state = self.transition_function[state, letter] + if state in exit: + return True + else: + state = 0 + return False + + pass + + +states = [1, 2, 3, 4, 5, 6, 7] +alphabet = ['1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', '.', 'r'] +start_state = 0 +accept_states = [7] +tf = dict() +tf[(0, '1')] = 1; +tf[(1, '9')] = 2; + +tf[(2, '0')] = 3; +tf[(2, '1')] = 3; +tf[(2, '2')] = 3; +tf[(2, '3')] = 3; +tf[(2, '4')] = 3; +tf[(2, '5')] = 3; +tf[(2, '6')] = 3; +tf[(2, '7')] = 3; +tf[(2, '8')] = 3; +tf[(2, '9')] = 3; + +tf[(3, '0')] = 4; +tf[(3, '1')] = 4; +tf[(3, '2')] = 4; +tf[(3, '3')] = 4; +tf[(3, '4')] = 4; +tf[(3, '5')] = 4; +tf[(3, '6')] = 4; +tf[(3, '7')] = 4; +tf[(3, '8')] = 4; +tf[(3, '9')] = 4; + +tf[(4, ' ')] = 5; +tf[(5, 'r')] = 6; +tf[(6, '.')] = 7; + +d = DFA(states, alphabet, tf, start_state, accept_states) + +for line in sys.stdin: + inp_program = list(line.rstrip("\n")) + if d.accept(inp_program): + print("YES") + else: + print("NO") diff --git a/TaskE05/Makefile b/TaskE05/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/TaskE05/run b/TaskE05/run new file mode 100755 index 0000000..cf8fe86 --- /dev/null +++ b/TaskE05/run @@ -0,0 +1,2 @@ +#!/bin/bash +python3 TaskE05/E05.py "$@" \ No newline at end of file