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[:]) 可以实现数组排序!
BaezaYates 交集python和golang代码的更多相关文章
- python 常忘代码查询 和autohotkey补括号脚本和一些笔记和面试常见问题
笔试一些注意点: --,23点43 今天做的京东笔试题目: 编程题目一定要先写变量取None的情况.今天就是因为没有写这个边界条件所以程序一直不对.以后要注意!!!!!!!!!!!!!!!!!!!!! ...
- vim python和golang开发环境配置
首先在-下新建目录.vim和配置文件.vimrc,.vimrc内容如下: syntax on set nocompatible filetype off set rtp+=~/.vim/bundle/ ...
- 深入浅出爬虫之道: Python、Golang与GraphQuery的对比
深入浅出爬虫之道: Python.Golang与GraphQuery的对比 本文将分别使用 Python ,Golang 以及 GraphQuery 来解析某网站的 素材详情页面 ,这个页面的特色是具 ...
- Python 坑爹之 代码缩进
建议:统一使用空格!!!!!!!!!不要Tab Python代码缩进 这两天python-cn邮件列表有一条thread发展的特别长,题目是<python的代码缩进真是坑爹>(地址), ...
- Python第一行代码
Python版本:Python 3.6.1 0x01 命令行交互 在交互式环境的提示符>>>下,直接输入代码,按回车,就可以立刻得到代码执行结果.现在,试试输入100+200,看看计 ...
- python的PEP8 代码风格指南
PEP8 代码风格指南 这篇文章原文实际上来自于这里:https://www.python.org/dev/peps/pep-0008/ 知识点 代码排版 字符串引号 表达式和语句中的空格 注释 版本 ...
- 用python处理html代码的转义与还原
用python处理html代码的转义与还原 转义 escape: import cgi s = cgi.escape("""& < >" ...
- 【转】利用Boost.Python将C++代码封装为Python模块
用Boost.Python将C++代码封装为Python模块 一. 基础篇 借助Boost.Python库可以将C/C++代码方便.快捷地移植到python模块当中,实现对python模块的扩 ...
- python爬虫小说代码,可用的
python爬虫小说代码,可用的,以笔趣阁为例子,python3.6以上,可用 作者的QQ:342290433,汉唐自远工程师 import requests import refrom lxml i ...
随机推荐
- [JOYOI] 1055 沙子合并
题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目描述 设有N堆沙子排成一排,其编号为1,2,3,-,N(N<=300).每堆沙子有 ...
- IDEA基本使用及配置(1)
前言:现在IDEA用的人很多,我以前都是用Eclipse的,都说这个IDE比较智能.好用,于是学习一下. IDEA与Eclipse目录结构对比: IDEA中的Project相当于Eclispe中的wo ...
- 使用 PHP + shell 生成 一键设置权限的脚本。
linux 系统 支持PHP脚本一键设置环境.shell脚本一键设置环境.那么 我今天 使用 PHP + shell 生成 一键设置权限的脚本. 举例子:linux服务器 一键配置discuz网站环 ...
- linux命令 host-常用的分析域名查询工具
博主推荐:更多网络测试相关命令关注 网络测试 收藏linux命令大全 host命令是常用的分析域名查询工具,可以用来测试域名系统工作是否正常. 语法 host(选项)(参数) 选项 -a:显示详细的 ...
- Centos6 安装nginx
一.编译安装nginx 1.安装nginx所需要的库pcre,pcre的全称为:perl compatible regular expression即perl正则表达式,是为了使nginx具备URL重 ...
- leds-gpio driver 续1
在上文中分析了gpio-led platform_device是如何定义并注册的. 那么gpio-led platform_device 和 gpio-led platform_driver是如何匹配 ...
- NioEventLoop.run select处理IO事件(boss/worker)流程:
NioEventLoop.run select处理IO事件(boss/worker)流程:processSelectedKeysprocessSelectedKeysOptimizedprocessS ...
- 【04】emmet系列之编辑器
[01]emmet系列之基础介绍 [02]emmet系列之HTML语法 [03]emmet系列之CSS语法 [04]emmet系列之编辑器 [05]emmet系列之各种缩写 前端开发人员,常用的是s ...
- 动态规划之最长递增子序列(LIS)
在一个已知的序列{ a1,a2,……am}中,取出若干数组成新的序列{ ai1, ai2,…… aim},其中下标 i1,i2, ……im保持递增,即新数列中的各个数之间依旧保持原数列中 ...
- uva1366/LA3530
简单的矩阵上的dp 从左上角扫到右下角,dp[i][j][0] 代表在i,j处选A ,dp[i][j][1] 代表在i,j处选B. dp[i][j][0]=max(dp[i-1][j][1],dp[i ...