SHARE
TWEET

Untitled

a guest Feb 10th, 2012 69 Never
  1. def is_more(num):
  2.         uinput = ''
  3.         while(uinput!='n' and uinput != 'y'):
  4.                 uinput = raw_input("Is the number greater than " + str(num) + "? [y/n]\n")
  5.         if(uinput == 'y'):
  6.                 return True
  7.         return False
  8.  
  9. def binary_search(minnum, maxnum):
  10.         if(maxnum-minnum == 0):
  11.                 return maxnum
  12.         if(maxnum-minnum == 1):
  13.                 if(is_more(minnum)):
  14.                         return maxnum
  15.                 return minnum
  16.         nextnum = (maxnum + minnum)/2
  17.         if(is_more(nextnum)):
  18.                 return binary_search(nextnum+1, maxnum)
  19.         else:
  20.                 return binary_search(minnum, nextnum)
  21.  
  22. print "Think of a number between 0 and 100...\n"
  23. print "You were thinking of " + str(binary_search(0, 100)) + "\n"
RAW Paste Data
Top