Tom
Tom

Reputation: 21

Binary Search to search a list

I have a code which first sorts the emails into alphabetical order and then attempts to use binary search to search a user inputted email from a list. However, I have been stuck on how to do this for so long and haven't found any solutions on the error I get and how to fix it. Here is my code

def BubbleSort(logindata):
    NoSwaps = 1
    N = len(logindata)
    logindata = list(logindata)
    while NoSwaps == 1:
        Count = 1
        NoSwaps = 0
        for Count in range(N-1):
            if logindata[Count] > logindata[Count+1]:
                temp = logindata[Count]
                logindata[Count] = logindata[Count+1]
                logindata[Count+1]=temp
                NoSwaps=1
    return tuple(logindata)

def BinarySearch(logindata,ItemSought):
    First=0
    Last=len(logindata)-1
    ItemFound = False
    SearchFailed = False
    while ItemFound == False or SearchFailed == False:
        Midpoint = (First + Last) // 2
        if logindata[Midpoint] == ItemSought:
            ItemFound = True
            print("Item Found")
            break

        elif logindata[Midpoint] > ItemSought:
            Last = Midpoint - 1
        else:
            First = Midpoint + 1

if __name__ == "__main__":
    logindata=["[email protected]","Password1"],["[email protected]","Password2"],["[email protected]","Password3"]
    logindata=BubbleSort(logindata)
    print(logindata)
    ItemSought=input("Enter username")
    BinarySearch(logindata,ItemSought)

The error I currently get is :

elif logindata[Midpoint] > ItemSought:
TypeError: unorderable types: list() > str()

Upvotes: 0

Views: 121

Answers (1)

NPE
NPE

Reputation: 500187

You're comparing a username/password pair (e.g. ["[email protected]","Password1"]) with a username (e.g. "[email protected]").

You need to extract the username from logindata[Midpoint] before comparing it to ItemSought.

Upvotes: 2

Related Questions