3
\$\begingroup\$

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.

\$\endgroup\$
0

14 Answers 14

4
\$\begingroup\$

K, 37

m[0;*>+/1_m:("SIII";",")0:`marks.csv]
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
3
  • 2
    \$\begingroup\$ Doesn't follow the requirement: Find the top student by simply summing the three marks *and then* output their name \$\endgroup\$
    – SpellingD
    Jul 27, 2012 at 17:45
  • 2
    \$\begingroup\$ Don't you see the part right before the word Jones? It does all the calculations. \$\endgroup\$
    – FUZxxl
    Jul 27, 2012 at 18:29
  • 1
    \$\begingroup\$ In this case implies other input is possible. -1 \$\endgroup\$
    – Mr.Wizard
    Jul 29, 2012 at 12:47
2
\$\begingroup\$

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]]
\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$
    – DavidC
    Jul 29, 2012 at 14:18
  • \$\begingroup\$ @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. \$\endgroup\$
    – Mr.Wizard
    Jul 29, 2012 at 14:38
  • \$\begingroup\$ 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. \$\endgroup\$
    – DavidC
    Jul 29, 2012 at 15:06
  • \$\begingroup\$ @David I didn't Export the file; I used Notepad and pasted the text from the question into it. \$\endgroup\$
    – Mr.Wizard
    Jul 29, 2012 at 15:41
2
\$\begingroup\$

Ruby 1.9 (71 62)

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

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

Easier to read version:

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

Thanks

\$\endgroup\$
1
  • \$\begingroup\$ 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 :) \$\endgroup\$
    – Leigh
    Jul 27, 2012 at 12:24
1
\$\begingroup\$

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

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

Mathematica 59 49 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}}
\$\endgroup\$
5
  • \$\begingroup\$ I missed the filename requirement, which is now taken into account. \$\endgroup\$
    – DavidC
    Jul 29, 2012 at 14:08
  • \$\begingroup\$ fix taken into account ;-) +1 \$\endgroup\$
    – Mr.Wizard
    Jul 29, 2012 at 14:11
  • \$\begingroup\$ @Mr.Wizard It turns out that % will work as the file name, provided that it is executed right after exporting the file. \$\endgroup\$
    – DavidC
    Jul 29, 2012 at 14:41
  • \$\begingroup\$ I like your ambition, but honestly I don't think that's fair: the export becomes part of your program. \$\endgroup\$
    – Mr.Wizard
    Jul 29, 2012 at 14:47
  • \$\begingroup\$ I rolled back to an earlier version for another reason. I seem to have been getting the right answer for the wrong reason. \$\endgroup\$
    – DavidC
    Jul 29, 2012 at 14:57
1
\$\begingroup\$

JavaScript (Node.js), 146 134 133 bytes

(feedback welcome)

s=a=>eval(a.split`,`.slice`1`.join`+`);_=>(""+require("fs").readFileSync("marks.csv")).split`
`.sort((a,b)=>s(b)-s(a))[0].split`,`[0]

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python, 82

print max((sum(eval(m)),n)for n,m in(l.split(',',1)for l in open("marks.csv")))[1]
\$\endgroup\$
0
\$\begingroup\$

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]
\$\endgroup\$
0
\$\begingroup\$

Awk, 64

$ awk -F, '{s=$2+$3+$4;if(s>m){m=s;n=$1}}END{print n}' marks.csv
\$\endgroup\$
1
  • 4
    \$\begingroup\$ {s=$2+$3+$4}s>m{m=s;n=$1}END{print n} is shorter - 30 chars \$\endgroup\$ Jul 27, 2012 at 12:12
0
\$\begingroup\$

k (47 chars)

Allows for ties

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

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;
\$\endgroup\$
0
\$\begingroup\$

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]);}}}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.