0

Lets assume I have an array arr = ['a', 'b', 'c', 'd', 'e']. I want to get a list of all the ways I can split this into x groups.

For instance, if I do: get_all_ways_to_split_array_into_x_groups(arr, 2) should return something like:

[
(['a'], ['b', 'c', 'd', 'e']),
(['a', 'b'], ['c', 'd', 'e']),
(['a', 'b', 'c'], ['d', 'e']),
(['a', 'b', 'c', 'd'], ['e']),
(['b'], ['a', 'c', 'd', 'e']),
...
]

Where the order of the groups and the order of the elements in each group does not matter. I've looked at numpy/scipy/itertools/etc and cannot find something like this so I suspect custom code will be required and this could be quite computationally expensive. Any suggestions?

Edit: The initial array is not large, max of 20 or so elements.

gatapia
  • 3,574
  • 4
  • 40
  • 48
  • It will certainly be computationally expensive (even if you find a library solution), because there are O(2^n) combinations. – perigon Jul 19 '17 at 06:17
  • Would be interesting to know what you want to do with the combinations. Computing them all may be time consuming and the list might not even fit in memory if the array is moderately large. Certain optimizations may be possible if e.g. you just want to count the combinations or if you want to iterate over them. – MB-F Jul 19 '17 at 06:29
  • @cᴏʟᴅsᴘᴇᴇᴅ you are right, it is a duplicate and that question has an answer, I'll give it a try. Thanks – gatapia Jul 19 '17 at 06:39

0 Answers0