DarylT01 commited on
Commit
7ef0785
·
1 Parent(s): 74e28e3

fix(trap): add trap state support

Browse files
Files changed (1) hide show
  1. main.py +18 -6
main.py CHANGED
@@ -15,12 +15,9 @@ class DFA:
15
  current_state = self.initial_state
16
 
17
  for char in input_string:
18
- if char not in self.alphabets:
19
- return False
20
-
21
- current_state = self.transitions.get(current_state, {}).get(char, None)
22
- if current_state is None:
23
- return False
24
 
25
  return current_state in self.final_states
26
 
@@ -136,4 +133,19 @@ def generate_dfa(input_strings: list[str]) -> DFA:
136
  # Add the final state to the set of final states
137
  final_states.add(current_state)
138
 
 
 
 
 
 
 
 
 
 
 
 
139
  return DFA(alphabets, states, transition_functions, 0, final_states)
 
 
 
 
 
15
  current_state = self.initial_state
16
 
17
  for char in input_string:
18
+ current_state = self.transitions.get(current_state, {}).get(
19
+ char, len(self.states) - 1
20
+ )
 
 
 
21
 
22
  return current_state in self.final_states
23
 
 
133
  # Add the final state to the set of final states
134
  final_states.add(current_state)
135
 
136
+ # Add the trap state to the set of states
137
+ states.add(len(states))
138
+
139
+ # Add the trap state to the transition functions
140
+ for state in states:
141
+ if state not in transition_functions:
142
+ transition_functions[state] = {}
143
+ for alphabet in alphabets:
144
+ if alphabet not in transition_functions[state]:
145
+ transition_functions[state][alphabet] = len(states) - 1
146
+
147
  return DFA(alphabets, states, transition_functions, 0, final_states)
148
+
149
+
150
+ dfa = generate_dfa(["and", "but", "or", "because"])
151
+ print(dfa.check("me and you."))