1. Engineering
  2. Computer Science
  3. constants balanced 0 moreleft 1 moreright ...

Question: constants balanced 0 moreleft 1 moreright ...

Question details

5. Write and test the following function that uses a Stack: Detez.ine2 і: ù 2tzini conta-L2 วฉในเ22dbzacket2 cı not. Hor-21 ackat dd the function to a lyDev module named tons,zr. Test it trom a PyDev module named t5. See: Stacks - Balancedl Brackets The unction tnust use one ㅀnd on one sia( Io so ve lhe poblern. Show enouah tesl s lo (over d balante(į string ard the three l pes o unbrl dned strini s n emipty string has brllanı ed brdt cels; a strint that contains no brackets has balanced brackets Your testing program should convert these constants into a readable message for the user (the furnction itself must not do anyp!) if balanced-EiliNCED

# Constants
BALANCED = 0
MORE_LEFT = 1
MORE_RIGHT = 2
MISMATCHED = 3

def has_balanced_brackets(string):
    """
    -------------------------------------------------------
    Determines if a string contains balanced brackets or not. Non-bracket
    characters are ignored. Uses a stack. Brackets include {}, [], (), <>.
    Use: balanced = has_balanced_brackets(string)
    -------------------------------------------------------
    Parameters:
        string - the string to test (str)
    Returns:
        balanced (int) -
            BALANCED if the brackets in string are balanced
            MISMATCHED if the brackets in string are mismatched
            MORE_RIGHT if there are more right brackets than left in string
            MORE_LEFT if there are more left brackets than right in string
    -------------------------------------------------------
    """

The function must use one and only one stack to solve the problem. Show enough tests to cover a balanced string and the three types of unbalanced strings. An empty string has balanced brackets; a string that contains no brackets has balanced brackets.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution