Something like https://play.golang.org/p/dZjaaorPcb ?

On Tue, Jul 12, 2016 at 1:54 PM, The MrU <drdos....@gmail.com> wrote:

> Hi,
>
> I have this code https://play.golang.org/p/9o5TReZ7jT3
> <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!")
> }
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to