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[:]) 可以实现数组排序!

BaezaYates 交集python和golang代码的更多相关文章

  1. python 常忘代码查询 和autohotkey补括号脚本和一些笔记和面试常见问题

    笔试一些注意点: --,23点43 今天做的京东笔试题目: 编程题目一定要先写变量取None的情况.今天就是因为没有写这个边界条件所以程序一直不对.以后要注意!!!!!!!!!!!!!!!!!!!!! ...

  2. vim python和golang开发环境配置

    首先在-下新建目录.vim和配置文件.vimrc,.vimrc内容如下: syntax on set nocompatible filetype off set rtp+=~/.vim/bundle/ ...

  3. 深入浅出爬虫之道: Python、Golang与GraphQuery的对比

    深入浅出爬虫之道: Python.Golang与GraphQuery的对比 本文将分别使用 Python ,Golang 以及 GraphQuery 来解析某网站的 素材详情页面 ,这个页面的特色是具 ...

  4. Python 坑爹之 代码缩进

    建议:统一使用空格!!!!!!!!!不要Tab Python代码缩进   这两天python-cn邮件列表有一条thread发展的特别长,题目是<python的代码缩进真是坑爹>(地址), ...

  5. Python第一行代码

    Python版本:Python 3.6.1 0x01 命令行交互 在交互式环境的提示符>>>下,直接输入代码,按回车,就可以立刻得到代码执行结果.现在,试试输入100+200,看看计 ...

  6. python的PEP8 代码风格指南

    PEP8 代码风格指南 这篇文章原文实际上来自于这里:https://www.python.org/dev/peps/pep-0008/ 知识点 代码排版 字符串引号 表达式和语句中的空格 注释 版本 ...

  7. 用python处理html代码的转义与还原

    用python处理html代码的转义与还原   转义 escape: import cgi s = cgi.escape("""& < >" ...

  8. 【转】利用Boost.Python将C++代码封装为Python模块

    用Boost.Python将C++代码封装为Python模块 一.     基础篇 借助Boost.Python库可以将C/C++代码方便.快捷地移植到python模块当中,实现对python模块的扩 ...

  9. python爬虫小说代码,可用的

    python爬虫小说代码,可用的,以笔趣阁为例子,python3.6以上,可用 作者的QQ:342290433,汉唐自远工程师 import requests import refrom lxml i ...

随机推荐

  1. luogu 2-SAT 问题

    题目大意:给出n个bool变量,以及m个条件,条件为x,vx,y,vy,表示 x == vx || y == vy . 求匹配. 题解: 最近新学了一下2-SAT算法.2-SAT指有若干个bool变量 ...

  2. Go:二分查找

    package main import "fmt" func BinarySearch(arr *[5]int, leftIndex int, rightIndex int, fi ...

  3. PHP经典算法集锦【经典收藏】

    本文实例总结了PHP经典算法.分享给大家供大家参考,具体如下: 1.首先来画个菱形玩玩,很多人学C时在书上都画过,咱们用PHP画下,画了一半. 思路:多少行for一次,然后在里面空格和星号for一次. ...

  4. 集训第六周 古典概型 期望 D题 Discovering Gold 期望

    Description You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell o ...

  5. SpringBoot第十六篇:自定义starter

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11058502.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言   这一段时间 ...

  6. c语音 dll断点调试方法

    转自:https://blog.csdn.net/qingzai_/article/details/45348613 dll调试方法: 1.把最新生成的dll和pdb放到 启动这个dll 的进程目录下 ...

  7. Leetcode 143.重排链表

    重排链表 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示 ...

  8. 九度oj 题目1438:最小公倍数

    题目1438:最小公倍数 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2451 解决:2057 题目描述: 给定两个正整数,计算这两个数的最小公倍数. 输入: 输入包含多组测试数据,每 ...

  9. JavaEE JDBC 了解JNDI

    了解JNDI @author ixenos Web与企业应用中的连接管理 1. 数据库连接方式: (1)使用配置文件 (2)使用JNDI 2. 在Web或企业环境中部署 JDBC应用时,数据库连接管理 ...

  10. Linux下汇编语言学习笔记17 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...