Skip to main content
3 of 3
edited tags

Optimal Solution for OddsBeforeEvens Wolfram Challenge

Odds before Evens Given a list of integers, rearrange them so that all of the odd integers appear before all of the even integers, without otherwise changing the order.

An obvious solution is

OddBeforeEven[data_List]:= Join[Cases[data, _?OddQ], Cases[data, _?EvenQ]]

But this isn't particularly efficient, so maybe we can try

OddBeforeEven[data_List]:= Join[Select[data, OddQ], Select[data, EvenQ]]

Now, an innovative solution could also be something like

OddBeforeEven[data_List]:= Flatten@GatherBy[data, OddQ]

But I can't seem to improve it further.

Can we optimize OddQ and EvenQ using Bit operations?

enter image description here

Navvye
  • 171
  • 9