def BinSearch(listo, val): #Naming of the function and setting the parameters.

if (len(listo) == 0): print val, “does not exist within your ordered list.” return #If the function below is repeated over and over to find val in listo, #the number of elements in listo will in a sense decrease with each loop #which means once every element has been checked, the number of elements #in listo will be equivalent to 0, therefore the length will be equal to 0. #Thus, if the length of listo is 0 and val is still not found, then this means #every element of listo has been checked and val has still not be found, thus #val must not be part of listo. mid = listolen(listo) / 2 #Determines the midpoint value of the list if val==mid: print val, “does exist within your ordered list.” return #If the midpoint value is that of the value the function is looking for, #then the search is complete and we can end the function, informing the user that #their chosen number is indeed part of their chosen list. if val

if val>mid: return BinSearch(listolen(listo)/2+1:, val) #If the midpoint value is less then the value we are looking for, then this will #replug in only the second half of the ordered list into the function, in other words, #it will input the only possible elements which could include our chosen value and repeat.listo = input(“Enter your ordered list in the form a, b, c, …: “)#Allows the user to enter their chosen ordered list and tells them which form to enter it in.if (len(listo) == 0): #Filters out if the empty set () is chosen.

print “The empty set contains no elements so it is not possible to search for a specific element within it.” exit(0) #No need to run the function if the chosen list is the empty set as it won’t contain any elements. #This informs the user and exits the code.val = input(“Enter the number you want to search your ordered list for: “)#Lets the user input which element they want to search their chosen list forBinSearch(listo, val)#Runs the function

x

Hi!

I'm Simon!

Would you like to get a custom essay? How about receiving a customized one?

Check it out