# Shortest Program to Sort a List of numbers into Odd and Even Category

In the New Modern Times, when Charlie Chaplin encounters a computer, he is employed in the sorting Yard, as a validator to determine if the workers are correctly sorting the items. The Items in question are packets of marbles. Packets with Odd number of Marbles are stacked in the Red Basket and Packets with Even Number of Marbles are stacked in the Blue Basket.

Charlie Chaplin is supposed to punch the program that would validate if there is any anomaly in the sorting procedure. Mack Swain his immediate Boss, shares an algorithm which he needs to code.

Algorithm

L = List of Marble packets that's already sorted
L_ODD = List of packets with Odd Number of Marbles
L_EVEN = List of packets with Even Number of Marbles
Check_Digit = √(ΣL_ODD² + ΣL_EVEN²)


His Job is to determine the Check_Digit and match it with what ever value his Boss calculates.

Charlie Chaplin during his lunch hours, were able to sneak to Mack Swain's drawer and determine, that his drawer has a single card with punches on the first 46 32 columns (which means Mack was able to write a program with only 46 32 characters).

Charlie Chaplin would now need the help of all the code ninja's to write a program with as few lines as possible. He also announces a bonus of 50 points, if someone can come up with a program which is shorter than his Boss.

Summary

Given a list/array/vector of positive numbers (odd and even), you need to write a function, which would accept the array(int [])/vector<int>/list and calculate the root of the sum of the squares of the sums of odd and even numbers in the list.

The Size of the program is the size of the body of the function, i.e. excluding the size of the function signature.

Example

List = [20, 9, 4, 5, 5, 5, 15, 17, 20, 9]
Odd = [9, 5, 5, 5, 15, 17, 9]
Even = [20, 4, 20]
Check_Digit = √(ΣOdd² + ΣEven²) = 78.49203781276162


Note, the actual output might vary based on the implementation's floating point precision.

Score

Score is calculated as Σ(Characters in your Program) - 46.Score is calculated as Σ(Characters in your Program) - 32. Apart from the regular upvoting from the community, the lowest negative score would receive an additional bonus of 50 points.

Edit

1. The offset that was used to calculate the Score has been changed from 46 to 32. Note, this would not affect the leader-board/ bounty eligibility or invalidate any solution.

Verdict

After a gruesome duel between the Ninjas, Mr. Chaplin received some wonderful answers. Unfortunately few of the answers tried to take undue advantage of the rule and was not very useful. He actually wanted a fair duel and answers where the logic was coded within the function signatures would eventually mean the function signature is an integral part of the solution. Finally, Ninja FireFly was the clear winner and awarded him the bonus he well deserves. Leaderboard (updated every day)

╒══════╤═════════════════╤══════════════╤═════════╤════════╤═══════╕
├ Rank │      Ninja      │   Dialect    │ Punches │ Scores │ Votes ┤
╞══════╪═════════════════╪══════════════╪═════════╪════════╪═══════╡
│  0   │     FireFly     │      J       │   17    │  -15   │   6   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  1   │     tmartin     │     Kona     │   22    │  -10   │   2   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  2   │ Sven Hohenstein │      R       │   24    │   -8   │   7   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  3   │    Ben Reich    │  GolfScript  │   30    │   -2   │   1   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  4   │    mollmerx     │      k       │   31    │   -1   │   0   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  5   │ David Carraher  │ Mathematica  │   31    │   -1   │   3   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  6   │     tmartin     │      Q       │   34    │   2    │   1   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  7   │     daniero     │      dc      │   35    │   3    │   1   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  8   │    psion5mx     │    Python    │   38    │   6    │   2   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  9   │       O-I       │     Ruby     │   39    │   7    │   5   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  10  │      gggg       │    Julia     │   40    │   8    │   1   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  11  │ FakeRainBrigand │  LiveScript  │   50    │   18   │   0   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  12  │    Sylwester    │    Perl5     │   50    │   18   │   2   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  13  │     daniero     │     Ruby     │   55    │   23   │   1   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  14  │    vasuakeel    │ Coffeescript │   57    │   25   │   1   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  15  │      dirkk      │    XQuery    │   63    │   31   │   0   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  16  │  crazedgremlin  │   Haskell    │   64    │   32   │   0   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  17  │   Uri Agassi    │     Ruby     │   66    │   34   │   1   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  18  │     Sumedh      │     JAVA     │   67    │   35   │   0   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  19  │      Danny      │  Javascript  │   67    │   35   │   0   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  20  │     deroby      │      c#      │   69    │   37   │   1   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  21  │  Adam Speight   │      VB      │   70    │   38   │   0   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  22  │    Andrakis     │    Erlang    │   82    │   50   │   0   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  23  │      Sp0T       │     PHP      │   85    │   53   │   0   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  24  │    brendanb     │   Clojure    │   87    │   55   │   0   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  25  │  Merin Nakarmi  │      C#      │   174   │  142   │   0   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  26  │    Boopathi     │     JAVA     │   517   │  485   │   0   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  27  │      Noyo       │     ES6      │    ?    │   ?    │   2   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  28  │     shiona      │   Haskell    │    ?    │   ?    │   0   │
├──────┼─────────────────┼──────────────┼─────────┼────────┼───────┤
│  29  │      Vivek      │     int      │    ?    │   ?    │   0   │
└──────┴─────────────────┴──────────────┴─────────┴────────┴───────┘

• Why does the lowest score need a bonus, they already won?
– gggg
Feb 5, 2014 at 20:02
• Moreover, the offset of 46 doesn't change anything regarding the order. Feb 5, 2014 at 20:06
• @gggg I think they mean they'll give the lowest scoring answer a +50 bounty.
– user8777
Feb 5, 2014 at 23:27
• @gggg: I will actually start a bounty as soon as I am allowed to. So that's what I meant by bonus. Feb 6, 2014 at 2:01
• Aw, rats. Here I thought the rules were there to be taken advantage of, and that cleverness would be rewarded here. ;] Still, fun question, and good job, everyone!
– Noyo
Feb 17, 2014 at 16:49

## LiveScript 50 (or 1 if you're flexible)

The 1 character solution is r where the function signature is this:

fun = (a,o=(sum a.filter odd),e=(sum a.filter even),r=(e*e+o*o)**0.5) ->


But I think that's a technicality.

o=sum a.filter odd
e=sum a.filter even
(e*e+o*o)^0.5


# k (31-32 = -1)

sqrt{+/b*b:+/'(.)x@=(_a)=a:x%2}


Example run:

  sqrt{+/b*b:+/'(.)x@=(_a)=a:x%2} 20 9 4 5 5 5 15 17 20 9
78.492037812761623


# C (68-32 = 36)

float f(int *a, int c)
{int s[2]={0};while(c--)s[a[c]%2]+=a[c];return sqrt(*s**s+s[1]*s[1]);}


In action:

#include <stdio.h>
#include <math.h>

float f(int *a, int c)
{int s[2]={0};while(c--)s[a[c]%2]+=a[c];return sqrt(*s**s+s[1]*s[1]);}

int main(void)
{
printf("%f\n", f((int[]){20, 9, 4, 5, 5, 5, 15, 17, 20, 9}, 10));
return 0;
}


# ised (10 characters - 32) = -22

ASCII:

@={@+E$1@+O$1}


UTF8:

@={ΣE$1ΣO$1}


The variable $1 must contain the array. Example invocation from shell: ised --l - '@={ΣE$1ΣO$1}' <<<"20, 9, 4, 5, 5, 5, 15, 17, 20, 9"  Example invocation in interactive mode: ised> @1{20, 9, 4, 5, 5, 5, 15, 17, 20, 9} 20 9 4 5 5 5 15 17 20 9 ised> @={ΣE$1ΣO$1} 78.492  Explanation: operators O and E extract odd and even elements of the array. The operator @+ or Σ sums the array. @= returns the length of the given vector. If you want a function, it's even shorter. Saving a function: ised> @2{: @={ΣExΣOx} :}  The function body is only 10 characters long. The rest is the function declaration ({: :}) and storage of the function handle into memory slot 2 (@2). Invocation ised>$2::{20 9 4 5 5 5 15 17 20 9}


# Tcl, 60 bytes (bonuses not included yet)

proc C L {lmap c $L {incr [expr$c%2] $c} expr hypot($0,\$1)}


Try it online!

# Factor + math.unicode, 43 - 32 = 11

[ [ odd? ] partition [ Σ sq ] bi@ + sqrt ]


Try it online!

# Jelly, 10 bytes - 32 = score -22

ḟḂƇ,ḂƇ§²S½


Try it online!

JAVA 517 bytes

 public class Link
{
public static void main(String[] args)
{
int[]a={20, 9, 4, 5, 5, 5, 15, 17, 20, 9};
int []b=new int[10];
int[]c=new int[10];
int j=0,k=0,l=0,m=0;
for(int i=0;i<a.length;i++)
{
if(a[i]%2==0)
{
b[j]=a[i];
j++;
}
else
{
c[k]=a[i];
k++;
}
}
for(int i=0;i<j;i++)
{
l=l+b[i];
}
l=l*l;
for(int i=0;i<k;i++)
{
m=m+c[i];
}
m=m*m;
l=l+m;
double t=Math.sqrt(l);
System.out.print(t);
}

}


# Javascript 67-32=35

function r(a){k=j=0;a.map(function z(i){i%2?j+=i:k+=i});return Math.sqrt(k*k+j*j)}


Doesn't seem like there is an easy way of spliting the array into even's and odd's without defining another function :(

console.log(r([20, 9, 4, 5, 5, 5, 15, 17, 20, 9]));
78.49203781276162


JAVA: 67-32=35

inside function(int[] a) :

int x=0,y=0;
for(int i:a)
{
if(i%2==0)
x+=i;
else y+=i;
}
Math.hypot(x,y);

• C'mon guys atleast tell why you've down-voted the answer. Its a working code. Feb 11, 2014 at 12:46
• I didn't downvote. But why i*i? I also get 1108.9891793881488 when I run your code with the sample input. Feb 11, 2014 at 17:33
• I thought we need to square them first,then add them.....ΣEven² is actually (ΣEven)², while I took it as Σ(Even²). but I should've checked with sample input earlier. Thanks @Danny. Feb 11, 2014 at 18:47
• If you're trying to make the code small, you can skip the curly braces and keep everything on one line. Feb 11, 2014 at 18:55

# Vyxalḋ, 7 bytes - 32 = score -25

⁽∷ġṠ²∑√


Try it Online!