BaezaYates 交集python代碼

來源:互聯網
上載者:User

標籤:turn   .so   main   交集   span   from   sed   port   style   

 

def bsearch(find, arr, low, high):    while low <= high:        mid = (low + high) >> 1        if arr[mid] == find:            return mid        elif arr[mid] > find:            high = mid - 1        else:            low = mid + 1    return -1def BaezaYates_intersect_helper(A, B, left1, right1, left2, right2, result):    if left1 > right1 or left2 > right2:        return    if right1-left1 > right2-left2:        left1, left2 = left2, left1        right1, right2 = right2, right1        A, B = B, A    mid = (left1 + right1) >> 1    index = bsearch(A[mid], B, left2, right2)    if index >= 0:        result.append(A[mid])        BaezaYates_intersect_helper(A, B, left1, mid-1, left2, index-1, result)        BaezaYates_intersect_helper(A, B, mid+1, right1, index+1, right2, result)    else:        if A[mid] > B[right2]:            BaezaYates_intersect_helper(A, B, left1, mid-1, left2, right2, result)        elif A[mid] < B[left2]:            BaezaYates_intersect_helper(A, B, mid+1, right1, left2, right2, result)def BaezaYates_intersect(A, B):    result = []    BaezaYates_intersect_helper(A, B, 0, len(A)-1, 0, len(B)-1, result)    result.sort()    return resultfrom random import randintif __name__ == "__main__":    for i in range(2000):        A = [randint(0, 100) for i in range(30)]        B = [randint(0, 100) for i in range(30)]        A.sort()        B.sort()        #print A        #print B        inter_set = BaezaYates_intersect(A, B)        #print inter_set        inter_set2 = set(A) & set(B)        for data in inter_set:            assert data in inter_set2    print "tests passed..."

 

BaezaYates 交集python代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.