← Quiz: CFG and PDA: all 10 questions
Quiz: CFG and PDA · Question 9 of 10
What does this print? (code)
What does this print?
pairs = {")": "(", "]": "[", "}": "{"}
def ok(s):
stack = []
for ch in s:
if ch in "([{":
stack.append(ch)
elif not stack or stack.pop() != pairs[ch]:
return False
return not stack
print([ok(s) for s in ["([]{})", "([)]", "(("]])
- A.[True, True, False]
- B.[True, False, False]
- C.[False, False, False]
- D.[True, False, True]
Answer & explanation
Correct answer: [True, False, False]
The stack matches the nearest open bracket: ([)] closes ( while [ is on top, and (( leaves the stack non-empty.
Can you get the rest right without peeking?
Take the timed Quiz: CFG and PDA quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →