简体   繁体   English

Python从程序重复输出

[英]Python repeated output from program

I created a binary search but I am stuck at a problem. 我创建了二进制搜索,但是遇到了问题。 Whenever I run main( ) I just get None for when key equals 8, 5, 2, and 1. I also am trying to have bsearch return the index of the key_point value in integer_list but instead it just returns the key_point. 每当我运行main()时,当键等于8、5、2和1时,我只会得到None。我还试图让bsearch返回integer_list中key_point值的索引,但它只会返回key_point。 If someone could let me know what is going wrong I would appreciate it. 如果有人能让我知道出了什么问题,我将不胜感激。

def bsearch(integer_list, key_point, start, end):
    midpoint = (start + end) // 2
    if start >= end:
        return None
    elif integer_list[midpoint] == key_point:
        return key_point
    elif integer_list[midpoint] > key_point:
        return bsearch(integer_list, key_point, start, (midpoint - 1))
    elif integer_list[midpoint] < key_point:
        return bsearch(integer_list, key_point, (midpoint + 1), end)


def main( ):
    integer_list = [x + 1 for x in range(0, 10)]
    key = 11
    p = len(integer_list) - 1
    start = integer_list[0]
    end = integer_list[p]
    while key >= 1:
        bsearch(integer_list, key, start, end)
        print(bsearch(integer_list, key, start, end))
        key = key - 1

You have following errors: 您有以下错误:

1) The call to binary_search from main contains start = 1 and end = 10 which should have been start = 0 and end = 9 (value of the indices).We find all the mid points according to indices. 1)从mainbinary_search的调用包含start = 1 and end = 10 which should have been start = 0和end = 9 (索引值)。我们根据索引找到所有中点。

2) Just check start > end because start == end is absolutely correct and the cases where you got a None were all due to this case. 2)只需检查start > end因为start == end绝对正确,并且出现None情况都是由于这种情况。 All the cases where you returned due to this condition were actually the case where integer_list[midpoint] == key_point holded true. 由于这种情况您返回的所有情况实际上都是integer_list[midpoint] == key_point成立的情况。

Here's the corrected code: 这是更正的代码:

def bsearch(integer_list, key_point, start, end):
    midpoint = (start + end) // 2
    if start > end:
        return None
    elif integer_list[midpoint] == key_point:
        return key_point
    elif integer_list[midpoint] > key_point:
        return bsearch(integer_list, key_point, start, (midpoint - 1))
    elif integer_list[midpoint] < key_point:
        return bsearch(integer_list, key_point, (midpoint + 1), end)


def main():
    integer_list = [x + 1 for x in range(0, 10)]
    key = 11
    p = len(integer_list) - 1
    start = integer_list[0]
    end = integer_list[p]
    while key >= 1:
        print(bsearch(integer_list, key, 0, p))
        key = key - 1
main()       

Hope that helps :) 希望有帮助:)

You're mixing up indexes and values several times in your algorithm. 您正在算法中多次混合索引和值。 Your base/found case should be returning midpoint since that's the place you found it, but you're returning key_point, the value you're actually searching for instead. 您的基本案例/发现案例应该返回中点,因为这是您找到它的地方,但是您返回的是key_point,实际上是您要搜索的值。

And again, when you're setting up your loop, you're setting your start and end values to be the contents of the List at the beginning and end, instead of the beginning and ending index. 同样,在设置循环时,您将开始和结束值设置为List的开头和结尾的内容,而不是开头和结尾的索引。 Some of these errors will become more noticeable if for your test_list you use a set of numbers that isn't just sequential numbers 1-11. 如果您为test_list使用了一组数字,而不仅仅是连续数字1-11,那么其中的一些错误将变得更加明显。 Right now it just happens to line up. 现在,它正好排队。

First of all, you are passing values to the function instead of indexes: 首先,您要将值而不是索引传递给函数:

start = 0
end = len(integer_list) - 1

Second you need to return None when both start and end are reversed. 第二,当开始和结束都相反时,您需要返回None。 not when they are equal 当它们相等时

if start > end:
        return None

Third, why would you return the value you are searching for? 第三,为什么要返回要搜索的值? it does not make any sense. 这没有任何意义。 You should return the index of the element instead. 您应该改为返回元素的索引。

elif integer_list[midpoint] == key_point:
        return mid_point

Finally, if you want to follow what almost every search method returns, you should return -1 when you can't find the value you are looking for, instead of None. 最后,如果要遵循几乎所有搜索方法返回的内容,则在找不到所需的值时应返回-1,而不是None。 This will save you some headaches. 这将使您免于头痛。

if start > end:
        return -1

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

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