Answered step by step
Verified Expert Solution
Question
1 Approved Answer
2 Balanced Parentheses The problem of balanced parenthesis is that given a string s containing just the characters (,)', '{', '3', '['& 'l', determine if
2 Balanced Parentheses The problem of balanced parenthesis is that given a string s containing just the characters (,)', '{', '3', '['& 'l', determine if all parentheses are correctly matched. For instance, " ()", "() [] {}", "{[]}" are examples of balanced strings. "()", "(D)]", "())", "O) (" are examples of unbalanced strings. Suppose I have a class Stack with a private list attribute, and the following public methods: 1. size (self): return the size of the stack. 2. top(self): return the top element of the stack (given size > 0). 3. pop (self): remove the top element of the stack (given size > 0). 4. push(self, x): push x to the stack. Also, I have function index(1, a) which takes a list 1 and an element x and returns the index of x in 1, assuming x is present in 1. For example, index(['a','b', 'c','d'],'c') gives 2. The following function balanced takes as input a string and returns a bool denoting whether the string is balanced or not. The helper function check ensures that the execution is stopped if we are unable to find an open bracket corresponding to some closed bracket. Fill in the five blanks (a), (b), (c), (d) and (e) to make the code work. You don't need to write the entire code to your sheet again. Just writing the values for (a)-(e) is enough. [5 marks] def check(stk, x): if stk.size() == 0: return True if --(a).- != x: return True --(b).- return false def balanced(s): stk = Stack() op1 ['e', '[', '{'] op2 = '' '11
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started