The question is; Write a deterministic automata code for the language with ∑ = {0, 1} accepts the only input 101.
My classmate sent the following code but it doesnt seem to work. PLEASE HELP.
Code:
from automata.fa.dfa import DFA
dfa=DFA(
states={'q0','q1','q2','q3'},
inputs symbols={'0','1'},
transitions={
'q0':{'0':'q0','1':'q1'}
'q1':{'0':'q2','1':'q1'}
'q2':{'0':'q2','1':'q3'}
'q3':{'0':'q3','1':'q3'}
},
initial state='q0',
final state={'q3'}
)
dfa.read_input('101') #answer is '101'
dfa.read_input('010') #answer is 'error'
if dfa.accepts_inputs('101'):
print('accepted')
else:
print('rejected')
What doesn't work. Does it give the wrong output or does it report an error and quit? If an error, pleas post the entire error message, including any traceback.
dfa.read_input('101') should return 'q3'
dfa.read_input('010') should return 'q2'
You should tell your friend the transitions map is incorrect. I am pretty sure it would accept '0101' or '1101' or '1011010101' or many other combinations.
To test your solution you need more test cases. I think you would learn quite a lot from using read_input_stepwise.
(Apr-27-2020, 05:48 PM)deanhystad Wrote: [ -> ]What doesn't work. Does it give the wrong output or does it report an error and quit? If an error, pleas post the entire error message, including any traceback.
I edited the code a bit.
from automata.fa.dfa
import DFA
dfa=DFA(
states={'q0','q1','q2','q3'},
inputs_symbols= {'0','1'},
transitions={
'q0':{'0':'q0','1':'q1'},
'q1':{'0':'q2','1':'q1'},
'q2':{'0':'q2','1':'q3'},
'q3':{'0':'q3','1':'q3'}
},
initial_state='q0',
final_state={'q3'}
)
dfa.read_input('101') #answer is '101'
dfa.read_input('010') #answer is 'error'
if dfa.accepts_inputs('101'):
print('accepted')
else:
print('rejected')
The following is the error:
File "<ipython-input-1-427c1524b6c1>", line 1
from automata.fa.dfa
^
SyntaxError: invalid syntax
you cannot split from automata.fa.dfa import DFA
on 2 lines.
(Apr-27-2020, 05:56 PM)buran Wrote: [ -> ]you cannot split from automata.fa.dfa import DFA
on 2 lines.
Still doesn't work. I am really new to python and coding.
This is the error:
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-d35aa8898687> in <module>
----> 1 from automata.fa.dfa import DFA
2 dfa=DFA(
3 states={'q0','q1','q2','q3'},
4 inputs_symbols= {'0','1'},
5 transitions={
ModuleNotFoundError: No module named 'automata'