Files

35 lines
1.1 KiB
Python

# HumanEval/119
# Loki Mode Multi-Agent Solution
# Attempts: 1
# Passed: True
def match_parens(lst):
'''
You are given a list of two strings, both strings consist of open
parentheses '(' or close parentheses ')' only.
Your job is to check if it is possible to concatenate the two strings in
some order, that the resulting string will be good.
A string S is considered to be good if and only if all parentheses in S
are balanced. For example: the string '(())()' is good, while the string
'())' is not.
Return 'Yes' if there's a way to make a good string, and return 'No' otherwise.
Examples:
match_parens(['()(', ')']) == 'Yes'
match_parens([')', ')']) == 'No'
'''
def is_balanced(s):
balance = 0
for char in s:
if char == '(':
balance += 1
else:
balance -= 1
if balance < 0:
return False
return balance == 0
# Try both concatenation orders
if is_balanced(lst[0] + lst[1]) or is_balanced(lst[1] + lst[0]):
return 'Yes'
return 'No'