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. centos 简单用户管理

    一.配置文件 /etc/passwd:存放用户信息,以“:”分割成7个部分 1.账号名称,用来对应UID: 2.早期密码存放位置,后来密码改存/etc/shadow中,以“x”代替: 3.UID,使用 ...

  2. 详细了解为什么支持Postman Chrome应用程序已被弃用?

    本地postman chrome插件确实也无法正常使用,只有Postman官方自己的软件应用程序可以使用.笔者多少追溯终于知道原因,并紧急上线了不同操作系统版本的Postman应用程序. 为什么近期P ...

  3. 基础知识:if条件、while循环、for循环 相关练习

    1.实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败! while True: name = input('请输入用户名:') psw = inpu ...

  4. Data of Ch5 --Dual rotor

    * Results *Conclusion*- little effect of rear rotor on Cp_1- Cp1 is independent of TI** TI effect on ...

  5. Microsoft Azure 资料整理

    鉴于Microsoft Azure的技术迭代更新相当快,所以推荐大家还是以官方文档为准. 以Global Azure 的为主,Mooncake版本自行删减 首先推荐Azure for MSDN htt ...

  6. 细说php第八章笔记(初稿)

    8.1 函数的定义      函数是被命名的:      函数是独立的:      函数执行特定的任务:      函数可以用将一个返回值返回给调用他的程序 函数的优越性      提高程序的重用性 ...

  7. 转盘抽奖 canvas & 抽奖 H5 源码

    转盘抽奖 canvas https://github.com/givebest/wechat-turntalbe-canvas https://blog.givebest.cn/GB-canvas-t ...

  8. POJ 1988相对偏移

    //不容易啊,终于自己a了一道这种类型的题 // #include<stdio.h> #include<iostream> using namespace std; const ...

  9. MongoDB学习day03--索引和explain分析查询速度

    一.索引基础 db.user.ensureIndex({"username":1}) 创建索引,username为key,数字 1 表示 username 键的索引按升序存储, - ...

  10. mysql你确定掌握的那些sql语句

    1.创建表 create table test(uid int not null,create_time timestamp default current_timestamp); 即:没有双引号,单 ...