Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!
tweet

# Untitled

By: a guest on Feb 10th, 2012  |  syntax: None  |  size: 0.80 KB  |  views: 68  |  expires: Never
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"
clone this paste RAW Paste Data
Top