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 ...
随机推荐
- buf.swap16()
buf.swap16() 返回:{Buffer} 将 Buffer 解释执行为一个16位的无符号整数数组并以字节顺序交换到位.如果 Buffer 的长度不是16位的倍数,则抛出一个 RangeErro ...
- 15Spring泛型依赖注入
Spring 4.x中可以为子类注入子类对应的泛型类型的成员变量的引用 BaseService<T>:有RoleService和UserService两的子类 BaseRepepositr ...
- Python之面向对象上下文管理协议
Python之面向对象上下文管理协议 析构函数: import time class Open: def __init__(self,filepath,mode='r',encode='utf-8') ...
- 【Codeforces 1117C】Magic Ship
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 我们可以把这个行船的过程分解成两个过程 1.船经过时间t被风吹到了某个地方 2.船用这t时间尝试到达终点(x2,y2) 会发现如果时间t能最终 ...
- 全文搜索(A-3)-用户建模
用户模型可以分为静态模型.动态模型.混合推荐用户模型. 静态模型往往通过显式方式收集用户偏好信息: 动态模型通过隐式方式收集用户偏好信息: 基于内容的用户系统的推荐模型: 关键字匹配,空间向量模型 协 ...
- MT6755 平台手机皮套驱动实现
是自己写注册一个input device,模仿keypad,在对应的中断处理函数中上报power key的键值. 具体实现代码如下: 在 alps/kernel-3.10/drivers/misc/m ...
- 自定义日志工具LogUtil
package com.pingyijinren.test; import android.util.Log; /** * Created by Administrator on 2016/5/20 ...
- Gym100812 L 扩展欧几里得
L. Knights without Fear and Reproach time limit per test 2.0 s memory limit per test 256 MB input st ...
- MySQL使用教程收集(语法教程/命令教程)
说明:现在市面上的教程除了基本语法外,都基本是五花八门的,最权威且最全面的解释应该上官网去查看. https://www.tutorialspoint.com/mysql/index.htm http ...
- Servlet的会话(Session)跟踪
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/session-tracking.html: HTTP是一种“无状态”协议,这意味着每次客户端检索 ...