arrays - How to make a go program recursive -


how make go program recursive. i'm learning golang writing game number analyzer. i've been thinking , thinking on how , can't come working solution. here link in google playground. appreciated.

/* file record.go author: dan huckson date: 20160120 purpose: number analyzer */ package main  import (     "fmt" )  type stats struct {     category map[string]events }  type events struct {     event map[string]*event }  type event struct {     value int64 }  func main() {     winners := [][]int{         {1, 2, 3, 4, 5, 6},         {2, 4, 6, 28, 26, 39},         {1, 4, 9, 10, 26, 39},         {1, 9, 19, 29, 26, 49},         {4, 5, 6, 28, 26, 49}}      keys := []string{"digits1", "digits2", "digits3", "digits4", "digits5", "digits6"}      stats := new(stats)     stats.category = make(map[string]events)      _, key := range keys {         events, ok := stats.category[key]         if !ok {             events = *new(events)         }         events.event = make(map[string]*event)         stats.category[key] = events      }     fmt.println()      _, winner := range winners {         fmt.println(winner)         stats.digits1("digits1", winner)         stats.digits2("digits2", winner)         stats.digits3("digits3", winner)         stats.digits4("digits4", winner)         stats.digits5("digits5", winner)         stats.digits6("digits6", winner)     } }  func (stats *stats) record(key string, balls string) {      event, ok := stats.category[key].event[balls]     if !ok {         event = new(event)         stats.category[key].event[balls] = event     }     stats.category[key].event[balls].value += 1      word := ""     if len(balls) > 1 {         word = "balls"     } else {         word = "ball"     }      fmt.printf("%s:%s\tcount:%d\n", word, balls_to_csv(balls), stats.category[key].event[balls].value) }  func (stats *stats) digits1(key string, winner []int) {     := 0; < len(winner); i++ {         stats.record(key, string(winner[i]))     } }  func (stats *stats) digits2(key string, winner []int) {     := 0; < len(winner)-1; i++ {         j := + 1; j < len(winner); j++ {             stats.record(key, string(winner[i])+string(winner[j]))         }     } }  func (stats *stats) digits3(key string, winner []int) {     := 0; < len(winner)-2; i++ {         j := + 1; j < len(winner)-1; j++ {             k := j + 1; k < len(winner); k++ {                 stats.record(key, string(winner[i])+string(winner[j])+string(winner[k]))             }         }     } }  func (stats *stats) digits4(key string, winner []int) {     := 0; < len(winner)-3; i++ {         j := + 1; j < len(winner)-2; j++ {             k := j + 1; k < len(winner)-1; k++ {                 l := k + 1; l < len(winner); l++ {                     stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l]))                 }             }         }     } }  func (stats *stats) digits5(key string, winner []int) {     := 0; < len(winner)-4; i++ {         j := + 1; j < len(winner)-3; j++ {             k := j + 1; k < len(winner)-2; k++ {                 l := k + 1; l < len(winner)-1; l++ {                     m := l + 1; m < len(winner); m++ {                         stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l])+string(winner[m]))                     }                 }             }         }     } }  func (stats *stats) digits6(key string, winner []int) {     := 0; < len(winner)-5; i++ {         j := + 1; j < len(winner)-4; j++ {             k := j + 1; k < len(winner)-3; k++ {                 l := k + 1; l < len(winner)-2; l++ {                     m := l + 1; m < len(winner)-1; m++ {                         n := m + 1; n < len(winner); n++ {                             stats.record(key, string(winner[i])+string(winner[j])+string(winner[k])+string(winner[l])+string(winner[m])+string(winner[n]))                         }                     }                 }             }         }     } }  func balls_to_csv(key string) string {     s := ""     length := len(key)     := 0; < length; i++ {         s += fmt.sprintf("%d,", key[i])     }     return s[:len(s)-1] } 

as far can tell, want recursively find combinations of winning numbers. example,

package main  import "fmt"  func combinations(n []int, c []int, ccc [][][]int) [][][]int {     if len(n) == 0 {         return ccc     }     if len(ccc) == 0 {         ccc = make([][][]int, len(n))     }     := range n {         cc := make([]int, len(c)+1)         copy(cc, c)         cc[len(cc)-1] = n[i]         ccc[len(cc)-1] = append(ccc[len(cc)-1], cc)         ccc = combinations(n[i+1:], cc, ccc)     }     return ccc }  func main() {     n := []int{1, 2, 3, 4}     fmt.println("winning numbers", n)     fmt.println()     nw := 0     w := combinations(n, nil, nil)     fmt.println("winning tickets:")     d := " digit : "     := range w {         fmt.print(i+1, d)         d = " digits: "         j := range w[i] {             nw++             fmt.print(w[i][j], " ")         }         fmt.println()     }     fmt.println()     fmt.println(nw, "winners") } 

output:

winning numbers [1 2 3 4]  winning tickets: 1 digit : [1] [2] [3] [4]  2 digits: [1 2] [1 3] [1 4] [2 3] [2 4] [3 4]  3 digits: [1 2 3] [1 2 4] [1 3 4] [2 3 4]  4 digits: [1 2 3 4]   15 winners 

simplifying, can see recursion.

func combinations(n []int) {     if len(n) == 0 {         return      }     := range n {         combinations(n[i+1:])     }     return  } 

the recursion terminates when len(n) == 0. in loop, i increases len(n)-1, combinations(n[i+1:]) becomes combinations(n[len(n):]), , len(n[len(n):]) == 0, terminate recursion.


Comments