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')