sort.Search,寻找一个在切片中不存在的数字

huangapple go评论66阅读模式
英文:

sort.Search, looking for a number that is not in the slice

问题

我目前在我的项目中遇到一个问题,我必须找到在一个切片中不存在的数字。我在golang网站上找到了可行的代码,这个代码很好用。在我的项目中,我创建了一个空块,让else语句完成所有的工作。我试图修改代码以删除空块,但每次都出现错误,最后我找到了一个能重现问题的示例:

package main

import (
	"fmt"
	"sort"
)

func main() {
	data := []int{27, 15, 8, 9, 12, 4, 17, 19, 21, 23, 25}
	nr := 9
	sort.Ints(data)
	index := sort.Search(len(data), func(index int) bool { return data[index] == nr })
	if index == len(data) {
		fmt.Print("It's not in : ")
		fmt.Println(nr)
	} else {
		fmt.Print("It's in! Index is at : ")
		fmt.Println(index)
	}
}

golang playground上可以运行这段代码!

英文:

I currently have a problem in my project where i must find numbers that are not present in a slice. I found working code in the golang website, this works great. In my project i make a empty block and let the else statement do all the work. I tried to alter the code to remove the empty block but i got errors every time, i finaly found a example that reproduces the problem :

package main

import (
	"fmt"
	"sort"
)

func main() {
	data := []int{27, 15, 8, 9, 12, 4, 17, 19, 21, 23, 25}
	nr := 9
	sort.Ints(data)
	index := sort.Search(len(data), func(index int) bool { return data[index] == nr })
	if index == len(data) {
		fmt.Print("It's not in : ")
		fmt.Println(nr)
	} else {
		fmt.Print("It's in! Index is at : ")
		fmt.Println(index)
	}
}

Working code on golang playground!

答案1

得分: 5

我也遇到了同样的问题,因为我误解了godoc sort Search中的文档。

> 如果调用者想要查找23是否在切片中,它必须单独测试data[i] == 23

我以为这意味着“文档说==是允许的”。实际上,在作为参数传递给sort.Search函数时,只能使用>=<=,不能使用==。而那句话的意思是,在你得到索引i之后,你必须用data[i] == 23来测试,以确保23在切片中。

英文:

I encounter the same problem too, because I misunderstood the document in godoc sort Search.

> If the caller wants to find whether 23 is in the slice, it must test data[i] == 23 separately.

I thought that means "The documentation says that == is allowed" too. In fact, in the function as a parameter in sort.Search, can only use &gt;= or &lt;=, no ==. And that sentence means after you got the index i, you have to test it with data[i] == 23 to make sure 23 is in the slice.

答案2

得分: 3

例如,

package main

import (
	"fmt"
	"sort"
)

func main() {
	data := []int{27, 15, 8, 9, 12, 4, 17, 19, 21, 23, 25}
	sort.Ints(data)
	fmt.Println(data)
	x := 9
	notpresent := false
	i := sort.Search(len(data), func(i int) bool { return data[i] >= x })
	if i >= len(data) || data[i] != x {
		// x不在data中,
		// 但i是它将被插入的索引。
		notpresent = true
	}
	fmt.Println(x, notpresent)
}

输出:

[4 8 9 12 15 17 19 21 23 25 27]
9 false
英文:

For example,

package main

import (
	&quot;fmt&quot;
	&quot;sort&quot;
)

func main() {
	data := []int{27, 15, 8, 9, 12, 4, 17, 19, 21, 23, 25}
	sort.Ints(data)
	fmt.Println(data)
	x := 9
	notpresent := false
	i := sort.Search(len(data), func(i int) bool { return data[i] &gt;= x })
	if i &gt;= len(data) || data[i] != x {
		// x is not present in data,
		// but i is the index where it would be inserted.
		notpresent = true
	}
	fmt.Println(x, notpresent)
}

Output:

[4 8 9 12 15 17 19 21 23 25 27]
9 false

huangapple
  • 本文由 发表于 2014年10月23日 08:24:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/26519344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定