Is it possible to select numbers by their digit parity?

Let’s say I have a list of all the 3 digit numbers from 100 to 999 and I want to select those that have an {even, odd, even} parity digit pattern like 204, 678, etc. Is this possible using Select, or Cases or even Pick for I have tried all with no success? The nearest I came to some sort of selection was with this code.

FromDigits /@ 

 Cases[IntegerDigits /@ Range[100, 999], {EvenQ_, OddQ_, EvenQ_}]

but as you can see the results are far from the desired outcome.

Paul.

You were very close…

FromDigits /@ 
 Cases[IntegerDigits /@ Range[100, 999], {_?EvenQ, _?OddQ, _?EvenQ}]

Fantastic, All working as expected now, thank you very much for the quick reply. Btw, is that a documented method? as it doesn’t look familiar.
Paul.

You can do this in a number of ways. Here are 2.

list = Range[100, 999];
result = FromDigits /@ Cases[IntegerDigits /@ list, {_?EvenQ, _?OddQ, _?EvenQ}];

Or you could just construct you list:

answer = FromDigits /@Tuples[{Range[2, 8, 2], Range[1, 9, 2], Range[0, 8, 2]}];

Some other approaches:

Select[list, EvenQ /@ IntegerDigits@# == {True, False, True} &]

or

Pick[list, EvenQ /@ (IntegerDigits /@ list), {True, False, True}]

Hello Paul,
I think it must be documented where pattern constraints are discussed.

But, I think if I were writing it for myself, I would have been more long-winded:

FromDigits/@Cases[
IntegerDigits /@ Range[100, 999],
 {a_, b_, c_} /;EvenQ[a] && OddQ[b] && EvenQ[c]
]

I find the above easier to read…

Using Select may be faster.

I tried using the method in the documentation simmilar to your method, namely this.

Cases[{{1, 2}, {2}, {3, 4, 1}, {5, 4}, {3, 3}}, {a_, b_} -> a + b]

and replacing the

-> a + b with -> EvenQ[a] && OddQ[b] && EvenQ[c]

and that didnt work either, which brings me back to the documentation where the syntax in your method isnt shown, i.e. the “/:” instead of “->”. So again I appreciate your input on this. and as you say the way you used it is easier to read.

Paul.

Hello Paul,
Hmmm, perhaps it is a matter of taste. But for this:

mylist = IntegerDigits /@ Range[100, 999]
Cases[mylist,{a_, b_, c_} /;EvenQ[a] && OddQ[b] && EvenQ[c]]

I would translate the last line in english as something like.

Find those elements in mylist which are a list of length three.
Each time a list of three is found, temporarily name the first element a, the second element b, and the third element c.
Finally, accept those elements where a is even, b is odd, and c is even.

If you cut and paste this into the help browser (tutorial/PuttingConstraintsOnPatterns) you will see examples of the /; and _? usage.

Just for fun: the 900 numbers can be partitioned by the parity of the integer digits. Of the 8 (2x2x2) patterns the 4 which start with an odd number: they ae of size 125 (5 x5 x5) and the 4 starting with even number are of size 100 (4x5x5): 900=4 x125 +4x100.

You can partition into these equivalence classes:

 num = GatherBy[Range[100, 999], EvenQ /@ IntegerDigits@# &];

You can visualize;

Grid[{StringJoin[
EvenQ /@ IntegerDigits@#[[1]] /. {True -> "E", False -> "O"}],Grid[Partition[#, 25]]} & /@ num, Frame -> All]