E05
This commit is contained in:
parent
4a5223c08b
commit
1649b19c4c
94
TaskE05/E05.py
Normal file
94
TaskE05/E05.py
Normal file
@ -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")
|
0
TaskE05/Makefile
Normal file
0
TaskE05/Makefile
Normal file
2
TaskE05/run
Executable file
2
TaskE05/run
Executable file
@ -0,0 +1,2 @@
|
||||
#!/bin/bash
|
||||
python3 TaskE05/E05.py "$@"
|
Loading…
Reference in New Issue
Block a user