Question: PYTHON 3 PLEASE COMPLETE CODE: Problem Complete the function isBalanced() to take in a string of various types of brackets, and reports back True if
PYTHON 3
PLEASE COMPLETE CODE:
Problem
Complete the function isBalanced() to take in a string of various types of brackets, and reports back True if they are balanced, False otherwise. The types of brackets you will be working with are '()', '[]' and '{}'.
This is much like the problem we solved together in class, except that you have to support three different types of brackets.
Remember, a set of brackets is considered balanced if for each opened bracket, there is a closed one that comes after it, and that no closing or opening bracket is unmatched.
Example
isBalanced('(())') would return True isBalanced('{}()') would return True isBalanced('))((') would return False isBalanced('([)]') would return False
isBalanced('') would return True
CODE FROM PIC:
def isBalanced(string): #TODO # the way we implemented this in-class for one # type of brackets, is: def is_balanced(string): stack = Stack() for i in string: if i == "(": stack.push("(") elif i == ")": if stack.isEmpty(): return False else: stack.pop() return stack.isEmpty()?
nstrucions trom your fedcher 1 def isBalanced(string): Problem #TODO Complete the function isBalanced) to take in astring of various types of brackets, and reports back True if they are balanced, False otherwise. The types of brackets you will be working with are 0.? and U 7 8 the way we implemented this in-class for one # type of brackets, is: def is_balanced(string): This is much like the problem we solved together in class, except that you have to support three different types of brackets. stack -StackO Remember, a set of brackets is considered balanced if for cach opened bracket, there is a closed one that comes after it, and that no closing or opening bracket is unmatched. 10 for i in string: "(": stack.pushC"C" 12 13 14 15 16 17 18 19 20 return stack.isEmptyO if -- Example ")": if stack.isEmptyO: elif i- isBalanced(' ( ( ) ) ) would return True isBalanced)" would return True isBalanced(' would return False isBalanced )would return False isBalanced( would return True return False else: stack.popO
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
