Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Can someone help me fix my code. The instructions are: Complete the function NFA ( input _ string, nfa ) that runs an NFA. In
Can someone help me fix my code. The instructions are:
Complete the function NFAinputstring, nfa that runs an NFA.
In particuar, modify the code on the following lines:
line : initialize currentstates note the plural!
line : define x
line : define the NFA given in Figure ; use, as an example, the definition of dfa given in line
n is so the th symbol from the end should be it should reject
class FA:
def initself alphabet, states, transition, acceptingstates:
self.alphabet alphabet
self.states states
self.transition transition
self.acceptingstates acceptingstates
def DFAinputstring, dfa:
# set currentstate to initial state
currentstate dfa.states
# run through the input string
for symbol in inputstring:
i dfa.states.indexcurrentstate
j dfa.alphabet.indexsymbol
currentstate dfa.transitionij
return currentstate
def NFAinputstring, nfa:
# set currentstate to initial state
currentstates nfastates
# run through the input string
for symbol in inputstring:
x set
for state in currentstates:
i nfa.states.indexstate
j nfa.alphabet.indexsymbol
x setnfatransitionij
currentstates x
return currentstates
def main:
# Read in the inputstring
inputstring input
# Define a DFA
dfa FAqrrqqrq
# Define the NFA given in Figure for n
nfa FAqrstuqqrssttuuu
# Run the DFA on the input stri
# finalstate DFAinputstring, dfa
# if finalstate in dfaacceptingstates:
# printAccept
# else:
# printReject
# Run the NFA on the input string
finalstates NFAinputstring, nfa
if setfinalstates & setnfaacceptingstates:
printAccept
else:
printReject
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started