Untitled
unknown
plain_text
8 months ago
1.1 kB
3
Indexable
class FA:
def __init__(self):
self.current_state="q0"
self.accept_state=["q2"]
def transition(self,input_char):
if self.current_state=="q0":
if input_char=="1":
self.current_state="q1"
else:
self.current_state="q0"
elif self.current_state=="q1":
if input_char=="1":
self.current_state="q2"
else:
self.current_state="q0"
elif self.current_state=="q2":
if input_char=="1":
self.current_state="q2"
else:
self.current_state="q0"
def is_accepted(self):
return self.current_state in self.accept_state
def reset(self):
self.current_state="q0"
def process_input_string(input_string,automaton):
for char in input_string:
automaton.transition(char)
return automaton.is_accepted()
input_string="111010111"
automaton=FA()
print(process_input_string(input_string,automaton))
Editor is loading...
Leave a Comment