Skip to content
Exercise · Final project · lesson 3 of 8

Exercise: brackets

Write a function is_balanced(text) that checks the brackets in the string text. It returns True if every bracket (, [ or { is closed later by a bracket of the same kind and the pairs do not cross. An extra closing bracket, like any other violation, gives False.

  • is_balanced("{[()()]}") → True
  • is_balanced("([)]") → False
  • is_balanced("())") → False

Constraints: text holds from 0 to 1000 characters; the function skips every character other than the six brackets; it returns True for an empty string and False for an unclosed or extra bracket.

Do not add extra imports unless the task says so explicitly.

brackets.py — python
python · 3 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way