This code does a recursive bisection search for a character in a string.
When the print
statements are not commented out, it seems to work well with the recursion and bisection, but the if
statement that returns True
does not seem to fire.
def isIn(char, aStr):
'''
char: a single character
aStr: an alphabetized string
returns: True if char is in aStr; False otherwise
'''
b = sorted(aStr)
c = len(aStr)
# print("string b " + str(b))
# print("c " + str(c))
# print("element middle: " + str(b[round(c/2)]))
#print("char: " + str(char))
#print(str(char) == str(b[round(c/2)]))
if ((str(char) == str(b[round(c/2)]))): # this if statement does not seem to fire
return True
elif (c == 1 and char != str(b[round(c/2)])) or (c == 0 and char != "") :
return False
#print("false")
else:
#if str(char) == str(b[round(c/2)]):
# return True
# print("true")
if char > b[round(c/2)]:
isIn(char, b[round(c/2):c])
elif char < b[round(c/2)]:
isIn(char, b[0:round(c/2)])
else:
return False
#print('fales')