2
$\begingroup$

I want to remove any sub-lists whose last digit is odd. My attempt below does not look elegant because I convert each sub-list of digits to a number first and then remove the odd numbers.

Question

How can I filter the input by checking the last digit in each sub-list? Using a single DeleteCases if preferred.

Attempt

ClearAll[input, output, final]
input = Permutations@IntegerDigits@444123;
output = FromDigits /@ DeleteCases[input, {___, 4, 4 .., ___}];
final = DeleteCases[output, x_?OddQ]
$\endgroup$
0
4
$\begingroup$
input={{4, 4, 4, 1, 2, 3}, {4, 4, 4, 1, 3, 2}, {4, 4, 4, 2, 1, 3}, {4, 4, 4, 2, 3, 1}, {4, 4, 4, 3, 1, 2}};
DeleteCases[input, _List?(OddQ[#[[-1]]] &)]

(*{{4, 4, 4, 1, 3, 2}, {4, 4, 4, 3, 1, 2}}*)

or

DeleteCases[input, {___, a_} /; OddQ[a]]
$\endgroup$
4
$\begingroup$
Pick[input, BitAnd[1, input[[All, -1]]], 0]

Should be quite snappy on large lists.

$\endgroup$
3
$\begingroup$
Pick[input, Mod[Last /@ input, 2], 0]
Pick[input, EvenQ[Last /@ input]]
$\endgroup$
1
$\begingroup$
input//Pick[#,OddQ@#[[All,-1]],False]&

or

input//Pick[#,Divisible[#[[All,-1]],2]]&
$\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.