Answered step by step
Verified Expert Solution
Link Copied!

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 NFA(input_string, nfa) that runs an NFA.
In particuar, modify the code on the following lines:
- line 20: initialize current_states (note the plural!)
- line 27: define x
- line 40: define the NFA given in Figure 8.2; use, as an example, the definition of dfa1 given in line 40
- n is 5, so the 5th symbol from the end should be 1, it should reject "01010101111111111001010000111110000000"
class FA:
def __init__(self, alphabet, states, transition, accepting_states):
self.alphabet = alphabet
self.states = states
self.transition = transition
self.accepting_states = accepting_states
def DFA(input_string, dfa):
# set current_state to initial state
current_state = dfa.states[0]
# run through the input string
for symbol in input_string:
i = dfa.states.index(current_state)
j = dfa.alphabet.index(symbol)
current_state = dfa.transition[i][j]
return current_state
def NFA(input_string, nfa):
# set current_state to initial state
current_states ={nfa.states[0]}
# run through the input string
for symbol in input_string:
x = set()
for state in current_states:
i = nfa.states.index(state)
j = nfa.alphabet.index(symbol)
x |= set(nfa.transition[i][j])
current_states = x
return current_states
def main():
# Read in the input_string
input_string = input()
# Define a DFA
dfa1= FA(['0','1'],['q','r'],[['r','q'],['q','r']],{'q'})
# Define the NFA given in Figure 8.2 for n=5
nfa1= FA(['0','1'],['q','r','s','t','u'],[[['q'],['q','r']],[['s'],['s']],[['t'],['t']],[['u'],['u']]],{'u'})
# Run the DFA on the input stri
# final_state = DFA(input_string, dfa1)
# if final_state in dfa1.accepting_states:
# print("Accept")
# else:
# print("Reject")
# Run the NFA on the input string
final_states = NFA(input_string, nfa1)
if set(final_states) & set(nfa1.accepting_states):
print("Accept")
else:
print("Reject")
if __name__=="__main__":
main()
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

How do you protect yourself against online fraud?

Answered: 1 week ago