I have a list of lists which contain a set of postcodes and the suburbs that match with. I need to create a iterative binary search algorithm that asks the user to input the name of the suburb and then the program outputs the postcode the suburb belongs to. The list looks like this:
L = [['4000', 'Charlestown'], ['4001', 'Jamestown'], ['4002', 'Henrytown']]
So if I were to run a binary search on this and the user entered 'Jamestown', then the program would output '4001'. This is what I've done to create the binary search function:
suburb = input("Please enter the name of a suburb: ")
def binarySearch(L, target):
low = 0
high = len(L)-1
while low <= high:
mid = (low + high)//2
if L[mid] == target:
return mid
elif L[mid] > target:
high = mid-1
else:
low = mid+1
binarySearch(L, suburb)
However, when I run the program I get an error:
elif L[mid] > target:
TypeError: '>' not supported between instances of 'list' and 'str'
I'm not really sure how to resolve this problem, any help would be greatly appreciated. Thanks!