A bracket is considered to be any one of:(),,{},,[, or]Two brackets are considered matched if an opening bracket (i.e.,(,{,[)is followed by a closing bracket (i.e., ), \}, ] ) of the exact same type. There are 3 types of brackets−parenthesis, that is, ( ), braces, that is, \{\} , and square brackets, that is [] . A matching pair of brackets is not balanced if the set of brackets it encloses is not balanced. For example,{[(])}}is not balanced because the set of brackets between \{\} is not balanced. The pair of square brackets encloses a single unbalanced open parenthesis, (, and the pair of parenthesis encloses a single unbalanced closing square bracket, ]. Hence, a sequence of brackets is balanced if the following conditions are met: 1. It contains no unmatched brackets 2. The subset of brackets enclosed within the confines of a matched pair of brackets is also a matched pair of brackets. Function Description: Write a function is_balanced () that takes a string, where each character in the string is a bracket, and returns the Boolean value True if the brackets are balanced; otherwise, returns the Boolean value False Sample Test Case 1. is_balanced(′{[()]}′)=True 2. is_balanced(′{[(])}′)=False 3. is balanced(′{{[(())]]}}′)=True
A bracket is considered to be any one of:(),,{},,[, or]Two brackets are considered matched if an opening bracket (i.e.,(,{,[)is followed by a closing bracket (i.e., ), \}, ] ) of the exact same type. There are 3 types of brackets−parenthesis, that is, ( ), braces, that is, \{\} , and square brackets, that is [] . A matching pair of brackets is not balanced if the set of brackets it encloses is not balanced. For example,{[(])}}is not balanced because the set of brackets between \{\} is not balanced. The pair of square brackets encloses a single unbalanced open parenthesis, (, and the pair of parenthesis encloses a single unbalanced closing square bracket, ]. Hence, a sequence of brackets is balanced if the following conditions are met: 1. It contains no unmatched brackets 2. The subset of brackets enclosed within the confines of a matched pair of brackets is also a matched pair of brackets. Function Description: Write a function is_balanced () that takes a string, where each character in the string is a bracket, and returns the Boolean value True if the brackets are balanced; otherwise, returns the Boolean value False Sample Test Case 1. is_balanced(′{[()]}′)=True 2. is_balanced(′{[(])}′)=False 3. is balanced(′{{[(())]]}}′)=True
Step by step
Solved in 2 steps