Predict the output
What does this print?
line = "{a[b(c)d]e}("
opened = []
for ch in line:
if ch in "([{":
opened.append(ch)
elif ch in ")]}":
opened.pop()
print(opened)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A ['(']
Right: everything pairs off except the last open bracket.
A non-empty stack at the end means something was left open, so this line does not balance.
Why not the others?
- B []. The final '(' is never closed.
- C ['{', '(']. The '{' is closed by the '}' before the last '('.