2
$\begingroup$

I have a list of sublists, let's say

Subsets[Range[15], {5}]

I need to select only those of them, that contain 3 odd and 2 even numbers, e.g. {{1,4,5,7,8},{2,7,9,10,11},{...}}

Please advise.

$\endgroup$
3
$\begingroup$
lst = Subsets[Range[15], {5}];
Pick[lst, Count[#, _?EvenQ] & /@ lst, 2]

%  // Length

1176

or

Pick[lst, Dot[Mod[lst, 2] , ConstantArray[1, 5]], 3] (* or *)
Pick[lst, Total[Mod[lst, 2], {2}], 3]
% // Length

1176

|improve this answer|||||
$\endgroup$
  • $\begingroup$ You can use Total /@ Mod[lst, 2] instead of Dot[Mod[lst, 2] , ConstantArray[1, 5]] $\endgroup$ – OkkesDulgerci Jul 21 '18 at 15:42
  • $\begingroup$ Thank uou @Okkes. I had Total [ Mod[lst, 2], {2}] previously; Thinking it would be faster i changed it to the Dot version. $\endgroup$ – kglr Jul 21 '18 at 16:38
1
$\begingroup$

If the sublists must be ordered:

Union @@@ Tuples[{Subsets[Range[1, 15, 2], {3}], Subsets[Range[2, 15, 2], {2}]}]

Otherwise:

Flatten[Tuples[{Subsets[Range[1, 15, 2], {3}], 
                Subsets[Range[2, 15, 2], {2}]}], {{1}, {2, 3}}]

where Flatten could alternatively be replaced by
ArrayReshape[#, {Binomial[Ceiling[15/2], 3] Binomial[Floor[15/2], 2], 3 + 2}] &

|improve this answer|||||
$\endgroup$
1
$\begingroup$
Union @@@Join @@ Outer[List, Subsets[Range[2, 15, 2], {2}],Subsets[Range[1, 17, 2], {3}], 1]
|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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