0

我正在尝试制作一个程序,该程序采用按字母顺序排列的字符串并通过将字符串分成两部分来查找该字符串中的特定字符,以找到中间的字符,或者比中间少一半,或者在大于中间字符的一半上。

我尝试了以下代码并得到了错误:

cannot concatenate 'str' and 'int' objects.|

我正在尝试将字符串切成两半并将正确的一半返回给程序以再次查找字符。例如,如果字符小于中间字符,我们应该扔掉右半部分,返回左半部分,然后找到中间部分。下面是程序挂起的代码示例:有人可以告诉我为什么吗?return isInF(char, aStr[:middle])

def isIn(char, aStr):

    def toChars(aStr):
        s = aStr.lower()
        ans = ''
        for c in s:
            if c in 'abcdefghijklmnopqrstuvwxyz':
                ans = ans + c
        return ans

    def isInF(aStr):
        if len(aStr) == 0:
            return False
        elif len(aStr) == 1:
            if char == aStr:
                return True
            else:
                return False
        elif len(aStr)> 1:
            length = len(aStr)
            middle = aStr[((length-1)/2)]
            if char == middle:
                return True
            elif char < middle:
                return isInF(char, aStr[:middle])
            elif char > middle:
                return isInF(char, aStr[middle+1:])
    return isInF(toChars(aStr))


isIn ('c', 'aaaabbbbc')
4

2 回答 2

1

为什么不简单地这样做呢?

char in aStr

您发布的代码存在一些问题,例如isInF(char, aStr[middle]),但isInF只接受一个参数。但是,看起来问题出在:

...
middle = aStr[((length-1)/2)]
...
return isInF(char, aStr[middle+1:])

middle从中间返回一个字符aStr并且是一个字符串。然后您尝试添加1它,这显然失败了。我怀疑您的意思middle是字符串中间的位置,而不是中间的字符,因此该行应为:

middle = (length-1) / 2
于 2015-01-30T11:47:18.397 回答
0

Here is a works version of your code:

def isIn(char, aStr):

    def toChars(aStr):
        s = aStr.lower()
        ans = ''
        for c in s:
            if c in 'abcdefghijklmnopqrstuvwxyz':
                ans = ans + c
        return ans

    def isInF(aStr):
        length = len(aStr) # call len() once will ok 

        if length == 0:
            return False
        elif length == 1:
            if char == aStr:
                return True
            else:
                return False
        elif length > 1:

            middle = (length-1)/2
            try:

              if char == aStr[middle]:
                  return True
              elif char < aStr[middle]:
                  return isIn(char, aStr[:middle])
              elif char > aStr[middle]:
                  # print aStr[:middle]
                  return isIn(char, aStr[middle+1:])
            except Exception, error:
                print error

    return isInF(toChars(aStr))


print isIn ('c', 'aaaabbbbc')

Your bug is here

middle = aStr[((length-1)/2)]

middle is a char cause you index the aStr with middle, but here:

isInF(char, aStr[:middle])

you index aStr with middle again but it's a char, so you get error.

Here is my rework version of the code:

import string

def isIn(char, aStr):

    def toChars(aStr):
        lower_str = aStr.lower()
        return ''.join([i for i in aStr if i in string.lowercase])

    def isInF(aStr):
        length = len(aStr) # call len() once will ok 

        if length == 0:
            return False
        elif length == 1:
            return True if char == aStr else False
        elif length > 1:

            middle = (length-1)/2
            if char == aStr[middle]:
                return True
            elif char < aStr[middle]:
                return isIn(char, aStr[:middle])
            elif char > aStr[middle]:
                return isIn(char, aStr[middle+1:])

    return isInF(toChars(aStr))

print isIn('c','abcdef')
print isIn('x','abcdef')
于 2015-01-30T12:03:02.820 回答