1
$\begingroup$

I have a list and I want to split it into 2 sublists in all possible ways If

 S={1,2,3,4}

I should get

{{{1},{2,3,4}},{{1,2},{3,4}}......{{1,4},{2,3}}....

I'm interested in the products of the sublists so I want

 {{1,4},{2,3}} 

to appear in the result

but NOT

  {{2,3},{1,4}} or {{3,2},{1,4}} 

to appear, too.

To make things clearer, for

S={1,2,3}

the result should be

{{{1},{2,3}},{{2},{1,3}},{{3},{1,2}}}

in any order you like

NOTE: Any element in the starting list can appear more than once

S={1,2,3,3,3,4} 

is acceptable and the result should have the element

 {{1,2,3},{3,3,4}}

multiple times for ALL different 3's

$\endgroup$
6
  • $\begingroup$ Have you seen this? $\endgroup$ Apr 9, 2017 at 22:56
  • $\begingroup$ What have you tried? TBH, this is one in a series of questions from you that seem more like a specification and a "do this for me" request, vs a "Here's my work so far"/"I'm having an issue implementing this", and this site is not a mechanical turk site. Add to this the very functionality requested is an example in the documentation. So, what have you tried? $\endgroup$
    – ciao
    Apr 9, 2017 at 22:58
  • $\begingroup$ That one splits the list into 2 with half lengths.I need all possible lengths... $\endgroup$
    – ZaMoC
    Apr 9, 2017 at 22:59
  • $\begingroup$ @ciao Is this a personal attack? I've tried many things that doesn't work and read many related questions.I believe that my question has not an answer yet, so it will benefit others, too.I don't understant why you're being so rude $\endgroup$
    – ZaMoC
    Apr 9, 2017 at 23:03
  • $\begingroup$ @Jenny_mathy I suggest you read this, in particular "a minimal working code example of your problem or your efforts" and "some proof of minimal Mathematica knowledge". This is not a tutorial site, this is not a "do this for me" site. Nothing personal about it. $\endgroup$
    – ciao
    Apr 9, 2017 at 23:10

2 Answers 2

1
$\begingroup$

I apologize if I have misunderstood. Perhaps,

f[u_] := 
 Module[{i = IntegerDigits[Range[2^Length[u] - 2], 2, Length[u]]},
  DeleteDuplicates[{Pick[u, #, 0], Pick[u, #, 1]} & /@ 
    i, #1 == Reverse[#2] &]]

For example:

Table[{j, f[j]}, {j, {Range[3], Range[4], {1, 2, 3, 3, 3, 4}}}] // 
 Grid[#, Frame -> All]

enter image description here

$\endgroup$
1
$\begingroup$

For this problem, you just use

Needs["Combinatorica`"]

KSetPartitions[Range[1, 3, 1], 2]

Mathematica 12.0

Needs[ "Combinatorica`"]



(* set partition *)
SetPartitions[{a, b, c}]  (* No constraints *)
RGFToSetPartition[#] & /@ RGFs[5]  (* Generate based on RGF, the result is consistent with the above, the order may be different *)
SetPartitionListViaRGF[5] (* Exactly the same as above, just wrapped again *)
KSetPartitions[3, 2]  (* Represents partitioning the set {1,2,3}, restricted to 2 parts *)
KSetPartitions[{a,b,c}, 2] (* Restricted to 2 parts *)

(* RGF related *)
RGFs[5]  (* Gives all RGF sequences of length 5 *)
SetPartitionToRGF[#] & /@ SetPartitions[5]  (* Generated based on sp, the result is consistent with the above, the order may be different *)
RandomRGF[5]  (* Equivalent to randomly selecting one from the above results *)
RGFToSetPartition[{1, 2, 1, 2, 1}, {a, b, c, d, e}]  (* Mapping from RGF to SetPartition *)
RGFToSetPartition[{1, 2, 1, 2, 1}]  (* If the second parameter is missing, association [n] will be performed *)
RGFToSetPartition[{1, 2, 1, 2, 1},{a,b}]  (* Invalid inputs will not calculate results *) 

(* Functions with rank are basically not needed to be looked at, if you know how to write [[]] *)
RankSetPartition[#] & /@ SetPartitions[4]  (* This result is Range[0, 14] *)

Reference

Restricted growth function(RGF) patterns and statistics

Partitions and Compositions - Wolfram Mathematica Official Documentation

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.