Hi,
I have this code https://play.golang.org/p/9o5TReZ7jT
My idea was to generate all possible combinations pretty much this:
aaa
bbb
ccc
abb
acc
baa
bbb
bcc
caa
cbb
ccc
aba
abb
abc
you get the picture I think.
I got this solution but the objective is to simplify the solution without using channels if possible :
package main
import "fmt"
func GenerateCombinations(alphabet string, length int) <-chan string {
c := make(chan string)
go func(c chan string) {
defer close(c)
AddLetter(c, "", alphabet, length)
}(c)
return c
}
func AddLetter(c chan string, combo string, alphabet string, length int) {
if length <= 0 {
return
}
var newCombo string
for _, ch := range alphabet {
newCombo = combo + string(ch)
c <- newCombo
AddLetter(c, newCombo, alphabet, length-1)
}
}
func main() {
list := "abc"
for combination := range GenerateCombinations(list, 3) {
if len(combination) == 3 {
fmt.Println(combination)
}
}
fmt.Println("Done!")
}