Hackbright Code Challenges

Balanced Brackets

Balanced Brackets

Whiteboard

Harder

Challenge

Medium

Concepts

Conditionals, Loops

Download

balanced-brackets.zip

Solution

Balanced Brackets: Solution


Given a string, return True or False depending on whether the string’s opening-and-closing marks (that is, brackets) are balanced. Account for the following bracket types:

Type

Opener

Closer

Parentheses

(

)

Curly Braces

{

}

Square Brackets

[

]

Angle Brackets

<

>

The string doesn’t need to have all four types of brackets, but if an open bracket appears, the pair should also be closed correctly.

Many of the same test cases from our Balanced Parentheses problem apply to this expanded problem, with the caveat that they must check all types of brackets.

These are fine:

>>> has_balanced_brackets("<ok>")
True

>>> has_balanced_brackets("<[ok]>")
True

>>> has_balanced_brackets("<[{(yay)}]>")
True

These are invalid, since they have too many open brackets:

>>> has_balanced_brackets("(Oops!){")
False

>>> has_balanced_brackets("{[[This has too many open square brackets.]}")
False

These are invalid, as they close brackets that weren’t open:

>>> has_balanced_brackets(">")
False

>>> has_balanced_brackets("(This has {too many} ) closers. )")
False

Here’s a case where the number of brackets opened matches the number closed, but in the wrong order:

>>> has_balanced_brackets("<{Not Ok>}")
False

If you receive a string with no brackets, consider it balanced:

>>> has_balanced_brackets("No brackets here!")
True

We’ve given you a file called balancedbrackets.py, which contains a has_balanced_brackets function:

balancedbrackets.py
def has_balanced_brackets(phrase):
    """Does a given string have balanced pairs of brackets?

    Given a string as input, return True or False depending on whether the
    string contains balanced (), {}, [], and/or <>.
    """

However, this function is unimplemented. Implement it to uncover any pesky extra brackets!