Sha256: 2edb892e55b3b7c3244c50678aa2e4206b6749ec2f46f677396fdf1fba8f3da9

Contents?: true

Size: 755 Bytes

Versions: 104

Compression:

Stored size: 755 Bytes

Contents

package binarysearch

import "fmt"

func SearchInts(s []int, k int) (i int) {
	for j := len(s); i < j; {
		if h := (i + j) / 2; s[h] < k {
			i = h + 1
		} else {
			j = h
		}
	}
	return
}

func Message(s []int, k int) string {
	x := SearchInts(s, k)
	switch {
	case x == len(s):
		if x == 0 {
			return "slice has no values"
		}
		return fmt.Sprintf("%d > all %d values", k, x)
	case s[x] != k:
		if x == 0 {
			return fmt.Sprintf("%d < all values", k)
		}
		return fmt.Sprintf("%d > %d at index %d, < %d at index %d",
			k, s[x-1], x-1, s[x], x)
	}
	switch x {
	case 0:
		return fmt.Sprintf("%d found at beginning of slice", k)
	case len(s) - 1:
		return fmt.Sprintf("%d found at end of slice", k)
	}
	return fmt.Sprintf("%d found at index %d", k, x)
}

Version data entries

104 entries across 104 versions & 1 rubygems

Version Path
trackler-2.2.1.71 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.70 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.69 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.68 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.67 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.66 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.65 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.64 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.63 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.62 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.61 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.60 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.59 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.58 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.57 tracks/go/exercises/binary-search/example.go
trackler-2.0.6.33 tracks/go/exercises/binary-search/example.go
trackler-2.0.6.32 tracks/go/exercises/binary-search/example.go
trackler-2.0.6.31 tracks/go/exercises/binary-search/example.go
trackler-2.0.6.30 tracks/go/exercises/binary-search/example.go
trackler-2.0.6.29 tracks/go/exercises/binary-search/example.go