Skip to content

Instantly share code, notes, and snippets.

@gobwas
Last active September 7, 2016 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gobwas/f6fcf0e3b08f2937668fd741ddaae0e4 to your computer and use it in GitHub Desktop.
Save gobwas/f6fcf0e3b08f2937668fd741ddaae0e4 to your computer and use it in GitHub Desktop.
Combination
func loop(v []int, r func([]int)) {
var i, j, n uint
n = uint(len(v))
a := make([]int, 0, len(v))
for ; i < (1 << n); i++ {
a = a[:0]
for j = 0; j < n; j++ {
if i&(1<<j) != 0 {
a = append(a, v[j])
}
}
r(a)
}
}
func recursion(v []int, rcv func([]int)) {
var rec func(a, b []int)
rec = func(a, b []int) {
if len(b) == 0 {
rcv(a)
} else {
rec(append(a, b[0]), b[1:])
rec(a, b[1:])
}
}
a := make([]int, 0, len(v))
rec(a, v)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment