Hi community,

I improved my binary search example with both sort.Search and sort.Find. 
Please correct me if I got new wrongs with new example.


1. 


My original question was if there are duplicate entries in sorted slice. 
After I located the first dup, how can I go to the rest of them without loop.
I ever thought using loop was ugly, so I tried to avoid loop. 
But I think loop is ok. because dups are adjacent on sorted slice.


2.


How can I use tuple style comparison found in other languages, like:


  (a.name, a.num) > (b.name, b.num)


struct can be used for == only, not for <, &gt;


3.


I think my conditions covered all the cases, but I need an extra trailing 
return ?&nbsp;
Whats the right way to do it?


&nbsp; if a < b {
&nbsp; &nbsp; return -1
&nbsp; } else if a == b {
&nbsp; &nbsp; return 0
&nbsp; } else if a &gt; b {
&nbsp; &nbsp; return 1
&nbsp; }


&nbsp; return -999 // an extra return




Thanks




//---


//sort.Search example
func sortSearch() {
&nbsp; haystack := []T{
&nbsp; &nbsp; {"aaa", 111},
&nbsp; &nbsp; {"bbb", 222}, //dup
&nbsp; &nbsp; {"bbb", 222}, //dup
&nbsp; &nbsp; {"ccc", 333},
&nbsp; }
&nbsp; needle := T{"bbb", 222}


&nbsp; index := sort.Search(len(haystack), func(i int) bool {
&nbsp; &nbsp; //asc &gt;=
&nbsp; &nbsp; //desc <=
&nbsp; &nbsp; if haystack[i].name &gt;= needle.name ||
&nbsp; &nbsp; &nbsp; haystack[i].name == needle.name &amp;&amp;
&nbsp; &nbsp; &nbsp; haystack[i].num &gt;= needle.num {
&nbsp; &nbsp; &nbsp; &nbsp; return true;
&nbsp; &nbsp; }


&nbsp; &nbsp; return false
&nbsp; })


&nbsp; log.Println(index)


&nbsp; for i := index; i < len(haystack); i++ {
&nbsp; &nbsp; if haystack[i] == needle {
&nbsp; &nbsp; &nbsp; log.Println(i, haystack[i])
&nbsp; &nbsp; } else {
&nbsp; &nbsp; &nbsp; break
&nbsp; &nbsp; }
&nbsp; }
}




//sort.Find example
func sortFind() {
&nbsp; log.SetFlags(log.LstdFlags | log.Llongfile)


&nbsp; haystack := []T{
&nbsp; &nbsp; {"aaa", 111},
&nbsp; &nbsp; {"bbb", 222}, //dup
&nbsp; &nbsp; {"bbb", 222}, //dup
&nbsp; &nbsp; {"ccc", 333},
&nbsp; }
&nbsp; needle := T{"bbb", 222}


&nbsp; i, found := sort.Find(len(haystack), func(i int) int {
&nbsp; &nbsp; //asc -1 0 1
&nbsp; &nbsp; //desc 1 0 -1
&nbsp; &nbsp; if needle.name < haystack[i].name ||
&nbsp; &nbsp; &nbsp; needle.name == haystack[i].name &amp;&amp;
&nbsp; &nbsp; &nbsp; needle.num < haystack[i].num {
&nbsp; &nbsp; &nbsp; &nbsp; return -1
&nbsp; &nbsp; } else


&nbsp; &nbsp; if needle == haystack[i] {
&nbsp; &nbsp; &nbsp; return 0
&nbsp; &nbsp; } else


&nbsp; &nbsp; if needle.name &gt; haystack[i].name ||
&nbsp; &nbsp; &nbsp; needle.name == haystack[i].name &amp;&amp;
&nbsp; &nbsp; &nbsp; needle.num &gt; haystack[i].num {
&nbsp; &nbsp; &nbsp; &nbsp; return 1
&nbsp; &nbsp; }


&nbsp; &nbsp; return 9 // do i have to add this line // Line 66
&nbsp; })


&nbsp; log.Println(found, i)


&nbsp; for i := i; found &amp;&amp; i != len(haystack); i++ {
&nbsp; &nbsp; if haystack[i] == needle {
&nbsp; &nbsp; &nbsp; log.Println(i, haystack[i])
&nbsp; &nbsp; } else {
&nbsp; &nbsp; &nbsp; break
&nbsp; &nbsp; }
&nbsp; }
}

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/tencent_9C30B68D792641F2B863FB873EF31B825309%40qq.com.

Reply via email to