13
\$\begingroup\$

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   │
└──────┴─────────────────┴──────────────┴─────────┴────────┴───────┘
\$\endgroup\$
  • 8
    \$\begingroup\$ Why does the lowest score need a bonus, they already won? \$\endgroup\$ – gggg Feb 5 '14 at 20:02
  • 6
    \$\begingroup\$ Moreover, the offset of 46 doesn't change anything regarding the order. \$\endgroup\$ – Howard Feb 5 '14 at 20:06
  • \$\begingroup\$ @gggg I think they mean they'll give the lowest scoring answer a +50 bounty. \$\endgroup\$ – user8777 Feb 5 '14 at 23:27
  • 1
    \$\begingroup\$ @gggg: I will actually start a bounty as soon as I am allowed to. So that's what I meant by bonus. \$\endgroup\$ – Abhijit Feb 6 '14 at 2:01
  • 1
    \$\begingroup\$ 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! \$\endgroup\$ – Noyo Feb 17 '14 at 16:49

38 Answers 38

1
2
0
\$\begingroup\$

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);
        }

    }
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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.

Actual answer:

o=sum a.filter odd
e=sum a.filter even
(e*e+o*o)^0.5
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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);
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ C'mon guys atleast tell why you've down-voted the answer. Its a working code. \$\endgroup\$ – Sumedh Feb 11 '14 at 12:46
  • \$\begingroup\$ I didn't downvote. But why i*i? I also get 1108.9891793881488 when I run your code with the sample input. \$\endgroup\$ – Danny Feb 11 '14 at 17:33
  • \$\begingroup\$ 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. \$\endgroup\$ – Sumedh Feb 11 '14 at 18:47
  • \$\begingroup\$ If you're trying to make the code small, you can skip the curly braces and keep everything on one line. \$\endgroup\$ – daniero Feb 11 '14 at 18:55
0
\$\begingroup\$

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;
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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}

Link to software: http://ised.sourceforge.net/

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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!

| improve this answer | |
\$\endgroup\$
1
2

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.