-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm.py
30 lines (26 loc) · 1.11 KB
/
fsm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Automaton(object):
def __init__(self, states):
self.states = states
def read_commands(self, commands):
# Return True if we end in our accept state, False otherwise
current_state = self.states[0]
for command in commands:
if command == "1":
if current_state == self.states[0]:
new_state = self.states[1]
elif current_state == self.states[1]:
new_state = self.states[1]
else:
new_state = self.states[1]
else:
if current_state == self.states[0]:
new_state = self.states[0]
elif current_state == self.states[1]:
new_state = self.states[2]
else:
new_state = self.states[1]
current_state = new_state
return True if current_state == self.states[1] else False
my_automaton = Automaton(["q1", "q2", "q3"])
print(my_automaton.read_commands(["1", "0", "0", "1"]))
# Do anything necessary to set up your automaton's states, q1, q2, and q3.