Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Feb 10th, 2012  |  syntax: None  |  size: 0.80 KB  |  hits: 67  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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"