BaezaYates 交集python和golang代码

复制代码
def bsearch(find, arr, low, high):
    while low <= high:
        mid = (low + high) >> 1
        if arr[mid] == find:
            return mid, True
        elif arr[mid] > find:
            high = mid - 1
        else:
            low = mid + 1
    return low, False


def 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,found = bsearch(A[mid], B, left2, right2)
    if found:
        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)
        else:
            BaezaYates_intersect_helper(A, B, left1, mid-1, left2, index-1, result)
            BaezaYates_intersect_helper(A, B, mid+1, right1, index, 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 result



from random import randint

if __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..."
复制代码

 对应的go代码:

复制代码
package main

import (
    "fmt"
    "math/rand"
    "sort"
    "time"
)

func bsearch(find int, arr []int, low int, high int) (int, bool) {
    for low <= high {
        mid := (low + high) >> 1
        if arr[mid] == find {
            return mid, true
        } else if arr[mid] > find {
            high = mid - 1
        } else {
            low = mid + 1
        }
    }
    return low, false
}

func BaezaYatesIntersectHelper(A []int, B []int, left1 int, right1 int, left2 int, right2 int, result *[]int) {
    if left1 > right1 || 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, found := bsearch(A[mid], B, left2, right2)
    /*
        if found {
            fmt.Printf("A[mid]=%d index=%d\n", A[mid], index)
        }
    */
    if found {
        *result = append(*result, A[mid])
        BaezaYatesIntersectHelper(A, B, left1, mid-1, left2, index-1, result)
        BaezaYatesIntersectHelper(A, B, mid+1, right1, index+1, right2, result)
    } else {
        if A[mid] > B[right2] {
            BaezaYatesIntersectHelper(A, B, left1, mid-1, left2, right2, result)
        } else if A[mid] < B[left2] {
            BaezaYatesIntersectHelper(A, B, mid+1, right1, left2, right2, result)
        } else {
            BaezaYatesIntersectHelper(A, B, left1, mid-1, left2, index-1, result)
            BaezaYatesIntersectHelper(A, B, mid+1, right1, index, right2, result)

        }
    }
}

func BaezaYatesIntersect(A, B []int) []int {
    result := []int{}
    BaezaYatesIntersectHelper(A, B, 0, len(A)-1, 0, len(B)-1, &result)
    sort.Ints(result)
    return result
}

func random(min, max int) int {
    return rand.Intn(max-min) + min
}

func main() {
    const SIZE int = 30
    for i := 0; i < 2000; i++ {
        A, B := [SIZE]int{}, [SIZE]int{}
        rand.Seed(time.Now().Unix())
        for j := 0; j < 30; j++ {
            A[j] = random(0, 100)
            B[j] = random(0, 100)
        }

        sort.Ints(A[:])
        sort.Ints(B[:])
        fmt.Println(A)
        fmt.Println(B)

        inter_set := BaezaYatesIntersect(A[:], B[:])
        fmt.Println(inter_set)
        /*
           inter_set2 = set(A) & set(B)

           for data in inter_set:
               assert data in inter_set2
        */
    }
    fmt.Printf("tests passed...\n")
}
复制代码

 牢记go语言中:

(1)要修改函数输入的slice参数,必须通过指针才能搞定,比如

func BaezaYatesIntersectHelper(A []int, B []int, left1 int, right1 int, left2 int, right2 int, result *[]int) 
最后一个参数!如果去掉指针,则无任何append效果!

(2)slice本质是array的内存引用!修改它必然会影响到array!因此,

sort.Ints(A[:]) 可以实现数组排序!





















本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6793788.html,如需转载请自行联系原作者



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值