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

Quiz: Simplifying CFGs and CNF · Question 10 of 10

What does this print? (code)

What does this print?

g = {"S": [("a", "S", "b"), ("a", "b")]}
terminals = sorted({s for bodies in g.values() for b in bodies if len(b) > 1 for s in b if s not in g})
print(["T" + t + " -> " + t for t in terminals])
  1. A.['Ta -> a']
  2. B.['Tb -> b', 'Ta -> a']
  3. C.['Ta -> a', 'Tb -> b']
  4. D.[]
Answer & explanation

Correct answer: ['Ta -> a', 'Tb -> b']

Both a and b appear in long bodies, so each gets its own variable; sorted puts a first.

Can you get the rest right without peeking?

Take the timed Quiz: Simplifying CFGs and CNF quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →