2016-07-14 47 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 

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