0
Error with python's project
Im solving python data structures project and all cases are ok but the number 7 is giving me error. The thing is i dont have sololearn plus or that membership so i cant see which is the answer. If anyone has the membership, could check the expected output please. here's my code,by the way: def balanced(expression): contadorA=0 contadorC=0 for i in expression: if i=="(": contadorA+=1 elif i==")": contadorC+=1 if contadorA==contadorC: return True else: return False print(balanced(input()))
4 Réponses
+ 2
Hi! Maybe you dont check the stack to be empty
"...Also, after checking all the parentheses, we need to check the stack to be empty -- if it's not empty, then the parentheses are not balanced."
+ 2
spacePrincess
Even those who have SoloLearn pro cannot see the hidden cases. This is intended so that you think about all possible cases and edge cases to come up with a proper solution.
You're supposed to use a stack like structure, such as a list, and add each '(' item to it, while popping an item for each ')'. When done, if the stack has any items left in it, then it is not balanced. Likewise, if the stack doesn't have any items in it when the current element in the expression is ')', then it can't possibly be balanced.
Note: The if-else at the end of your current function is redundant, and as is could currently be reduced to;
return contadorA == contadorC
+ 1
Ohh right. I guess easy way isnt usefull anymore. Thank you very much
+ 1
I have almost the same code as you have. But i have one more check. I always check in the loop if contadorC <= contadorA, if it is not true, i return false. For me number 7 works fine.
I think test case 7 looks somewhat like this: '(()))('.