
Untitled
By: a guest on
Feb 10th, 2012 | syntax:
None | size: 0.80 KB | hits: 67 | expires: Never
def is_more(num):
uinput = ''
while(uinput!='n' and uinput != 'y'):
uinput = raw_input("Is the number greater than " + str(num) + "? [y/n]\n")
if(uinput == 'y'):
return True
return False
def binary_search(minnum, maxnum):
if(maxnum-minnum == 0):
return maxnum
if(maxnum-minnum == 1):
if(is_more(minnum)):
return maxnum
return minnum
nextnum = (maxnum + minnum)/2
if(is_more(nextnum)):
return binary_search(nextnum+1, maxnum)
else:
return binary_search(minnum, nextnum)
print "Think of a number between 0 and 100...\n"
print "You were thinking of " + str(binary_search(0, 100)) + "\n"