go-examples/example-clumps/main.go

27 lines
634 B
Go
Raw Normal View History

2017-07-07 12:29:36 +00:00
package main
import "fmt"
func countClumps(sl []int) uint {
var count uint
var prevs [2]int
for idx, val := range sl {
if (idx == 1 && val == prevs[1]) || (idx > 1 && val == prevs[1] && prevs[0] != prevs[1]) {
count++
}
prevs[0] = prevs[1]
prevs[1] = val
}
return count
}
func main() {
fmt.Println(countClumps([]int{2, 2, 2, 3, 4, 4}))
fmt.Println(countClumps([]int{1, 1, 2, 1, 1}))
fmt.Println(countClumps([]int{1, 1, 1, 1, 1}))
fmt.Println(countClumps([]int{0, 1, 1, 1, 1}))
fmt.Println(countClumps([]int{0, 0}))
fmt.Println(countClumps([]int{0}))
fmt.Println(countClumps([]int{1, 1, 2, 3, 1, 4, 1, 1}))
}