Thanks Ian,

You are correct (although the post I submitted yesterday had the clobbering 
lines incorrect - that was one of my experiments to see if rebuilding the 
3D slice differently would overcome the problem).   The code below is my 
original.

Yes, the function works by building fresh versions of out at each later and 
then reconstructing each set from the returned outs.

I am currently assuming that because it works for 2, 3, and 4 then somehow 
I'm doing something that causes my Go runtime (latest version running on 
Linux Mint 18 on an Intel i5) to return an answer before it has finished 
producing sets.

My next experiments will be to try to use the Gogland debug to establish 
why it stops.


package main
import (
    "fmt"
    "time")

func get_partitions(set []string, out [][][]string) [][][]string {
    out = make([][][]string, 1)
    out[0] = make([][]string, 1)
    out[0][0] = make([]string, 0)

    if len(set) <= 1 {
        out[0][0] = set
        return out
    }
    for j := 0; j < len(set)*len(set)/2; j++ {
        i := j
        parts := make([][]string, 2)
        for _, item := range set {
            parts[i&1] = append(parts[i&1], item)
            i >>= 1
        }
        pts := get_partitions(parts[1], out)
        for _, pt := range pts {
            i3 := len(out) - 1
            if len(out[i3][len(out[i3])-1]) == 0 {
                out[i3][len(out[i3])-1] = parts[0]
            } else {
                out = append(out, [][]string{})
                i3 += 1
                out[i3] = append(out[i3], parts[0])
            }
            if len(pt[0]) > 0 {
                for ptidx, _ := range pt {
                    out[i3] = append(out[i3], pt[ptidx])
                }
            }
        }
    }
    return out}

func main() {
    set := []string{"1", "2", "3", "4", "5"}
    var out, ppp [][][]string
    out = make([][][]string, 1) // create & initialize 3D slice
    out[0] = make([][]string, 1)
    out[0][0] = make([]string, 1)
    t0 := time.Now()
    ppp = get_partitions(set, out)
    elapsed := time.Since(t0)
    fmt.Printf("Took %s\n", elapsed)
    fmt.Printf("\nGot the partition: %v\n", ppp)
    fmt.Println(len(ppp))}

-- 
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