0
Guys anyone help me with this code to solve this problem (BALANCED PARANTHESIS) .
class check: def __init__(self): self.l=[] def is_empty(self): return self.l==[] def push(self,d): self.l.insert(0,d) def pop(self): self.l.pop(0) def print_all(self): return self.l def balanced(a): #your code goes here x=check() t=len(a) i=0 f=0 while i<t: if a[i]=="(": x.push(a[i]) i+=1 for i in a: if i==")": if x.is_empty(): f=1 break x.pop() if not x.is_empty() or f==1 : print("False") else: print("True") balanced(input())
4 Respostas
+ 4
Introduce this code in a private python code, and upload it here, to make it more readable
+ 4
Any reason why you changed the code they provide?
Is your i+=1 meant to be aligned with the x.push(a[i])?
+ 3
Oh and I think I see that you've made the same mistake as many others.
You're expecting that the parentheses will always be paired and ordered.
Your code most likely works in situations like ()()(() or ((())) etc
What does your code do in a situation like )(()() or ))(( etc
0
Why are you handling your push and pop operations in different loops? The logic is mostly correct but I feel like you'll fail some test cases because you are checking for open and close parens in two steps and it should really be done at the same time for this problem. At least the way you are doing the checks