The function is never returning True
. I think it should return True
when char == m
, so you could delete it from the if-clause
(that is returning False
) and put it in another if
:
if char == m:
return True
elif aStr == '' or len(aStr) == 1:
return False
else:
...
Also, you are calling isIn
method which is not defined. I think you wanted to recursively call isitIn
.
After comparing char < m
and char > m
you should "bisect" the string, so don't do return isitIn(char, aStr[:-1])
nor return isIn(char, aStr[1:])
, instead pass (in the recursive call) the "half" of the string.
if char < m:
return isitIn(char, aStr[:len(aStr) // 2])
elif char > m:
return isitIn(char, aStr[len(aStr) // 2:])
Edit: Just in case, the code I've tried is:
def isitIn(char, aStr):
if aStr == '': # Check for empty string
return False
m = aStr[len(aStr) // 2]
if char == m:
return True
elif len(aStr) == 1:
return False
else:
if char < m:
return isitIn(char, aStr[:len(aStr) // 2])
elif char > m:
return isitIn(char, aStr[len(aStr) // 2:])
return isitIn(char, aStr)