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
I have a sequence print(lcp(["flower","flow","flight", "dog"])) which should return fl. Currently I can get it to return flowfl.
I can locate the instances where o or w should be removed, and tried different approaches to remove them. However they seem to hit syntax issue, which I cannot seem to resolve by myself.
I would very much appreciate a little guidance to either have the tools to remedy this issue my self, or learn from a working proposed solution.
def lcp(strs):
if not isinstance(strs, list) or len(strs) == 0:
return ""
if len(strs) == 1:
return strs[0]
original = strs[0]
original_max = len(original)
result = ""
for _, word in enumerate(strs[1:],1):
current_max = len(word)
i = 0
while i < current_max and i < original_max:
copy = "".join(result)
if len(copy) and copy[i-1] not in word:
# result = result.replace(copy[i-1], "")
# result = copy[:i-1]
print(copy[i-1], copy, result.index(copy[i-1]), i, word)
if word[i] == original[i]:
result += word[i]
i += 1
return result
print(lcp(["flower","flow","flight", "dog"])) # returns flowfl should be fl
print(lcp(["dog","car"])) # works
print(lcp(["dog","racecar","car"])) # works
print(lcp([])) # works
print(lcp(["one"])) # works
I worked on an alternative which does not be solve removing inside the same loop, adding a counter at the end. However my instincts suggest it can be solved within the for and while loops without increasing code bloat.
if len(result) > 1:
counter = {char: result.count(char) for char in result}
print(counter)
I have solved this using the below approach.
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
N = len(strs)
if N == 1:
return strs[0]
len_of_small_str, small_str = self.get_min_str(strs)
ans = ""
for i in range(len_of_small_str):
ch = small_str[i]
is_qualified = True
for j in range(N):
if strs[j][i] != ch:
is_qualified = False
break
if is_qualified:
ans += ch
else:
break
return ans
def get_min_str(self, A):
min_len = len(A[0])
s = A[0]
for i in range(1, len(A)):
if len(A[i]) < min_len:
min_len = len(A[i])
s = A[i]
return min_len, s
Returns the longest prefix that the set of words have in common.
def lcp(strs):
if len(strs) == 0:
return ""
result = strs[0]
for word in strs[1:]:
for i, (l1, l2) in enumerate(zip(result, word)):
if l1 != l2:
result = result[:i]
break
else:
result = result[:i+1]
return result
Results:
>>> print(lcp(["flower","flow","flight"]))
fl
>>> print(lcp(["flower","flow","flight", "dog"]))
>>> print(lcp(["dog","car"]))
>>> print(lcp(["dog","racecar","car"]))
>>> print(lcp([]))
>>> print(lcp(["one"]))
one
>>> print(lcp(["one", "one"]))
one
You might need to rephrase your goal.
By your description you don't want the longest common prefix, but the prefix that the most words have in common with the first one.
One of your issues is that your tests only test one real case and four edgecases. Make some more real examples.
Here's my proposition: I mostly added the elif to check if we already have a difference on the first letter to then discard the entry.
It also overwrites the original to rebuild the string based on the common prefix with the next word (if there are any)
def lcp(strs):
if not isinstance(strs, list) or len(strs) == 0:
return ""
if len(strs) == 1:
return strs[0]
original = strs[0]
result = ""
for word in strs[1:]:
i = 0
while i < len(word) and i < len(original) :
if word[i] == original[i]:
result += word[i]
elif i == 0:
result = original
break
i += 1
original = result
result = ""
return original
print(lcp(["flower","flow","flight", "dog"])) # fl
print(lcp(["shift", "shill", "hunter", "shame"])) # sh
print(lcp(["dog","car"])) # dog
print(lcp(["dog","racecar","car"])) # dog
print(lcp(["dog","racecar","dodge"])) # do
print(lcp([])) # [nothing]
print(lcp(["one"])) # one
Modify the chkBracketsBal function so that the caller can supply the brackets to match as arguments to this function. The second argument should be a list of beginning brackets, and the third argument should be a list of ending brackets. The pairs of brackets at each position in the two lists; that is, position 0 in the two lists might have [ and ], respectively. You should be able to modify the code for the function so that it does not reference any literal bracket symbols, but just uses the list arguments. (Hint: The method index returns the position of an item in a list.)
begList = ["[","{","("]
endList = ["]","}",")"]
def chkBracketsBal_v2(myStr):
stk = LinkedStack()
for i in myStr:
if i in begList:
stack.append(i)
elif i in endList:
pos = endList.index(i)
if ((len(stack) > 0) and
(begList[pos] == stack[len(stack)-1])):
stack.pop()
else:
return "Unbalanced"
if len(stack) == 0:
return "Balanced"
else:
return "Unbalanced"
string = "[]{}"
print ("Beginning List: " ,string[1], string [3] )
print ("Ending List: ", string[0], string[2])
print("Exp: ",string,"-", check(string))
This is my answer, I don't know how to print the value of the beglist which are being used in the string
Append them to a list while parsing the string and return the list along with the result string like this.
begList = ["[","{","("]
endList = ["]","}",")"]
def chkBracketsBal_v2(myStr):
stack = LinkedStack()
open_brackets = []
for i in myStr:
if i in begList:
stack.append(i)
open_brackets.append(i)
elif i in endList:
pos = endList.index(i)
if ((len(stack) > 0) and
(begList[pos] == stack[len(stack)-1])):
stack.pop()
else:
return "Unbalanced", open_brackets
if len(stack) == 0:
return "Balanced", open_brackets
else:
return "Unbalanced", open_brackets
string = "[]{}"
print ("Beginning List: " ,string[1], string [3] )
print ("Ending List: ", string[0], string[2])
result, blist = chkBracketsBal_v2(string)
print("Exp: ",string,"-", result)
print(blist)
I have a string like '....(((...((...' for which I have to generate another string 'ss(4)h5(3)ss(3)h2(2)ss(3)'.
'.' corresponds to 'ss' and the number of continous '.' is in the bracket.
'(' corresponds to 'h5' and the number of continuos '(' is in the bracket.
Currently I'm able to get the output 'ss(4)h5(3)ss(3)' and my code ignores the last two character sequences.
This is what I have done so far
def main():
stringInput = raw_input("Enter the string:")
ssCount = 0
h5Count = 0
finalString = ""
ssString = ""
h5String = ""
ssCont = True
h5Cont = True
for i in range(0, len(stringInput), 1):
if stringInput[i] == ".":
h5Cont = False
if ssCont:
ssCount = ssCount + 1
ssString = "ss(" + str(ssCount) + ")"
ssCont = True
else:
finalString = finalString + ssString
ssCont = True
ssCount = 1
elif stringInput[i] == "(":
ssCont = False
if h5Cont:
h5Count = h5Count + 1
h5String = "h5(" + str(h5Count) + ")"
h5Cont = True
else:
finalString = finalString + h5String
h5Cont = True
h5Count = 1
print finalString
main()
How to modify the code to get the desired output?
I don’t know about modifying your existing code, but to me this can be done very succinctly and pythonically using itertools.groupby. Note that I’m not sure if the 'h2' in your expected output is a typo or if it should be 'h5', which I’m assuming.
from itertools import chain, groupby
string = '....(((...((...'
def character_count(S, labels): # this allows you to customize the labels you want to use
for K, G in groupby(S):
yield labels[K], '(', str(sum(1 for c in G)), ')' # sum() counts the number of items in the iterator G
output = ''.join(chain.from_iterable(character_count(string, {'.': 'ss', '(': 'h5'}))) # joins the components into a single string
print(output)
# >>> ss(4)h5(3)ss(3)h5(2)ss(3)
#Kelvin 's answer is great, however if you want to define a function yourself, you could do it like this:
def h5ss(x):
names = {".": "ss", "(": "h5"}
count = 0
current = None
out = ""
for i in x:
if i == current:
count += 1
else:
if current is not None:
out += "{}({})".format(names[current], count)
count = 1
current = i
if current is not None:
out += "{}({})".format(names[current], count)
return out