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()))

27th Mar 2021, 9:33 AM
Md Sohrab Akhtar Emam
Md Sohrab Akhtar Emam - avatar
4 odpowiedzi
0
Forget about start with ")" and end with "("
27th Mar 2021, 10:02 AM
Илья Мирошник
Илья Мирошник - avatar
0
Then how it can be resolve?
27th Mar 2021, 10:05 AM
Md Sohrab Akhtar Emam
Md Sohrab Akhtar Emam - avatar
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()))
27th Mar 2021, 10:06 AM
Илья Мирошник
Илья Мирошник - avatar
0
Thanks now understand
28th Mar 2021, 5:09 AM
Md Sohrab Akhtar Emam
Md Sohrab Akhtar Emam - avatar