#Using Slicing ##def binary_search(array, searchterm): ## ## while len(array)!= 1: ## ## midpoint = len(array) // 2 ## ## if searchterm < array[midpoint]: ## ## array = array[:midpoint] ## else: ## ## array = array [midpoint:] ## return array #Using Markers (Without Slicing) ##def binary_search(array, searchterm): ## ## left = 0 ## ## right = len(array) - 1 ## ## while left <= right: ## ## midpoint = (left + right) // 2 ## ## if array[midpoint] < searchterm: ## ## left = midpoint + 1 ## else: ## ## right = midpoint - 1 ## ## return(array[midpoint]) #Recursive: def binary_search(array, searchterm): if len(array) == 1: return array[0] == searchterm mid = len(array) // 2 if searchterm < array[mid]: array = array [:mid] else: array = array[mid:] return binary_search(array, searchterm) file = open("townlist.txt","r") townlist = [] for line in file: town = line.strip() town = town.lower() townlist.append(town) sortarray = sorted(townlist) print(binary_search(sortarray, 'nuneaton'))