0

Balanced Parentheses

Hello ladies and gentelmen What is wrong with this input def balanced(expression): expression = list(expression.split()) try: for i in expression: if i == '(': expression.insert(0, i) elif i == ')': expression.pop(0, i) finally: if len(expression) == 0: return True elif len(expression) > 0: return False print(balanced(input()))

30th Mar 2021, 12:02 AM
Joshua chola
Joshua chola - avatar
15 Respuestas
+ 5
I see the most of answers going to define own class, that can ofcourse. but there is a simple way to do the same thing by using list class in python , i used the code below and it works : def balanced(expression): nn = [] for char in expression: if char == '(': nn.insert(0, char) elif char == ')': if len(nn) == 0: return False nn.pop() return len(nn) == 0 print(balanced(input())
25th Sep 2021, 7:35 PM
Murtada abed
Murtada abed - avatar
+ 4
Consider a case string starts with ")" Now we know paranthese aren't balanced But your code will not do anything Hope It Helps You 😊
30th Mar 2021, 4:30 AM
Hacker Badshah
Hacker Badshah - avatar
+ 3
At least ur trying class Stack: def __init__(self): self.items = [] def push(self, item): self.items.insert(0, item) def pop(self): return self.items.pop(0) s = Stack() inp = input() def balanced(inp): try: for i in inp: if i == str("("): s.push('(') elif i == str(")"): s.pop() else: continue if len(s.items) == 0: print(" True") else: print("False") except: print("False") balanced(inp)
26th Jun 2021, 3:34 AM
dead falcon
+ 3
@Charan Manikanta Nalla From begin : For every turn if you found "(" put it in list . If you found ")" delet the last "(" from the list , => if list is empty it means i got a ")" without "(" so break down and the result is False .. its not balanced . there is no need to check any more . That why i put => return : False . if list is not empty it mean's i have at least one "(" so i will delete it and so on
9th Nov 2021, 11:33 PM
Murtada abed
Murtada abed - avatar
+ 1
Still struggling
30th Mar 2021, 7:03 AM
Joshua chola
Joshua chola - avatar
+ 1
class Stack: def __init__(self): self.items = [] def push(self, item): self.items.insert(0, item) def pop(self): try: return self.items.pop(0) except: par.push(")") def is_empty(self): if self.items == []: return True else: return False prompt = input() par = Stack() for c in prompt: if c == "(": par.push("(") elif c == ")": par.pop() print(par.is_empty())
17th Jul 2021, 3:17 PM
Ale
Ale - avatar
+ 1
@Murtada abed I couldn't quite understand the "return False" part. Can you explain that a little bit, please?
9th Nov 2021, 6:40 PM
Charan Manikanta Nalla
Charan Manikanta Nalla - avatar
+ 1
pop expected only one argument
26th Jan 2022, 8:56 PM
or haim perets
or haim perets - avatar
0
Sandeep Kushwaha I have tried that no i dont get it Demostrate what you are taking about it.
31st Mar 2021, 4:35 AM
Joshua chola
Joshua chola - avatar
0
Looser what does “try” in your code do? I cant remeber learning it here
13th Oct 2021, 2:10 PM
Loai Wahdieh
0
Welp, this is what worked for me. The "except IndexError:" is what solved the issue of having any extra ")" at the beginning of the expression. def balanced(expression): cache = [] try: for char in expression: if char == "(": cache.insert(0,"(") elif char == ")": cache.pop(0) except IndexError: return False if cache == []: return True else: return False print(balanced(input()))
17th Feb 2022, 1:28 AM
Nathanael Bell
0
def balanced(expression): #your code goes here stack = [] for b in expression: if b == "(": stack.insert(0,b) elif b == ")" and stack != []: stack.pop() elif b == ")" and stack == []: stack.insert(0, b) return len(stack) == 0 print(balanced(input()))
24th Jun 2022, 10:01 AM
Aryan Ahmed Adil
Aryan Ahmed Adil - avatar
0
class Stack: def __init__(self): self.items = [] def push(self, item): self.items.insert(0, item) def pop(self): self.items.pop() def list_stack(self): return self.items s = Stack() z = input() def push_bal(x): for i in x: if i == '(': s.push(i) return s.list_stack() def pop_bal(x): for i in x: if i == ')': s.pop() return s.list_stack() def check_bal(z): if ')' and '(' not in z: print(False) else: try: push_bal(z) pop_bal(z) except : print(False) else: L = s.list_stack() print(len(L) == 0) check_bal(z);
3rd Jan 2023, 10:11 PM
Biruk Tzehaye
Biruk Tzehaye - avatar
0
Actually this code donot pass one of the tests. Please let me know . I am not using a pro version so my mistake is hidden.
3rd Jan 2023, 10:13 PM
Biruk Tzehaye
Biruk Tzehaye - avatar