← 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])
- A.['Ta -> a']
- B.['Tb -> b', 'Ta -> a']
- C.['Ta -> a', 'Tb -> b']
- 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 →