I'm writing a Go function that kind of mimics Python's itertools.permutations()
, but returning all permutations at once instead of yielding one at a time.
I'm seeing unexpected behavior when updating 2 variables at the same time in the following lines of code:
setcopy := append([]int(nil), sorted...)
for i := 0; i < r; i++ {
c := counters[r-1-i]
current[i], setcopy = setcopy[c], append(setcopy[:c], setcopy[c+1:]...)
}
I get correct results when decoupling the above update:
current[i] = setcopy[c]
setcopy = append(setcopy[:c], setcopy[c+1:]...)
I was mainly inspired by the Pop
and Delete
examples from the SliceTricks wiki article. Is there a significant difference between that and what I'm trying to do?
If you'd like to check out the full code (including an example of the faulty output and some print statements for debugging), you can check out this Gist.