简体   繁体   English

python 3.6 中的二进制搜索

[英]Binary search in python 3.6

I am writing a binary search function for a school assignment and while it works fine for my own tests, checking it in the upload submission portal returns that it fails the majority of hidden tests.我正在为学校作业编写二进制搜索 function,虽然它适用于我自己的测试,但在上传提交门户中检查它会返回它未通过大多数隐藏测试。 Is there anything obviously wrong with it, ie the assignment for left, right, middle being incorrect etc?有什么明显的问题吗,即左、右、中间的分配不正确等? (other than it being very crude) (除了它非常粗糙)

EDIT: added more details that were missing, yes the list in stolen_plates will be sorted.编辑:添加了更多丢失的详细信息,是的,被盗板中的列表将被排序。 The test I have at the bottom works fine for me but there must be something I am overlooking for it to fail the submission tests我在底部的测试对我来说很好,但一定有一些我忽略的东西无法通过提交测试

def binary_simple_plate_finder(stolen_plates, sighted_plates):

    result_list = []
    
    total_comparisons = 0
    for plate in sighted_plates:
        middle = (len(stolen_plates) - 1) // 2
        right = (len(stolen_plates) - 1)
        left = 0
        finished = False
        while not finished:
            if plate == stolen_plates[middle]:
                total_comparisons = total_comparisons + 1
                result_list.append(plate)
                finished = True
            elif left == right and plate != stolen_plates[middle]:
                total_comparisons = total_comparisons + 1
                finished = True            
            elif plate > stolen_plates[middle]:
                total_comparisons = total_comparisons + 1
                left = middle + 1
                middle = ((left + right) // 2)
            elif plate < stolen_plates[middle]:
                total_comparisons = total_comparisons + 1
                right = middle - 1
                middle = ((left + right) // 2)
            
    return result_list, total_comparisons

stolen = ["ABJ603", "BADMON", "CHUD69", "CRAP22", "DENGEL", "EFFETE", "IMGAY", "NINGAS", "OKEDOK", "PUS556", "ZMAN21"]
sighted = ["OKEDOK", "ABJ603", "BASTAR", "NINGAS"]
print(binary_simple_plate_finder(stolen, sighted))

Here's a binary search implementation that also counts the number of comparisons.这是一个二进制搜索实现,它也计算比较次数。

You'll need to populate sighted_plates and stolen_plates and remember that stolen_plates needs to be sorted.您需要填充有视力的_plates 和被盗的_plates,并记住需要对被盗的_plates 进行排序。

def bsearch(lst, x):
    c = 0
    L = 0
    R = len(lst) - 1
    while L <= R:
        m = (L + R) // 2
        c += 1
        if (v := lst[m]) == x:
            return m, c
        c += 1
        if v < x:
            L = m + 1
        else:
            R = m - 1
    return -1, c

stolen_plates = sorted(["ABJ603", "BADMON", "CHUD69", "CRAP22", "DENGEL", "EFFETE", "IMGAY", "NINGAS", "OKEDOK", "PUS556", "ZMAN21"])

sighted_plates = ["OKEDOK", "ABJ603", "BASTAR", "NINGAS"]

result_list = [] # list of stolen plates that have been sighted

total_comparisons = 0

for plate in sighted_plates:
    i, c = bsearch(stolen_plates, plate)
    if i >= 0:
        result_list.append(plate)
    total_comparisons += c

print(*result_list, sep='\n')
print(f'{total_comparisons=}')

Output: Output:

OKEDOK
ABJ603
NINGAS
total_comparisons=23

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM