# Find top student from three test marks

First save the following text into a file called `marks.csv`

``````Smith,6,9,24
Jones,9,12,20
Fred,5,9,20
``````

Each line contains a students name and three marks they scored. Find the top student by simply summing the three marks and then output their name. In this case the output is:

``````Jones
``````

I would like to see somebody beat my Python solution but any language will be allowed :) If anybody beats my Python solution I will accept their answer but for now I will just accept the shortest code.

-

# K, 37

``````m[0;*>+/1_m:("SIII";",")0:`marks.csv]
``````
-

## Cat (5 characters)

``````Jones
``````

You don't specify that the code has to work on other datasets as well ;-)

Save code in a file `FILE` and execute with:

``````cat FILE
``````
-
+1 WINNER! Next task plz. – Ev_genus Jul 27 '12 at 12:39
Doesn't follow the requirement: `Find the top student by simply summing the three marks *and then* output their name` – SpellingD Jul 27 '12 at 17:45
Don't you see the part right before the word Jones? It does all the calculations. – FUZxxl Jul 27 '12 at 18:29
In this case implies other input is possible. -1 – Mr.Wizard Jul 29 '12 at 12:47

## Mathematica, 44

``````SortBy[Tr/@Import@"marks.csv",First][[-1,2]]
``````

If I were allowed to save the data in Mathematica's native format in a file named `z` I could use:

``````SortBy[Tr/@<<z,First][[-1,2]]
``````
-
 Your answer appears to be problematic. Try running: `Tr /@ Import@"marks.csv"`. I get `{"{6, 9, 24}" + "Smith", "{9, 12, 20}" + "Jones", "{5, 9, 20}" + "Fred"}` as output. – David Carraher Jul 29 '12 at 14:18 @David I think your file is in the wrong format. It should contain exactly the text shown in the first code block in the question. – Mr.Wizard Jul 29 '12 at 14:38 Would you kindly show me the exact code you are using for exporting the file in the first place. Somehow, my integer lists are interpreted as strings. – David Carraher Jul 29 '12 at 15:06 @David I didn't `Export` the file; I used Notepad and pasted the text from the question into it. – Mr.Wizard Jul 29 '12 at 15:41

## Ruby 1.9 (71 62)

``````\$><<open('marks.csv').max_by{|l|eval l[/,.*/].tr ?,,?+}[/\w+/]
``````
-

PHP, 103 95

I know PHP is not the best tool for this problem, however I would appreciate any tip to improve my answer :)

First version (103)

``````<?\$h=fopen('marks.csv','r');for(;\$f=fgetcsv(\$h);\$m[\$f[0]]=array_sum(\$f));echo array_search(max(\$m),\$m);
``````

Improved version, inspired by @Leigh's solution (95) - considering low error level:

``````<?\$h=fopen('marks.csv',r);for(;\$f=fgetcsv(\$h);\$m[array_sum(\$f)]=\$f[0]);ksort(\$m);echo end(\$m);
``````

``````<?
\$h=fopen('marks.csv',r);
for(; \$f=fgetcsv(\$h); \$m[\$f[0]]=array_sum(\$f));
echo array_search(max(\$m),\$m);
``````

Thanks

-
 I was just about to post a new solution based on yours ;) - Except I had moved the fopen into the first part of the for loop. FYI I started the results array the same as yours, with arsort + key. Then I reversed the results and used ksort + end. Every little helps :) – Leigh Jul 27 '12 at 12:24

## PHP, 97 93

Original

``````foreach(file('marks.csv')as\$l){\$x=str_getcsv(\$l);\$r[array_sum(\$x)]=\$x[0];}ksort(\$r);echo end(\$r);
``````

Inspired by milo5b

``````for(\$f=fopen('marks.csv','r');\$l=fgetcsv(\$f);)\$r[array_sum(\$l)]=\$l[0];ksort(\$r);echo end(\$r);
``````

And how to run without tags.

`cat golf.php | php -a`

-
You would also need starting <? php tag. And by the way, you inspired an improvement to my solution, thanks eheh +1 – milo5b Jul 27 '12 at 12:23
Don't need the tags, and thanks to you too ;) – Leigh Jul 27 '12 at 12:32
eheh great I did not know this. I will not edit my solution further or we'll end up with exactly the same code! – milo5b Jul 27 '12 at 12:35
btw i think you forgot to take off the string quotes around 'r' in fopen – milo5b Jul 27 '12 at 12:38
I'll let you keep that one, or we'll end up with the same code ;) – Leigh Jul 27 '12 at 12:39
show 1 more comment

# Mathematica 5949 53 chars

``````Sort[{Tr@Rest@#, #[[1]]} & /@ Import@"marks.csv"][[-1, 2]]
``````

Because csv is a subject to various interpretations, I'm including the exact file contents of o.csv:

``````{{"Smith", 6, 9, 24}, {"Jones", 9, 12, 20}, {"Fred", 5, 9, 20}}
``````
-
 I missed the filename requirement, which is now taken into account. – David Carraher Jul 29 '12 at 14:08 fix taken into account ;-) +1 – Mr.Wizard Jul 29 '12 at 14:11 @Mr.Wizard It turns out that `%` will work as the file name, provided that it is executed right after exporting the file. – David Carraher Jul 29 '12 at 14:41 I like your ambition, but honestly I don't think that's fair: the export becomes part of your program. – Mr.Wizard Jul 29 '12 at 14:47 I rolled back to an earlier version for another reason. I seem to have been getting the right answer for the wrong reason. – David Carraher Jul 29 '12 at 14:57

## Python, 82

``````print max((sum(eval(m)),n)for n,m in(l.split(',',1)for l in open("marks.csv")))[1]
``````
-

## R, 58

``````j=read.csv("marks.csv",F);j[which.max(rowSums(j[,2:4])),1]
``````

Alternatively (62 characters):

``````j=read.csv("marks.csv",F);j[which.max(apply(j[,2:4],1,sum)),1]
``````
-

Awk, 64

``````\$ awk -F, '{s=\$2+\$3+\$4;if(s>m){m=s;n=\$1}}END{print n}' marks.csv
``````
-
`{s=\$2+\$3+\$4}s>m{m=s;n=\$1}END{print n}` is shorter - 30 chars – Prince John Wesley Jul 27 '12 at 12:12

# k (47 chars)

Allows for ties

``````(*x)'&(|/i)=i:+/'+:1_x:("SIII";",")0:`marks.csv
``````
-

This might be orthodox and first of it kind on Code golf. Below will work find under circumstances where CSV file - been imported into as a table ( External Table) on Oracle.

M := Marks.csv Table a-d := CSV Fields

PLSQL 60 Characters

``````SELECT a,max(b+c+d) FROM M WHERE ROWNUM<=1 GROUP BY a,b,c,d;
``````
-

C# Two different versions

Version:1 245 Characters

``````using System;using System.Linq;namespace X{class Y{static void Main(){Console.Write((from l in System.IO.File.ReadAllLines("m.csv")let x =l.Split(',')select new{f=x[0],a=x.Skip(1).Sum(Z=>Int32.Parse(Z))}).OrderByDescending(p=>p.a).First().f);}}}
``````

Version:2 214 Characters

``````using System;using System.Linq;namespace X{class Y{static void Main(){Console.Write(System.IO.File.ReadAllLines("m.csv").OrderByDescending(z=>z.Split(',').Skip(1).Sum(y=>Int32.Parse(y))).First().Split(',')[0]);}}}
``````
-