0
Where I did mistake ?
def balanced(expression): #your code goes here stack =[] count =0 rem =0 for i in range(len(expression )): if '(' in expression[i] : stack.insert(0,'(') count +=1 elif ')' in expression[i]: stack.pop(0) rem +=1 else: continue if stack == [] and count !=0 and rem !=0: return True elif count == 0 and rem ==0: return False elif stack[0] =='(' : return False print(balanced(input()))
4 Respuestas
0
Forget about start with ")" and end with "("
0
Then how it can be resolve?
0
Just add check on this in start of function
def balanced(expression):
#your code goes here
lst = []
if expression [0]==')':
return False
if expression [len(expression )-1]=='(':
return False
for i in expression:
if i == '(':
lst.insert(0,i)
elif i == ')':
lst.pop(0)
#print(lst)
if len(lst) == 0:
return True
else:
return False
#print(balanced('(a( ) eee) ))'))
print(balanced(input()))
0
Thanks now understand