Advertisement
Guest User

Untitled

a guest
Feb 10th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  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"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement