我只是不明白实验室到底在要求什么.

enter image description here:

这是一个很好的例子.

class StringComparer:
    def compare(self, string1, string2):
        if string1 < string2:
            return -1
        elif string1 > string2:
            return 1
        else:
            return 0

and the main file looks like this:
from Searcher import Searcher
from NumComparer import NumComparer
from StringComparer import StringComparer

def main():
    sorted_fruits = [
        "Apple", "Apricot", "Banana", "Blueberry", "Cherry",
        "Grape", "Grapefruit", "Guava", "Lemon", "Lime", "Orange",
        "Peach", "Pear", "Pineapple", "Raspberry", "Strawberry"
    ]
    fruit_searches = [
        "Nectarine", "Mango", "Guava", "Strawberry",
        "Kiwi", "Apple", "Raspberry", "Carrot", "Lemon", "Bread"
        ]
    expected_fruit_search_results = [-1, -1, 7, 15, -1, 0, 14, -1, 8, -1]

    string_comparer = StringComparer()
    print_searches(sorted_fruits,
                   fruit_searches,
                   string_comparer,
                   expected_fruit_search_results,
                   True);

    # Perform sample searches with integers
    integers = [11, 21, 27, 34, 42, 58, 66, 71, 72, 85, 88, 91, 98]
    integer_searches = [42, 23, 11, 19, 87, 98, 54, 66, 92, 1, 14, 21, 66, 87, 83]
    expected_integer_search_results = [4, -1, 0, -1, -1, 12, -1, 6, -1, -1, -1, 1, 6, -1, -1]

    num_comparer = NumComparer()
    print_searches(integers,
                   integer_searches,
                   num_comparer,
                   expected_integer_search_results,
                   False);

def print_searches(sorted_list,
                   search_keys,
                   comparer,
                   expected_results,
                   key_in_quotes):
    # If key_in_quotes is True, " characters surround the key in output
    # statements. Otherwise empty strings surround the key.
    extra = '\"' if key_in_quotes else ''

    for i in range(len(search_keys)):
        # Get the key to search for
        search_key = search_keys[i]

        # Peform the search
        index = Searcher.binary_search(sorted_list, search_key, comparer)

        # Compare actual result against expceted
        expected = expected_results[i]
        if index == expected:
            print(f"PASS: Search for key {extra}{search_key}{extra} returned {expected}.")
        else:
            print(f"FAIL: Search for key {extra}{search_key}{extra} should have returned {expected}, but returned {index}.")

if __name__ == '__main__':
    main()

我不确定如何实现二分搜索到这个

推荐答案

我希望这对你有帮助.😊

def binary_search(sorted_list, key, comparer):
        low = 0
        high = len(sorted_list) - 1
        while low <= high:
            mid = (low + high) // 2
            comparison_result = comparer.compare(sorted_list[mid], key)
            if comparison_result == 0:
                return mid
            elif comparison_result <0:
                low = mid + 1
            else:
                high = mid - 1
        return -1

Python相关问答推荐

如何在Deliveryter笔记本中从同步上下文正确地安排和等待Delivercio代码中的结果?

Python库:可选地支持numpy类型,而不依赖于numpy

如何从在虚拟Python环境中运行的脚本中运行需要宿主Python环境的Shell脚本?

"使用odbc_connect(raw)连接字符串登录失败;可用于pyodbc"

如何从pandas的rame类继承并使用filepath实例化

删除marplotlib条形图上的底边

Django—cte给出:QuerySet对象没有属性with_cte''''

使用特定值作为引用替换数据框行上的值

无论输入分辨率如何,稳定扩散管道始终输出512 * 512张图像

如何使regex代码只适用于空的目标单元格

手动设置seborn/matplotlib散点图连续变量图例中显示的值

从一个df列提取单词,分配给另一个列

根据Pandas中带条件的两个列的值创建新列

如何将相同组的值添加到嵌套的Pandas Maprame的倒数第二个索引级别

pytest、xdist和共享生成的文件依赖项

启动线程时,Python键盘模块冻结/不工作

具有不匹配列的2D到3D广播

在Pandas 中以十六进制显示/打印列?

将数据从一个单元格保存到Jupyter笔记本中的下一个单元格

Python:使用asyncio.StreamReader.readline()读取长行