簡體   English   中英

如何對以某個字母開頭的單詞進行二進制搜索?

[英]How do I run a binary search for words that start with a certain letter?

我被要求對名稱列表進行二進制搜索,如果這些名稱以特定字母開頭,例如A,那么我要打印該名稱。 我可以通過執行更簡單的代碼來完成此任務,例如

for i in list:
    if i[0] == "A":
        print(i)

但是我被要求使用二進制搜索,而我正努力理解其背后的過程。 我們獲得了可以輸出給定字符串位置的基本代碼。 我的問題是不知道要編輯什么以便可以達到期望的結果

name_list = ["Adolphus of Helborne", "Aldric Foxe", "Amanita Maleficant", "Aphra the Vicious", "Arachne the Gruesome", "Astarte Hellebore", "Brutus the Gruesome", "Cain of Avernus"]


def bin_search(list, item):
    low_b = 0
    up_b = len(list) - 1
    found = False

    while low_b <= up_b and found ==  False:
        midPos = ((low_b + up_b) // 2)
        if list[midPos] < item:
            low_b = midPos + 1
        elif list[midPos] > item:
            up_b = midPos - 1
        else:
            found = True
    if found:
        print("The name is at positon " + str(midPos))
        return midPos
    else:
        print("The name was not in the list.")

期望的結果

bin_search(name_list,"A")

打印所有以A開頭的名稱(HelBorne的Adolphus,Aldric Foxe ....等)

編輯:我只是在做一些猜測和檢查,並找出如何做。 這是解決方案代碼

def bin_search(list, item):
    low_b = 0
    up_b = len(list) - 1
    true_list = []
    count = 100
    while low_b <= up_b and count > 0:
        midPos = ((low_b + up_b) // 2)
        if list[midPos][0] == item:
            true_list.append(list[midPos])
            list.remove(list[midPos])
            count -= 1
        elif list[midPos] < item:
            low_b = midPos + 1
            count -= 1
        else:
            up_b = midPos - 1
            count -= 1
    print(true_list)

不太確定這是否是您想要的,因為它似乎效率不高...正如您提到的那樣,僅遍歷整個列表但使用二進制搜索似乎更直觀,我在這里找到

def binary_search(seq, t):
    min = 0
    max = len(seq) - 1
    while True:
        if max < min:
            return -1
        m = (min + max) // 2
        if seq[m][0] < t:
            min = m + 1
        elif seq[m][0] > t:
            max = m - 1
        else:
            return m

index=0
while True:
    index=binary_search(name_list,"A")
    if index!=-1:
        print(name_list[index])
    else:
        break
    del name_list[index]

輸出我得到:

Aphra the Vicious
Arachne the Gruesome
Amanita Maleficant
Astarte Hellebore
Aldric Foxe
Adolphus of Helborne

您只需要找到一個以字母開頭的項目,然后確定范圍即可。 這種方法應該是快速且高效的內存。

def binary_search(list,item):
    low_b = 0
    up_b = len(list) - 1
    found = False
    midPos = ((low_b + up_b) // 2)
    if list[low_b][0]==item:
        midPos=low_b
        found=True
    elif list[up_b][0]==item:
        midPos = up_b
        found=True
    while True:
        if found:
            break;
        if list[low_b][0]>item:
            break
        if list[up_b][0]<item:
            break
        if up_b<low_b:
            break;
        midPos = ((low_b + up_b) // 2)
        if list[midPos][0] < item:
            low_b = midPos + 1
        elif list[midPos] > item:
            up_b = midPos - 1
        else:
            found = True
            break
    if found:
        while True:
            if midPos>0:
                if list[midPos][0]==item:
                    midPos=midPos-1
                    continue
            break;
        while True:
            if midPos<len(list):
                if list[midPos][0]==item:
                    print list[midPos]
                    midPos=midPos+1
                    continue
            break
    else:
        print("The name was not in the list.")

輸出是

>>> binary_search(name_list,"A")
Adolphus of Helborne
Aldric Foxe
Amanita Maleficant
Aphra the Vicious
Arachne the Gruesome
Astarte Hellebore

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM