2016-07-14 48 views
1

我一直堅持這個很長一段時間,我不能拿出遞歸的情況下,特別是我不明白如何分割列表來檢查它是否平衡。 如果有人能幫助我,我會非常感激。遞歸檢查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]) 
+2

我想你的意思是沒有不匹配的圓括號,不是嗎? –

+0

@ joelgoldstick是 – jia

回答

1

一個簡單的迭代方法可能是創建一個小的詞法分析器。當出現一個開頭括號(時,它會增加一個計數器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 
+1

雖然這個工作,OP要求遞歸算法 – Brian

+0

我的不好!我誤解了這個問題。 – keksnicoh

2

對於遞歸方法,您可以創建一個小幫助函數,它需要更多的參數(即迄今爲止我們已經看到的parens的數量)。下面這個方法,你可以看到你如何能做到這沒有一個輔助功能通過使用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:]) 
0

這裏是我的候選解決方案:

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(),因爲如果它寫得不錯,它應該處理字符串或li字符(即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 

我相信這是其他提出的解決方案也是如此,不只是我的。