🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Regular Grammars and Finite Automata: all 10 questions

Quiz: Regular Grammars and Finite Automata · Question 9 of 10

What does this print? (code)

What does this print?

grammar = {"S": ["aA"], "A": ["aA", "bA", "b"]}
nfa = {}
for x, bodies in grammar.items():
    for body in bodies:
        target = body[1:] if len(body) == 2 else "F"
        nfa.setdefault((x, body[0]), set()).add(target)
print(sorted(nfa[("A", "b")]))
  1. A.['A']
  2. B.['F']
  3. C.['A', 'F']
  4. D.['b']
Answer & explanation

Correct answer: ['A', 'F']

A → bA gives the arrow to A and A → b gives the arrow to F.

Can you get the rest right without peeking?

Take the timed Quiz: Regular Grammars and Finite Automata quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →