Sha256: 2b10d84875e332644b60bb31c1ac21ed1d6613a2190ad8b4e1ecb76ca92653ad

Contents?: true

Size: 778 Bytes

Versions: 185

Compression:

Stored size: 778 Bytes

Contents

package binarysearch

import "fmt"

const testVersion = 1

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

185 entries across 185 versions & 1 rubygems

Version Path
trackler-2.2.1.56 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.55 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.54 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.53 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.52 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.51 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.50 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.49 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.48 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.47 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.46 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.45 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.44 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.43 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.42 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.41 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.40 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.39 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.38 tracks/go/exercises/binary-search/example.go
trackler-2.2.1.37 tracks/go/exercises/binary-search/example.go