Related
I came across this exercise of checking whether or not the simple brackets "(", ")" in a given string are matched evenly.
I have seen examples here using the stack command which I haven't encountered yet. So I attempted a different approach. Can anyone tell me where I am going wrong?
def matched(str):
ope = []
clo = []
for i in range(0,len(str)):
l = str[i]
if l == "(":
ope = ope + ["("]
else:
if l == ")":
clo = clo + [")"]
else:
return(ope, clo)
if len(ope)==len(clo):
return True
else:
return False
The idea is to pile up "(" and ")" into two separate lists and then compare the length of the lists. I also had another version where I had appended the lists ope and clo with the relevant I which held either ( or ) respectively.
A very slightly more elegant way to do this is below. It cleans up the for loop and replaces the lists with a simple counter variable. It also returns false if the counter drops below zero so that matched(")(") will return False.
def matched(str):
count = 0
for i in str:
if i == "(":
count += 1
elif i == ")":
count -= 1
if count < 0:
return False
return count == 0
This checks whether parentheses are properly matched, not just whether there is an equal number of opening and closing parentheses. We use a list as a stack and push onto it when we encounter opening parentheses and pop from it when we encounter closing parentheses.
The main problem with your solution is that it only counts the number of parentheses but does not match them. One way of keeping track of the current depth of nesting is by pushing opening parentheses onto a stack and popping them from the stack when we encounter a closing parenthesis.
def do_parentheses_match(input_string):
s = []
balanced = True
index = 0
while index < len(input_string) and balanced:
token = input_string[index]
if token == "(":
s.append(token)
elif token == ")":
if len(s) == 0:
balanced = False
else:
s.pop()
index += 1
return balanced and len(s) == 0
My solution here works for brackets, parentheses & braces
openList = ["[", "{", "("]
closeList = ["]", "}", ")"]
def balance(myStr):
stack = []
for i in myStr:
if i in openList:
stack.append(i)
elif i in closeList:
pos = closeList.index(i)
if stack and (openList[pos] == stack[-1]):
stack.pop()
else:
return "Unbalanced"
if len(stack) == 0:
return "Balanced"
print(balance("{[()](){}}"))
Most blatant error done by you is:
if l == ")":
clo = clo + [")"]
else:
return(ope, clo) # here
By using return, you exit from function when first char not equal to "(" or ")" is encountered. Also some indentation is off.
Minimal change which allows your code to run (although it won't give correct answers for all possible input strings) is:
def matched(str):
ope = []
clo = []
for i in range(0,len(str)):
l = str[i]
if l == "(":
ope = ope + ["("]
elif l == ")":
clo = clo + [")"]
if len(ope)==len(clo):
return True
else:
return False
The problem with your approach is that you don't consider the order. Following line would pass: ))) (((.
I'd suggest to keep the count of open and closed parenthesis:
counter starts from 0
every ( symbol increments counter
every ) symbol decrements counter
if at any moment counter is negative it is an error
if at the end of the line counter is 0 - string has matching parenthesis
a = "((a+b)*c)+(b*a))"
li = list(a)
result = []
for i in range(0, len(a)):
if a[i] == "(":
result.append(i)
elif a[i] == ")":
if len(result) > 0:
result.pop()
else:
li.pop(i)
for i in range(0, len(result)):
li.pop(result[i])
print("".join(li))
this code works fine
def matched(s):
p_list=[]
for i in range(0,len(s)):
if s[i] =='(':
p_list.append('(')
elif s[i] ==')' :
if not p_list:
return False
else:
p_list.pop()
if not p_list:
return True
else:
return False
You can do this in a couple of lines using accumulate (from itertools). The idea is to compute a cumulative parenthesis level going through the string with opening parentheses counting as level+1 and closing parentheses counting as level-1. If, at any point, the accumulated level falls below zero then there is an extra closing parenthesis. If the final level is not zero, then there is a missing closing parenthesis:
from itertools import accumulate
def matched(s):
levels = list(accumulate((c=="(")-(c==")") for c in s))
return all( level >= 0 for level in levels) and levels[-1] == 0
An alternative to check for balanced nested parentheses:
def is_balanced(query: str) -> bool:
# Alternative: re.sub(r"[^()]", "", query)
query = "".join(i for i in query if i in {"(", ")"})
while "()" in query:
query = query.replace("()", "")
return not query
for stmt in [
"(()()()())", # True
"(((())))", # True
"(()((())()))", # True
"((((((())", # False
"()))", # False
"(()()))(()", # False
"foo", # True
"a or (b and (c or d)", # False
"a or (b and (c or d))" # True
"a or (b and (c or (d and e)))", # True
]:
print(stmt)
print("Balanced:", is_balanced(stmt))
print()
It works by:
Removing everything but parentheses
Recursively remove innermost parentheses pairs
If you're left with anything besides the empty string, the statement is not balanced. Otherwise, it is.
if the parenthesis sequence is not an issue (strings like )( ) this code is faster :
def matched_parenthesis(s):
return s.count('(') == s.count(')')
Tested with 15KB string, it is ~20μs v.s. 1ms iterating over the whole string.
And for me the order is not an issue as the underlying protocol guaranties that the string is well-formed.
In case u also need to find the position of the first mismatching bracket from left u can use the below code which also cover certain edge cases:
def isBalanced(expr):
opening=set('([{')
new=set(')]}{[(')
match=set([ ('(',')'), ('[',']'), ('{','}') ])
stack=[]
stackcount=[]
for i,char in enumerate(expr,1):
if char not in new:
continue
elif char in opening:
stack.append(char)
stackcount.append(i)
else:
if len(stack)==0:
print(i)
return False
lastOpen=stack.pop()
lastindex=stackcount.pop()
if (lastOpen, char) not in match:
print (i)
return False
length=len(stack)
if length!=0:
elem=stackcount[0]
print (elem)
return length==0
string =input()
ans=isBalanced(string)
if ans==True:
print("Success")
if "(" ,")" these two characters are not present then we don't want to return true or false just return no matching found. if matching found i just checking the count of both characters are same then return true, else return false
def matched(str):
count1=0
count2=1
for i in str:
if i =="(":
count1+=1:
elif i==")":
count2+=1:
else:
print "no matching found for (,)"
if count1==count2:
return True
else:
return False
Simplest of all , though all of you guys have done good:
def wellbracketed(s):
left=[]
right=[]
for i in range(0,len(s)):``
if s[i]=='(':
left=left+['(']
elif s[i]==')':
if len(left)!=0:
right=right+[')']
else:
return False
return(len(left)==len(right))
here's another way to solve it by having a counter that tracks how many open parentheses that are difference at this very moment.
this should take care all of the cases.
def matched(str):
diffCounter = 0
length = len(str)
for i in range(length):
if str[i] == '(':
diffCounter += 1
elif str[i] == ')':
diffCounter -= 1
if diffCounter == 0:
return True
else:
return False
input_str = "{[()](){}}"
strblance=""
for i in input_str:
if not strblance:
strblance = strblance+i
elif (i is '}' and strblance[len(strblance)-1] is '{') \
or ( i is']'and strblance[len(strblance)-1] is '[') \
or ( i is ')'and strblance[len(strblance)-1] is '('):
strblance = strblance[:len(strblance)-1]
else:
strblance = strblance+i
if not strblance:
print ("balanced")
else:
print ("Not balanced")
More advanced example in which you additionally need to check a matching of square brackets '[]' and braces '{}' pars.
string = '([]{})'
def group_match(string):
d = {
')':'(',
']':'[',
'}':'{'
}
list_ = []
for index, item in enumerate(string):
if item in d.values():
list_.append(item)
elif (item in d.keys()) and (d.get(item) in list_):
list_.pop()
return len(list_) == 0
The simplest code ever!!
def checkpar(x):
while len(''.join([e for e in x if e in "()"]).split('()'))>1: x=''.join(x.split('()'))
return not x
you can check this code.
This code don't use stack operations.
def matched(s):
count = 0
for i in s:
if i is "(":
count += 1
elif i is ")":
if count != 0:
count -= 1
else:
return (False)
if count == 0:
return (True)
else:
return (False)
#function to check if number of closing brackets is equal to the number of opening brackets
#this function also checks if the closing bracket appears after the opening bracket
def matched(str1):
if str1.count(")")== str1.count("("):
p1=str1.find("(")
p2=str1.find(")")
if p2 >= p1:
str1=str1[p1+1:p2]+ str1[p2+1:]
if str1.count(")")>0 and str1.count("(")>0:
matched(str1)
return True
else:
return False
else:
return False
matched(str1)
parenthesis_String = input("Enter your parenthesis string")
parenthesis_List = []
for p in parenthesis_String:
parenthesis_List.append(p)
print(parenthesis_List)
if len(parenthesis_List)%2 != 0:
print("Not Balanced Wrong number of input")
for p1 in parenthesis_List:
last_parenthesis = parenthesis_List.pop()
print(last_parenthesis)
if (p1 == '{' and last_parenthesis == '}' or p1 == '[' and last_parenthesis == ']' or p1 == '(' and last_parenthesis == ')'):
print("Balanced")
else:
print("Not balanced")
A little different one.
expression = '{(){({)}}'
brackets = '[](){}'
stack = []
balanced = False
for e in expression:
if e in brackets and stack: # Popping from the stack if it is closing bracket
if stack [-1] == brackets[brackets.index(e)-1]:
stack.pop()
balanced = True
continue # it will go to the new iteration skipping the next if below
if e in brackets: # Push to stack if new bracket in the expression
stack .append(e)
balanced = False
balanced = 'Balanced' if balanced and not stack else 'Unbalanced'
print(balanced, stack)
just modified Henry Prickett-Morgan's code a little bit to handle it more sensibly, namely taking into account that the number of "(" matches that of ")" but string starts with ")" or ends with "(" which are apparently not right.
def ValidParenthesis(s):
count = 0
if s[0] == ')' or s[-1] == '(':
return False
else:
for c in s:
if c == '(':
count += 1
elif c == ')':
count -= 1
else:
continue
return count == 0
The best way to understand this snippet is to follow along with all kind of scenarios.
in_data = ['{','[','(']
out_data = ['}',']',')']
def check_match(statements):
stack = []
for ch in statements:
if ch in in_data:
stack.append(ch)
if ch in out_data:
last = None
if stack:
last = stack.pop()
if last is '{' and ch is '}':
continue
elif last is '[' and ch is ']':
continue
elif last is '(' and ch is ')':
continue
else:
return False
if len(stack) > 0:
return False
else:
return True
print(check_match("{www[eee}ee)eee"))
print(check_match("(ee)(eee[eeew]www)"))
print(check_match("(ss(ss[{ss}]zs)zss)"))
print(check_match("([{[[]]}])"))
def matched(str):
braces = {"{": "}", "(": ")", "[": "]"}
stack = []
for c in str:
if c in braces.keys():
stack.append(c)
elif c in braces.values():
if not stack:
return False
last_brace = stack.pop()
if braces[last_brace] != c:
return False
if stack:
return False
return True
print(matched("()"))
>> True
print(matched("(}"))
>> False
print(matched("}{"))
>> False
print(matched("}"))
>> False
print(matched("{"))
>> False
print(matched("(ff{fgg} [gg]h)"))
>> True
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']',
determine if the input string is valid.
def isValid(s):
stack = []
for i in s:
if i in open_list:
stack.append(i)
elif i in close_list:
pos = close_list.index(i)
if open_list[pos] == stack[len(stack)-1]:
stack.pop()
else:
return False
if len(stack) == 0:
return True
else:
return False
print(isValid("{[(){}]}"))
s='{[]{()}}}{'
t=list(s)
cntc=0
cnts=0
cntp=0
cntc=min(t.count("{"),t.count("}"))
cnts=min(t.count("["),t.count("]"))
cntp=min(t.count("("),t.count(")"))
print(cntc+cnts+cntp)
for a balanced string, we can find an opening brace followed by it closing brace. if you do this basic check you could remove the checked substring and check the remaining string. At the end, if the string is not empty then it is not balanced.
def is_balanced(s: str) -> bool:
while any([x in s for x in ["", "", ""]]):
s=s.replace("{}", "").replace("[]","").replace("()","")
return s==""
def parenthesis_check(parenthesis):
chars = []
matches = {')':'(',']':'[','}':'{'}
for i in parenthesis:
if i in matches:
if chars.pop() != matches[i]:
return False
else:
chars.append(i)
return chars == []
foo1="()()())("
def bracket(foo1):
count = 0
for i in foo1:
if i == "(":
count += 1
else:
if count==0 and i ==")":
return False
count -= 1
if count == 0:
return True
else:
return False
bracket(foo1)
Although I'm not proposing a fix to your implementation, I suggest a cleaner and more pythonic version of the #kreld solution:
def check_parentheses(expr):
s = []
for c in expr:
if c in '(':
s.append(c)
elif c in ')':
if not len(s):
break
else:
s.pop()
else:
return not len(s)
return False
# test -----------------------------------------------------------------
test_expr = [')(', '(()', '())', '(', ')', '((', '))', '(()())', '(())',
'()', '()(())']
for i, t in enumerate(test_expr, 1):
print '%i\t%s\t%s' % (i, t, check_parentheses(t))
# output ---------------------------------------------------------------
1 )( False
2 (() False
3 ()) False
4 ( False
5 ) False
6 (( False
7 )) False
8 (()()) True
9 (()) True
10 () True
11 ()(()) True
The code takes in any combination of brackets and checks if they are balanced or not. If they are balanced it should output success; if they aren't balanced it should output the index (starting at index 1) where the brackets are not balanced.
Example:
Input: ())
Output: 3
\\
Input: ()
Output: Success
The code always displays "Success" regardless of it being balanced or not.
Instead i get this:
Input: ())
Output: Success
import sys
def Match(self, c):
if self == '[' and c == ']':
return True
if self == '{' and c == '}':
return True
if self == '(' and c == ')':
return True
else:
return False
if __name__ == "__main__":
text = sys.stdin.read()
char_code = 0
opening_brackets_stack = []
for i, next in enumerate(text):
if next == '(' or next == '[' or next == '{':
char_code += 1
opening_brackets_stack.append(next)
stack_pop = opening_brackets_stack.pop()
if next == ')' or next == ']' or next == '}':
char_code += 1
if not Match(stack_pop, next):
print(char_code)
else:
char_code += 1
print ('Success')
Your code is printing "Success" because you've told it that after it finishes it should always print success
if __name__ == "__main__":
# A bunch of stuff unrelated to program flow...
print ('Success')
You probably only want success if you've reached the end of your text with nothing in the queue.
if __name__ == "__main__":
text = sys.stdin.read()
char_code = 0
opening_brackets_stack = []
for i, next in enumerate(text):
if next == '(' or next == '[' or next == '{':
char_code += 1
opening_brackets_stack.append(next)
stack_pop = opening_brackets_stack.pop()
if next == ')' or next == ']' or next == '}':
char_code += 1
if not Match(stack_pop, next):
print(char_code)
else:
char_code += 1
if not opening_brackets_stack: # <-- new line
print ('Success')
Except this won't solve your problem either, since you've never properly checked if you have an unmatched closing bracket, only an unmatched opening bracket. Consider this, instead:
# this will let us check for an expected closing bracket more easily
opening_brackets = "([{"
closing_brackets = ")]}"
mapping = dict(zip(opening_brackets, closing_brackets))
stack = []
for i, ch in enumerate(text):
if ch in opening_brackets:
# throw the closing bracket on the stack
matching_closer = mapping[ch]
stack.append(matching_closer)
elif ch == stack[-1]:
# if the character closes the last-opened bracket
stack.pop() # pop it off
elif ch in closing_brackets:
# this is an unmatched closing bracket, making the brackets
# imbalanced in this expression
print("FAILED")
sys.exit(1) # closes the program immediately with a retcode of 1
else:
# not a bracket, continue as normal
# this is technically a NOP and everything from the `else` can be
# omitted, but I think this looks more obvious to the reader.
continue
if not stack: # empty stack means matched brackets!
print("SUCCESS")
else:
print("FAILED")
Code can contain any brackets from the set []{}(), where the opening brackets are [,{, and ( and the closing brackets corresponding to them are ],}, and ).
For convenience, the text editor should not only inform the user that there is an error in the usage of brackets, but also point to the exact place in the code with the problematic bracket. First priority is to find the first unmatched closing bracket which either doesn’t have an opening bracket before it, like ] in ](), or closes the wrong opening bracket, like } in ()[}. If there are no such mistakes, then it should find the first unmatched opening bracket without the corresponding closing bracket after it, like ( in {}([]. If there are no mistakes, text editor should inform the user that the usage of brackets is correct.
Apart from the brackets, code can contain big and small latin letters, digits and punctuation marks.
More formally, all brackets in the code should be divided into pairs of matching brackets, such that in each pair the opening bracket goes before the closing bracket, and for any two pairs of brackets either one of them is nested inside another one as in (foo[bar]) or they are separate as in f(a,b)-g[c]. The bracket [ corresponds to the bracket ], { corresponds to }, and ( corresponds to ).
# python3
from collections import namedtuple
Bracket = namedtuple("Bracket", ["char", "position"])
def are_matching(left, right):
return (left + right) in ["()", "[]", "{}"]
def find_mismatch(text):
opening_brackets_stack = []
mismatch_pos = None
for i, next in enumerate(text):
if next in "([{":
# Process opening bracket, write your code here
opening_brackets_stack.append(next)
if len(opening_brackets_stack) < 2:
mismatch_pos = Bracket(next, i + 1).position
if next in ")]}":
# Process closing bracket, write your code here
if len(opening_brackets_stack) == 0:
return Bracket(next, i + 1).position
top = opening_brackets_stack.pop()
if not are_matching(top, next):
return Bracket(next, i + 1).position
if len(opening_brackets_stack) == 0:
return "Success"
return mismatch_pos
def main():
text = input()
mismatch = find_mismatch(text)
# Printing answer, write your code here
print(mismatch)
if __name__ == "__main__":
main()
I have this code to check whether or not brackets are balanced within a piece of text in a string.
def balanced_brackets(text):
s = Stack()
for character in text:
if character == "(":
s.push(character)
elif character == ")":
if s.is_empty():
return False
else:
s.pop()
if character == "<":
s.push(character)
elif character == ">":
if s.is_empty():
return False
else:
s.pop()
if s.is_empty():
return True
else:
return False
For the following tests:
print(balanced_brackets('(<x)>(())()'))
print(balanced_brackets('x<y)(>z'))
print(balanced_brackets('<(x)<y>)z'))
The following outputs should all print
False
but they print
True
for some reason. I need help on determining why this is. Thank you.
I would probably do it this way using a dictionary to map which closing bracket belongs to which opening bracket. This way you can simplify the code to only one if/elif block, instead of one for each bracket type. You can easily add more brackets to the dictionary as well.
# Map Closing to Opening
BRACKETS = {
')' : '(',
'>' : '<'
}
def balanced_brackets(text):
s = Stack()
for c in text:
# if opening bracket
if c in BRACKETS.values():
s.push(c)
# if closing bracket
elif c in BRACKETS:
if s.is_empty():
return False
# if opening bracket doesn't match closing bracket popped from Stack
elif BRACKETS[c] != s.pop():
return False
return s.is_empty()
# Output
>>> balanced_brackets('(<x)>(())()')
False
>>> balanced_brackets('x<y)(>z')
False
>>> balanced_brackets('<(x)<y>)z')
False
>>> balanced_brackets('<((<<hello>>))>')
True
>>> balanced_brackets('<')
False
I am trying to split some text. Basically I want to separate level-1 brackets, like "('1','a',NULL),(2,'b')" => ["('1','a',NULL)", "(2,'b')]", but I need to be aware of possible quoted strings inside. It needs to at least satisfy the following py.tests:
from splitter import split_text
def test_normal():
assert split_text("('1'),('2')") == ["('1')", "('2')"]
assert split_text("(1),(2),(3)") == ["(1)", "(2)", "(3)"]
def test_complex():
assert split_text("('1','a'),('2','b')") == ["('1','a')", "('2','b')"]
assert split_text("('1','a',NULL),(2,'b')") == ["('1','a',NULL)", "(2,'b')"]
def test_apostrophe():
assert split_text("('\\'1','a'),('2','b')") == ["('\\'1','a')", "('2','b')"]
def test_coma_in_string():
assert split_text("('1','a,c'),('2','b')") == ["('1','a,c')", "('2','b')"]
def test_bracket_in_string():
assert split_text("('1','a)c'),('2','b')") == ["('1','a)c')", "('2','b')"]
def test_bracket_and_coma_in_string():
assert split_text("('1','a),(c'),('2','b')") == ["('1','a),(c')", "('2','b')"]
def test_bracket_and_coma_in_string_apostrophe():
assert split_text("('1','a\\'),(c'),('2','b')") == ["('1','a\\'),(c')", "('2','b')"]
I have tried the following:
1) Regular expressions
This looks like the best solution, but unfortunately I did not come up with anything satisfying all tests.
My best try is:
def split_text(text):
return re.split('(?<=\)),(?=\()', text)
But obviously, that is rather simplistic and fails test_bracket_and_coma_in_string and test_bracket_and_coma_in_string_apostrophe.
2) Finite-state-machine-like solution
I tried to code the FSM myself:
OUTSIDE, IN_BRACKETS, IN_STRING, AFTER_BACKSLASH = range(4)
def split_text(text):
state = OUTSIDE
read = []
result = []
for character in text:
if state == OUTSIDE:
if character == ',':
result.append(''.join(read))
read = []
elif character == '(':
read.append(character)
state = IN_BRACKETS
else:
read.append(character)
elif state == IN_BRACKETS:
read.append(character)
if character == ')':
state = OUTSIDE
elif character == "'":
state = IN_STRING
elif state == IN_STRING:
read.append(character)
if character == "'":
state = IN_BRACKETS
elif character == '\\':
state = AFTER_BACKSLASH
elif state == AFTER_BACKSLASH:
read.append(character)
state = IN_STRING
result.append(''.join(read)) # The rest of string
return result
It works, passes all tests, but is very slow.
3) pyparsing
from pyparsing import QuotedString, ZeroOrMore, Literal, Group, Suppress, Word, nums
null_value = Literal('NULL')
number_value = Word(nums)
string_value = QuotedString("'", escChar='\\', unquoteResults=False)
value = null_value | number_value | string_value
one_bracket = Group(Literal('(') + value + ZeroOrMore(Literal(',') + value) + Literal(')'))
all_brackets = one_bracket + ZeroOrMore(Suppress(',') + one_bracket)
def split_text(text):
parse_result = all_brackets.parseString(text)
return [''.join(a) for a in parse_result]
Also passes all tests, but surprisingly it is even slower than solution #2.
Any ideas how to make the solution fast and robust? I have this feeling that I am missing something obvious.
One way would be to use the newer regex module which supports the (*SKIP)(*FAIL) functionality:
import regex as re
def split_text(text):
rx = r"""'.*?(?<!\\)'(*SKIP)(*FAIL)|(?<=\)),(?=\()"""
return re.split(rx, text)
Broken down it says:
'.*?(?<!\\)' # look for a single quote up to a new single quote
# that MUST NOT be escaped (thus the neg. lookbehind)
(*SKIP)(*FAIL)| # these parts shall fail
(?<=\)),(?=\() # your initial pattern with a positive lookbehind/ahead
This succeeds on all your examples.
I cooked this and it works on given tests.
tests = ["('1'),('2')",
"(1),(2),(3)",
"('1','a'),('2','b')",
"('1','a',NULL),(2,'b')",
"('\\'1','a'),('2','b')",
"('1','a,c'),('2','b')",
"('1','a)c'),('2','b')",
"('1','a),(c'),('2','b')",
"('1','a\\'),(c'),('2','b')"]
for text in tests:
tmp = ''
res = []
bracket = 0
quote = False
for idx,i in enumerate(text):
if i=="'":
if text[idx-1]!='\\':
quote = not quote
tmp += i
elif quote:
tmp += i
elif i==',':
if bracket: tmp += i
else: pass
else:
if i=='(': bracket += 1
elif i==')': bracket -= 1
if bracket: tmp += i
else:
tmp += i
res.append(tmp)
tmp = ''
print res
Output:
["('1')", "('2')"]
['(1)', '(2)', '(3)']
["('1','a')", "('2','b')"]
["('1','a',NULL)", "(2,'b')"]
["('\\'1','a')", "('2','b')"]
["('1','a,c')", "('2','b')"]
["('1','a)c')", "('2','b')"]
["('1','a),(c')", "('2','b')"]
["('1','a\\'),(c')", "('2','b')"]
The code has room for improvement, and edits are welcome. :)
This is the regular expression which seems to work and passes all the tests. Running it on real data it is about 6x faster than finite state machine implemented in Python.
PATTERN = re.compile(
r"""
\( # Opening bracket
(?:
# String
(?:'(?:
(?:\\')|[^'] # Either escaped apostrophe, or other character
)*'
)
|
# or other literal not containing right bracket
[^')]
)
(?:, # Zero or more of them separated with comma following the first one
# String
(?:'(?:
(?:\\')|[^'] # Either escaped apostrophe, or other character
)*'
)
|
# or other literal
[^')]
)*
\) # Closing bracket
""",
re.VERBOSE)
def split_text(text):
return PATTERN.findall(text)
Im type a program that see if a equation with brackets are balanced, like same amount of left brackets to right. I need to do it with "(",")","[","]","{","}". this is what I have so far:
# [import statements]
from stack_array import Stack
# [constants]
def is_balanced(symbolstring):
s = Stack()
balanced = True
index = 0
while index < len(symbolstring) and balanced:
symbol = symbolstring[index]
if symbol == "(" and "[" and"{" :
s.push(symbol)
else:
if s.is_empty():
balanced = False
else:
s.pop()
index = index + 1
if balanced and s.is_empty():
return (True)
else:
return(False)
and I need to see if I can get an equation like print(is_balanced("([5x+(2/4(5))])")
to work.
Use in to test if a variable has one of several values.
if symbol in ['(', '[', '{']:
When you pop an item off the stack, you'll want to check that the open symbol matches the close symbol.
if s.is_empty():
balanced = False
else:
opener = s.pop()
if opener == '(' and symbol != ')': balanced = False
if opener == '[' and symbol != ']': balanced = False
if opener == '{' and symbol != '}': balanced = False