42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
from deterministic_automaton import DeterministicAutomaton
|
|
|
|
|
|
def three_automaton():
|
|
a = DeterministicAutomaton()
|
|
|
|
state_i = a.add_state()
|
|
state_only_0 = a.add_state()
|
|
state_leading_zeroes = a.add_state()
|
|
state_0 = a.add_state()
|
|
state_1 = a.add_state()
|
|
state_2 = a.add_state()
|
|
|
|
a.mark_as_initial(state_i)
|
|
a.mark_as_final(state_0)
|
|
a.mark_as_final(state_only_0)
|
|
|
|
add_transitions(a, state_i, "0", state_only_0)
|
|
add_transitions(a, state_i, "147", state_1)
|
|
add_transitions(a, state_i, "258", state_2)
|
|
add_transitions(a, state_i, "369", state_0)
|
|
|
|
add_transitions(a, state_only_0, "0123456789", state_leading_zeroes)
|
|
|
|
add_transitions(a, state_0, "0369", state_0)
|
|
add_transitions(a, state_0, "147", state_1)
|
|
add_transitions(a, state_0, "258", state_2)
|
|
|
|
add_transitions(a, state_1, "0369", state_1)
|
|
add_transitions(a, state_1, "147", state_2)
|
|
add_transitions(a, state_1, "258", state_0)
|
|
|
|
add_transitions(a, state_2, "0369", state_2)
|
|
add_transitions(a, state_2, "147", state_0)
|
|
add_transitions(a, state_2, "258", state_1)
|
|
|
|
return a
|
|
|
|
|
|
def add_transitions(a, state_from, chars, state_to):
|
|
for ch in chars:
|
|
a.add_transition(state_from, ch, state_to) |