Skip to main content
Tweeted twitter.com/#!/StackMma/status/312921528276762625
added 17 characters in body; edited title
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

Pretty way to extractgroup elements at odd and even elementspositions

How to make a function that splits list to anelements by odd and even elementspositions? Shortest implementation wins. I myself came up with:

splitOdds[x_] := 
 Extract[x, {#}\[Transpose]] & /@ GatherBy[Range@Length@x, OddQ]

And:

splitOdds[x_] := Flatten[Partition[#, 1, 2]] & /@ {x, Rest@x}

splitOdds[{a, b, c, d, e, f}]
(*{{a, c, e}, {b, d, f}}*)

Pretty way to extract odd and even elements

How to make a function that splits list to an odd and even elements? Shortest implementation wins. I myself came up with:

splitOdds[x_] := 
 Extract[x, {#}\[Transpose]] & /@ GatherBy[Range@Length@x, OddQ]
splitOdds[x_] := Flatten[Partition[#, 1, 2]] & /@ {x, Rest@x}

splitOdds[{a, b, c, d, e, f}]
(*{{a, c, e}, {b, d, f}}*)

Pretty way to group elements at odd and even positions

How to make a function that splits list elements by odd and even positions? Shortest implementation wins. I myself came up with:

splitOdds[x_] := 
 Extract[x, {#}\[Transpose]] & /@ GatherBy[Range@Length@x, OddQ]

And:

splitOdds[x_] := Flatten[Partition[#, 1, 2]] & /@ {x, Rest@x}

splitOdds[{a, b, c, d, e, f}]
(*{{a, c, e}, {b, d, f}}*)
Clarifying what the result should look like
Source Link
swish
  • 8k
  • 1
  • 28
  • 50

How to make a function that splits list to an odd and even elements? Shortest implementation wins. I myself came up with:

splitOdds[x_] := 
 Extract[x, {#}\[Transpose]] & /@ GatherBy[Range@Length@x, OddQ]
splitOdds[x_] := Flatten[Partition[#, 1, 2]] & /@ {x, Rest@x} 

splitOdds[{a, b, c, d, e, f}]
(*{{a, c, e}, {b, d, f}}*)

How to make a function that splits list to an odd and even elements? Shortest implementation wins. I myself came up with:

splitOdds[x_] := 
 Extract[x, {#}\[Transpose]] & /@ GatherBy[Range@Length@x, OddQ]
splitOdds[x_] := Flatten[Partition[#, 1, 2]] & /@ {x, Rest@x}

How to make a function that splits list to an odd and even elements? Shortest implementation wins. I myself came up with:

splitOdds[x_] := 
 Extract[x, {#}\[Transpose]] & /@ GatherBy[Range@Length@x, OddQ]
splitOdds[x_] := Flatten[Partition[#, 1, 2]] & /@ {x, Rest@x} 

splitOdds[{a, b, c, d, e, f}]
(*{{a, c, e}, {b, d, f}}*)
Post Reopened by Mr.Wizard
Post Closed as "exact duplicate" by Mr.Wizard
Source Link
swish
  • 8k
  • 1
  • 28
  • 50

Pretty way to extract odd and even elements

How to make a function that splits list to an odd and even elements? Shortest implementation wins. I myself came up with:

splitOdds[x_] := 
 Extract[x, {#}\[Transpose]] & /@ GatherBy[Range@Length@x, OddQ]
splitOdds[x_] := Flatten[Partition[#, 1, 2]] & /@ {x, Rest@x}