第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

RecursionError:與進行二分搜索相比,超出了最大遞歸深度

RecursionError:與進行二分搜索相比,超出了最大遞歸深度

慕森卡 2021-12-08 10:13:01
這是使用python的字典程序。但我發(fā)現(xiàn)了這樣的錯誤。我想知道我看到的原因..如果你知道,請問我。這是我得到的錯誤:$ read datafile.txt$ size176050$ find YesterdayTraceback (most recent call last):  File "C:/Users/LG/PycharmProjects/alg1/lesson1.py", line 55, in <module>    word_index = binarysearch(words,word,0,len(words)-1)  File "C:/Users/LG/PycharmProjects/alg1/lesson1.py", line 25, in binarysearch    return binarysearch(data, target,middle+1, end)  File "C:/Users/LG/PycharmProjects/alg1/lesson1.py", line 23, in binarysearch    return binarysearch(data,target,begin,middle-1)  File "C:/Users/LG/PycharmProjects/alg1/lesson1.py", line 23, in binarysearch    return binarysearch(data,target,begin,middle-1)  File "C:/Users/LG/PycharmProjects/alg1/lesson1.py", line 23, in binarysearch    return binarysearch(data,target,begin,middle-1)  [Previous line repeated 994 more times]  File "C:/Users/LG/PycharmProjects/alg1/lesson1.py", line 13, in binarysearch    if begin > end:RecursionError: maximum recursion depth exceeded in comparisonProcess finished with exit code 1這是我的代碼:def datafile(file):    f = open(file,'rt',encoding='UTF8')    while True:        line = f.readline()        if not line:            break        if line == '\n':            continue        lines.append(line.split('\n')[0])    return linesdef binarysearch(data,target,begin,end):    if begin > end:        if data[end]:  #"end" index's front data exist              return end        else:            return -1    else:        middle = int(begin+end/2)        if data[middle] == target:            return middle        elif data[middle] > target:            return binarysearch(data,target,begin,middle-1)        else:            return binarysearch(data, target,middle+1, end)if __name__=="__main__":    lines = []  # all list    words = []  # list that all words is changed to lower    pos = []  # word's pos list
查看完整描述

1 回答

?
哆啦的時光機

TA貢獻1779條經(jīng)驗 獲得超6個贊

你的二分搜索算法有錯誤:

middle = int(begin+end/2)

由于除法優(yōu)先于加法,因此不會計算中間位置。它可能導致middle變得大于end,如果data[middle] > target那么下一個間隔將在下一次遞歸調用中更大。這會導致無限遞歸。

更正為:

middle = int((begin+end)/2)

或者干脆:

middle = (begin+end)//2


查看完整回答
反對 回復 2021-12-08
  • 1 回答
  • 0 關注
  • 253 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號