Skip to content

Instantly share code, notes, and snippets.

@johannespetzold
Created April 27, 2018 18:35
Show Gist options
  • Save johannespetzold/a19ff606232dc6d2f2cf129963e11c57 to your computer and use it in GitHub Desktop.
Save johannespetzold/a19ff606232dc6d2f2cf129963e11c57 to your computer and use it in GitHub Desktop.
multi dimensional list
package main
import (
"fmt"
)
func main() {
l := &l{}
fmt.Printf("result=%v\n", sum(l, nil, l.dims()))
}
func sum(l *l, prefix []int, dims []int) int {
if len(dims) == 0 {
return l.get(prefix)
}
result := 0
n := dims[0]
for i := 0; i < n; i++ {
newPrefix := make([]int, len(prefix)+1)
for j := 0; j < len(prefix); j++ {
newPrefix[j] = prefix[j]
}
newPrefix[len(prefix)] = i
result += sum(l, newPrefix, dims[1:])
}
return result
}
type l struct{}
func (*l) get(ins []int) int {
fmt.Printf("get %v\n", ins)
return 0
}
func (*l) dims() []int {
return []int{1, 2, 3}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment