Hackbright Code Challenges

Balanced Parentheses

Balanced Parentheses

Whiteboard

Medium

Concepts

Conditionals, Loops

Download

balanced-parentheses.zip

Solution

Balanced Parentheses: Solution


Given a string, return True or False depending on whether that string has balanced parentheses.

For the purposes of this problem, you only need to worry about parentheses (( and )), not other opening-and-closing marks, like curly brackets, square brackets, or angle brackets.

For example:

 >>> has_balanced_parens("()")
 True

 >>> has_balanced_parens("(Oh Noes!)(")
 False

 >>> has_balanced_parens("((There's a bonus open paren here.)")
 False

 >>> has_balanced_parens(")")
 False

 >>> has_balanced_parens("(")
 False

>>> has_balanced_parens("(This has (too many closes.) ) )")
False

You may consider a string with no parentheses balanced:

>>> has_balanced_parens("Hey...there are no parens here!")
True

We given you a file, balancedparentheses.py, with a has_balanced_parens function:

balancedparentheses.py
def has_balanced_parens(phrase):
    """Does a string have balanced parentheses?"""

However, this function is unimplemented. Implement this function to uncover any any stray parentheses!