That is what I thought as well. The following is the coding given in the solution sheet for question 6 of Quiz 2. Integrating this coding with the above coding doesn't result in the expected solution to the problem. e.g.: if the statement "findNumber(50)" is given, the answer returned is 49, not 4, as it should be. Is the solution given in the marking scheme wrong? or is my coding wrong? Can you please explain this? Thanks
def findNumber(maxVal):
""" Assumes that maxVal is a positive integer.
Returns a number, num, such that
cmpGuess(num) == 0 """
s = range(0, maxVal)
return bsearch(s, 0, len(s) -1)
def bsearch(s, first, last):
if (last -first) < 2:
if cmpGuess(s[first]) == 0:
return first
else:
return last
mid = first + (last -first)/2
if cmpGuess(s[mid]) == 0:
return s[mid]
if cmpGuess(s[mid]) == -1:
return bsearch(s, first, mid -1)
return bsearch(s, mid + 1, last)