Question: Python Let s be a string. The string can have any characters. Specifically it can have the parenthesis characters '(',')', [', '1' We say that



Python Let s be a string. The string can have any characters. Specifically it can have the parenthesis characters '(',')', [', '1' We say that s is balanced if all these parenthesis characters either do not appear in s at all, or they do appear in one legal nested structures. That means, all the open parenthesis characters must appear before all closing parenthesis characters, and also '(' must be legally closed by ')' and '' must be legally closed by '1'. Other characters may appear as well. We just ignore them. Examples for balanced strings : ' '[' ((([[(0)]]))) As said, in addition to the parenthesis characters, we can have also any other characters which we just ignore. They do NOT affect the balance of the string. '[ ]' is balanced, therefore also 'a@#[656]?>' #just ignore all other characters. Examples for NON balanced strings : '()' '(( '00' #Notice - this is NOT blanced in our question, ONLY'(0)'. The recursive function balanced_r(s) gets a string s and retums True ifs is balanced. Otherwise False is retumed. Complete the following code for the function balanced_r. Write the full code of the function in the answer notebook. def balanced_r(s): if len(s)=0: # number of lines does not matter. if s[0] not in {C,T,'','1'}: #number of lines does not matter. # may have here other checks, if needed. Number of lines does not matter. Requirements: The function MUST be RECURSIVE, WITHOUT loops
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
