0

I am trying to make a program which takes an alphabetized string and looks for a particular character within that string by breaking the string up into 2 parts, to find the character that is in the middle, either on the half less than the middle, or on the half greater than the middle character.

I tried the following code and got the error :

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

I am trying to cut the string in half and return the correct half to the program to look for the character again. For example, if the character was smaller than the middle character we should throw out the right half, return the left half, and find the middle of that. Below is the code sample where the program hangs: can someone tell me why? 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')
radiovisual
  • 6,298
  • 1
  • 26
  • 41

2 Answers2

1

Why not simply do this?

char in aStr

There are a few issues with the code you've posted, e.g isInF(char, aStr[middle]), but isInF only takes one argument. However, it looks like the problem is with :

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

middle returns a character from the middle of the aStr and is a string. You then attempt to add 1 to it, which obviously fails. I suspect you mean middle to be the location of the middle of the string rather than the character at the middle, so that line should read:

middle = (length-1) / 2
aquavitae
  • 17,414
  • 11
  • 63
  • 106
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')
lqhcpsgbl
  • 3,694
  • 3
  • 21
  • 30