In lexicographic order, do numbers (i.e., "50") or alphabets (e.g., "a") come first if both are used?

Viewed 20

I'm new to python and trying to understand lexicographic. My codes is:

inventory=["10", "50", "100", "150", "200"]

SearchItem=input("Enter item: ")

LB=0
UB=len(inventory)-1
Found=False

while Found==False and LB <= UB:
    Mid=(LB + UB)//2
    if inventory[Mid]==SearchItem:
        print("Item found in the position: ", Mid + 1)
        Found=True
    elif SearchItem>inventory[Mid]:
        LB= Mid + 1
    else:
        UB= Mid - 1

if Found==False:
    print("Item doesn't exist.")

It's a code I wrote in which inventory is an array of 5 elements. We then input a particular item and then search the item using the binary search technique.

I'm asking this because my list:

inventory=["a", "10", "50", "100", "200"]

outputs as:

Enter item: a
Item doesn't exist.

when the module is run.

I changed my list to:

inventory=["10", "50", "100", "200", "a"]

which outputs as:

Enter item: a
Item found in the position:  5

How come the latter one works but the other one doesn't? I tried looking it up but the closest I got to find an answer was lexicographic order - but all of those are either only considering strings as alphabets in the list or strings as numerical values in the list.

0 Answers
Related