![](/img/trans.png)
[英]Balanced Parenthesis challenge: Checking for string that contains no parenthesis
[英]Recursively checking for balanced string in Python
我已經堅持了很長一段時間,我無法提出遞歸案例,特別是我不明白如何拆分列表以檢查其是否平衡。 如果有人可以幫助我,我將不勝感激。
def balanced_str(s):
"""
Return whether string s is balanced or not. A balanced string is one where
the string contains no parentheses
>>> balanced_str('a')
True
>>> balanced_str('abbcsi')
True
>>> balanced_str('ak)')
False
>>> balanced_str('hah(dh')
False
>>> balanced_str('()')
True
>>> balanced_str('(hghghgh)')
True
>>> balanced_str('((a))')
True
>>> balanced_str('((hahsh))')
True
>>> balanced_str('(gfjf)h)')
False
>>> balanced_str('(hhg)(hfhg)')
True
"""
if '(' not in s and ')' not in s:
return True
elif '(' in s and ')' not in s or ')' in s and '(' not in s:
return False
else:
if s[0] == '(' and s[len(s) - 1] == ')':
return balanced_str(s[1:len(s) - 2])
一種簡單的迭代方法可能是創建一個小型詞法分析器。 當出現左括號(
出現,如果出現右括號,則減少計數器)
時,它將增加計數器o
。 如果同時搜索字符串計數器o
負,或者在循環結束時計數器o
不為零,則測試失敗:
def balanced_str(s):
o = 0
for c in s:
if c == ')':
if o <= 0:
# this only happens if there are more closing
# parentheses then opening parentheses.
return False
o -= 1
elif c == '(':
o += 1
# all parentheses should be closed
return o == 0
對於您的測試用例,這種方法有效:
>>> balanced_str('a')
True
>>> balanced_str('abbcsi')
True
>>> balanced_str('ak)')
False
>>> balanced_str('hah(dh')
False
>>> balanced_str('()')
True
>>> balanced_str('(hghghgh)')
True
>>> balanced_str('((a))')
True
>>> balanced_str('((hahsh))')
True
>>> balanced_str('(gfjf)h)')
False
>>> balanced_str('(hug)(hfhg)')
True
對於遞歸方法,您可以創建一個帶有更多參數的小型輔助函數(即我們目前看到的括號數量)。 在這種方法下面,您可以看到如何在沒有輔助函數的情況下通過使用global
def balanced_str(s):
"""
Return whether string s is balanced or not. A balanced string is one where
the string contains no parentheses
>>> balanced_str('a')
True
>>> balanced_str('abbcsi')
True
>>> balanced_str('ak)')
False
>>> balanced_str('hah(dh')
False
>>> balanced_str('()')
True
>>> balanced_str('(hghghgh)')
True
>>> balanced_str('((a))')
True
>>> balanced_str('((hahsh))')
True
>>> balanced_str('(gfjf)h)')
False
>>> balanced_str('(hhg)(hfhg)')
True
"""
return helper(s,0)
def helper(s, numP):
if len(s)==0: return numP==0
if numP < 0: return False
if s[0] == "(": return helper(s[1:], numP+1)
elif s[0] == ")": return helper(s[1:], numP-1)
return helper(s[1:], numP)
沒有幫手:
def balanced_str(s):
"""
Return whether string s is balanced or not. A balanced string is one where
the string contains no parentheses
>>> balanced_str('a')
True
>>> balanced_str('abbcsi')
True
>>> balanced_str('ak)')
False
>>> balanced_str('hah(dh')
False
>>> balanced_str('()')
True
>>> balanced_str('(hghghgh)')
True
>>> balanced_str('((a))')
True
>>> balanced_str('((hahsh))')
True
>>> balanced_str('(gfjf)h)')
False
>>> balanced_str('(hhg)(hfhg)')
True
"""
try:
numP
except NameError:
numP = 0
global numP
if len(s)==0: return numP==0
if numP < 0: return False
if s[0] == "(":
numP += 1
return balanced_str(s[1:])
elif s[0] == ")":
numP -= 1
return balanced_str(s[1:])
return balanced_str(s[1:])
這是我的候選解決方案:
def balanced(iterable, semaphore=0):
if semaphore < 0 or len(iterable) == 0:
return semaphore == 0
first, *rest = iterable
return balanced(rest, semaphore + { "(": 1, ")": -1 }.get(first, 0))
我已將balanced_str()
重命名為balanced()
因為如果它寫得正確,它應該處理字符串或字符列表(即iterables ):
>>> balanced('a')
True
>>> balanced(['a', 'b', 'b', 'c', 's', 'i'])
True
>>> balanced('ak)')
False
>>> balanced(['h', 'a', 'h', '(', 'd', 'h'])
False
>>> balanced('()')
True
>>> balanced(['(', 'h', 'g', 'h', 'g', 'h', 'g', 'h', ')'])
True
>>> balanced('((a))')
True
>>> balanced(['(', '(', 'h', 'a', 'h', 's', 'h', ')', ')'])
True
>>> balanced('(gfjf)h)')
False
>>> balanced(['(', 'h', 'h', 'g', ')', '(', 'h', 'f', 'h', 'g', ')'])
True
我相信其他提議的解決方案也是如此,而不僅僅是我的。
這是一種遞歸方法。 我認為這非常簡潔直觀
def is_balanced(s, c=0):
if len(s) == 0:
if c == 0:
return True
else:
return False
elif s[0] == "(":
c = c + 1
return is_balanced(s[1 : len(s)], c)
elif s[0] == ")":
c = c - 1
return is_balanced(s[1 : len(s)], c)
else:
return is_balanced(s[1 : len(s)], c)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.