Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement