+ 1
Balanced Parentheses? [Py]
I keep getting False even when it should be True/: def isBalanced(expr): stack = [] for char in expr: if char in ["(", "{", "["]: stack.append(char) else: if not stack: return False current_char=stack.pop() if current_char == '(': if char != ')': return False if current_char == '{': if char != '}': return False if current_char == '[': if char != ']': return False if stack: return True return False print(isBalanced(input()))
5 Réponses
+ 4
function defination starts with ___? def missing
def isBalanced(expr) :
Share link by saving code..
+ 4
https://code.sololearn.com/cIoqL6hpHauH/?ref=app
https://code.sololearn.com/cO03cO5e5cSS/?ref=app
https://code.sololearn.com/c76dLc83PvAq/?ref=app
Let you inspire from 100 codes dealing with that topic.
As a beginner one should spend hours for analysing other people's code.
I like sandeeps solution. it is straightforward.
+ 3
Guessed it so asked to share link with original code... Better always share link by saving it, especially for python..
To check it, it took to adjust indentations many..
But finally, i think problem in:
if stack :
return True #this means list not empty, should return false
next return True, instead of false..
Swap last 2.
edit:
#BLAKE
your code works for ex: () but not works for (x)
you need to change logic to reversing in
if char == ')' :
if current_char != '(' :
return false.
+ 1
Thank you, Jayakrishna🇮🇳 ! I tried that as well and it still wasnt working for some reason. I ended up rewritting the code entirely using the “try: except:” method and it worked!
0
That was a typo in the comments. I do in fact have “def” in my actual code