PythiloLesson 5 of 32 · progress kept in this browser

Match brackets with a stack

The most recently opened bracket must close first. That last-in, first-out order is exactly what a stack gives you: push every opener, and every closer must match the top.

stack = []
for char in "([])":
    print(char, stack)
Your task

Write balanced(text) returning True when every (, [ and { is closed by its own kind in the right order. Ignore all other characters.