しつこいようだけど、今度は Go でやってみた。
package main import ( "fmt" ) func main() { var l1 = []int{1, 1, 2, 2, 3, 1, 1} var l2 = []int{} fmt.Printf("%v\n", adjacentGroup(l1)) fmt.Printf("%v\n", adjacentGroup(l2)) } func adjacentGroup(l []int) [][]int { var result [][]int if len(l) == 0 { return result } var current = []int{l[0]} for i := 1; i < len(l); i++ { if current[0] == l[i] { current = append(current, l[i]) } else { result = append(result, current) current = []int{l[i]} } } result = append(result, current) return result }
^o^ > go run adjacentGroup.go [[1 1] [2 2] [3] [1 1]] []