Home > Net >  Binary Search Python- by product list barcode
Binary Search Python- by product list barcode

Time:06-16

i need to for binary search find a Product, we used a method to query the database using the product Barcode as the primary key value. but keeps giving me this error.

binary search code

binary search being called looking for product list by barcode

def binary_search(list_of_products):
bar_code = []
low = 0
high = len(list_of_products) - 1
mid = 0

while low <= high:

    mid = (high   low) // 2

    # If x is greater, ignore left half
    if (list_of_products[mid]).get_barcode() < bar_code:
        low = mid   1

    # If x is smaller, ignore right half
    elif (list_of_products[mid]).get_barcode() > bar_code:
        high = mid - 1

    # means x is present at mid
    else:
        return mid

# If we reach here, then the element was not present
return -1

CodePudding user response:

You are comparing binary search value (list_of_products[mid]).get_barcode() and empty list bar_code

You need a target such as target_product.get_barcode() to be compared with (list_of_products[mid]).get_barcode().

def binary_search(list_of_products, target_product):
    bar_code = target_product.get_barcode()  # Need to define target
    low = 0
    high = len(list_of_products) - 1
    mid = 0
    
    while low <= high:
    
        mid = (high   low) // 2
    
        # If x is greater, ignore left half
        if (list_of_products[mid]).get_barcode() < bar_code:
            low = mid   1
    
        # If x is smaller, ignore right half
        elif (list_of_products[mid]).get_barcode() > bar_code:
            high = mid - 1
    
        # means x is present at mid
        else:
            return mid
    
    # If we reach here, then the element was not present
    return -1

binary_search(list_of_products, target_product)  # add target_product when running

CodePudding user response:

Assume the 'list of product' is a list of 'product' object sorted by its attribute 'barcode'. Since that is what you are going to compare. And all products have a barcode, that is no null value.

Now what you are actually do is searching the target value (your target product's barcode) on a sorted array (the list of barcode of your products.)

def binarySearch(list_of_product, bar_code): # bar_code is your target's barcode
    left = 0
    right = len(list_of_product) - 1

    while left <= right:
        mid = left   (right - left) // 2 # Avoid overflow

        if list_of_product[mid].get_barcode() == bar_code:
            return mid
        elif list_of_product[mid].get_barcode() < bar_code:
            left = mid   1
        else: right = mid - 1
    
    return -1
    

Note the return value is the index of that specific product in the list. If the none of the product in the list contain that barcode, then the return value is -1.

There are also rooms for improvements. For example instead of passing a list of product objects, you could opt for passing just barcode as list, thus avoid multiple get_barcode() function call.

  • Related