0
Please help me to figure it out what is wrong with this code! It's the last lesson of data structures course 🙏
We have to print True or False if the input expression is equilibrated or not (parentheses equilibrated) Here's my code: def balanced(expression): #tu código va aquí items=expression.split() for i in range(len(items)): par= [] if i== "(": par.insert(0,"(") elif i==")": if par!= []: par.pop(0) else: return("False") if par== []: print("True") else: print("False") print(balanced(input()))
2 odpowiedzi
+ 2
Well, firstly, remove the extra print function, otherwise you essentially get print(print()).
Secondly, what is the value of the variable "i"?
And thirdly, "par==[]" will always be True 😉
0
Mabel Zavala Moreira
If you print function then return in function otherwise it would print 'None'
Define empty list outside the loop
Change your loop like because no need to split string
for i in expression:
Don't compare list like that. Get length of list then compare with 0 which means list is empty.
------Solution-----
def balanced(expression):
#tu código va aquí
items = expression.split()
par = []
#for i in range(len(items)):
for i in expression:
#par = []
if i == "(":
par.insert(0, "(")
elif i == ")":
#if par != []:
if len(par) > 0:
par.pop(0)
else:
return False
#if par == []:
return len(par) == 0
print(balanced(input()))