I am trying to create a small program that uses binary search algorithm to have the computer itself guess a number given a set of arguments.
The arguments the function takes in are how many 'tries' it has to guess and the values that it can guess between (for example, 5 tries to guess the number in between 0 and 10).
When I run my code, it only seems to run my else: statement, regardless of the arguments I pass in.
I am missing something in my "if" statements but I am stumped and can't figure out what I have done incorrectly.
Thank you so much for your time and help!
import random
def guess_random_number_binary(tries, start, stop):
rand_number = random.randint(start,stop)
num_list = range(start,stop)
lower_bound = start
upper_bound = len(str(stop)) - 1
while lower_bound <= upper_bound:
pivot = (lower_bound upper_bound) // 2
pivot_value = num_list[pivot]
if pivot_value == rand_number and tries > 0:
print("Found it! " str(rand_number))
return pivot
if pivot_value > rand_number and tries > 0:
upper_bound = pivot - 1
tries -= 1
print ("incorrect guess" str(tries) " Remaining")
else:
lower_bound = pivot 1
print("Out of tries")
return
guess_random_number_binary(5, 0 ,10)
UPDATE:
import random
def guess_random_number_binary(tries, start, stop):
rand_number = random.randint(start,stop)
num_list = []
lower_bound = start
upper_bound = stop
num_list = range(start,stop 1)
while lower_bound <= upper_bound:
pivot = (lower_bound upper_bound) // 2
pivot_value = num_list[pivot]
if tries > 0:
if pivot_value == rand_number:
print("Found it! " str(rand_number))
return pivot
elif pivot_value > rand_number:
upper_bound = pivot - 1
tries -= 1
print ("Guessed " str(pivot) " incorrectly \n" str(tries) " Tries remaining")
elif pivot_value < rand_number:
lower_bound = pivot 1
tries -= 1
print ("Guessed " str(pivot) " incorrectly \n" str(tries) " Tries remaining")
else:
print ("Ran out of tries!")
break
guess_random_number_binary(5, 20 ,30)
I have been trying to debug and even if my new code is over simplified, I hope that it is at least headed in the right direction.
I believe the main issue stands with how I am creating "num_list" as pointed out by an answer below. Receiving an IndexError, which makes sense in theory. However, I cannot seem to find an alternative for creating that list.
Once again, thank you.
CodePudding user response:
You're resetting the value of pivot
at the beginning of each loop, so
You passed in the value 10
for stop
. So in the line upper_bound = len(str(stop)) - 1
,
upper_bound = len(str(stop))-1 = len(str(10) = len ('10')-1 = 1.
Your while loop never runs, because lower_bound
is never less than upper_bound
.
You perhaps were at some point intending to do upper_bound = len(num_list)-1
and somehow wrote upper_bound = len(str(stop)) - 1
instead. However, even that would not have been correct; the length of range(start,stop)
is stop-start, not stop-start 1 (range
is not inclusive of stop
). And using range
in the first place is unnecessarily confusing and would not work for larger start
values. For instance, suppose (start, stop) = (20, 30)
. Then pivot
will be 25. But the range
object will have only 10 elements, so num_range[pivot]
will return an error.