I have been tasked with creating a binary search over a list of words, I have come up with 2 implementations (and clearly haven't put in a case where the word isn't found yet but that's not an issue yet), however when the list is narrowed down to the word I am looking for, my function does not finish, instead it keeps running until maximum recursive depth is exceeded.
I put in print and it clearly shows the word at dasList[mid]
, and shows this over and over again until it finally gives up.
def _bisect2(dasList, word):
mid = int(len(dasList)/2)
if word.lower() > dasList[mid].lower():
return _bisect2(dasList[mid: len(dasList)], word)
if word.lower() < dasList[mid].lower():
return _bisect2(dasList[0: mid], word)
else:
return mid
this is being called by
print(_bisect2(fileList, input('Please type a word')))
I am using the Python 3.0 interpretor. Any suggestions?