Welcome! Please see the About page for a little more info on how this works.

+3 votes
in math.combinatorics by
retagged by

When called with some duplicate items in items and with a :min & :max supplied, clojure.math.combinatorics/partitions can throw an IndexOutOfBoundsException. For example:
(dorun (clojure.math.combinatorics/partitions [1 1 2 3 4 5] :min 5 :max 5)) Execution error (IndexOutOfBoundsException) at clojure.math.combinatorics/m5$fn (combinatorics.cljc:859).
I can't replicate this when items doesn't include at least one duplicate, nor can I replicate it when :min isn't at least 5. I think :max has to be supplied, but doesn't always have to be equal to :min.
It happens with non-number items, and regardless of the order of items. It seems that it occurs when the first item is duplicated, but not for the other items.

1 Answer

+1 vote
by
selected by
 
Best answer

Here's my attempt at a patch, after verifying the issue (and discovering some extra issues, like :min 5 by itself causing a heap space error: https://clojure.atlassian.net/browse/MCOMB-11

I'd love to know if this fixes the other cases you found!

by
Yes that appears to fix all cases of exceptions I found. It may be useful to know it also appears to fix a stack overflow error too:

```
(dorun (combo/partitions [3 2 7 7 14 5 3 4 9 2] :min 2 :max 2))
;; => Execution error (StackOverflowError) at clojure.math.combinatorics/m5 (combinatorics.cljc:870).
```

I'm sure there's a simpler repro, but I haven't found it.

Cheers!
...