Modify the bracketsBalance function so that the caller can supply the brackets to match as arguments to this function. The second argument should be a list of beginning brackets, and the third argument should be a list of ending brackets. The pairs of brackets at each position in the two lists should match; that is, position 0 in the two lists might have [ and], respectively. You should be able to modify the code for the function so that it does not reference literal bracket symbols, but just uses the list arguments. (Hint: The method index returns the position of an item in a list.) the bracketsBalance function: def bracketsBalance(exp): """exp is a string that represents the expression""" stk = LinkedStack() for ch in exp: if ch in [’[’, ’(’]: stk.push(ch) elif ch in [’]’, ’)’]: if stk.isEmpty(): # Create a new stack # Scan across the expression # Push an opening bracket # Process a closing bracket # Not balanced return False chFromStack = stk.pop() # Brackets must be of same type and match up if ch == ’]’ and chFromStack != ’[’ or \ ch == ’)’ and chFromStack != ’(’: return False Use this Python template: from collections import deque class Stack: ''' TODO: Remove the "pass" statements and implement each method Add any methods if necesssary DON'T use any builtin stack class to store your items ''' def __init__(self): # Constructor function pass def isEmpty(self): # Returns True if the stack is empty or False otherwise pass def len(self): # Returns the number of items in the stack pass def peek(self): # Returns the item at the top of the stack pass def push(self, item): # Adds item to the top of the stack pass def pop(self): # Removes and returns the item at the top of the stack pass def bracketsBalance(input_str,opening_list,closing_list): isBalanced = True stk=Stack() #TODO: Your work here # Return if input_str is balanced or not return isBalanced if __name__ == "__main__": my_str = "([{))}" opening_list=['(', '[', '{'] closing_list=[')', ')', '}'] print(bracketsBalance(my_str,opening_list,closing_list)) # Correct Output: False #TODO (optional): Your testing code here

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter17: Linked Lists
Section: Chapter Questions
Problem 18PE
icon
Related questions
Question

4. Modify the bracketsBalance function so that the caller can supply the brackets to match as arguments to this function. The second argument should be a list of beginning brackets, and the third argument should be a list of ending brackets. The pairs of brackets at each position in the two lists should match; that is, position 0 in the two lists might have [ and], respectively. You should be able to modify the code for the function so that it does not reference literal bracket symbols, but just uses the list arguments. (Hint: The method index returns the position of an item in a list.)

the bracketsBalance function:

def bracketsBalance(exp):
"""exp is a string that represents the expression"""

stk = LinkedStack() for ch in exp:

if ch in [’[’, ’(’]: stk.push(ch)

elif ch in [’]’, ’)’]: if stk.isEmpty():

# Create a new stack # Scan across the expression

# Push an opening bracket

# Process a closing bracket
# Not balanced

return False
chFromStack = stk.pop()
# Brackets must be of same type and match up if ch == ’]’ and chFromStack != ’[’ or \

ch == ’)’ and chFromStack != ’(’:

return False

Use this Python template:

from collections import deque
class Stack:
'''
TODO: Remove the "pass" statements and implement each method
Add any methods if necesssary
DON'T use any builtin stack class to store your items
'''
def __init__(self): # Constructor function
pass
def isEmpty(self): # Returns True if the stack is empty or False otherwise
pass
def len(self): # Returns the number of items in the stack
pass
def peek(self): # Returns the item at the top of the stack
pass
def push(self, item): # Adds item to the top of the stack
pass
def pop(self): # Removes and returns the item at the top of the stack
pass
def bracketsBalance(input_str,opening_list,closing_list):
isBalanced = True
stk=Stack()
#TODO: Your work here
# Return if input_str is balanced or not
return isBalanced
if __name__ == "__main__":
my_str = "([{))}"
opening_list=['(', '[', '{']
closing_list=[')', ')', '}']
print(bracketsBalance(my_str,opening_list,closing_list)) # Correct Output:
False
#TODO (optional): Your testing code here

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Lists
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning