Kind of meta: get the longest of the shortest answers
Perl, 195 bytes
while(<>){/(\S+)\t(\d+)\t(.+)/;push@{$a{$1}},$3if$2==$l{$1};$l{$1}=$2,$a{$1}=[$3]if $2<($l{$1}//65536)}$m=(sort{$b<=>$a}values%l)[0];map{$l=$_;map{print"$l\t$m\t$_\n"if$l{$l}==$m}@{$a{$l}}}keys%l
Input is expected in STDIN, result is written to STDOUT:
C# 208 http://short.url/answer/poi
C# 208 http://short.url/answer/yac
Java 208 http://short.url/answer/jav
Ungolfed version
#!/usr/bin/env perl
use strict;
$^W=1;
# hash %language remembers the minimum count for a language
# %language: <language> => <minimum count>
my %language;
# hash %array remembers the URLs for the minimum count of the language
# %array: <language> => [<url>, <url>, ....]
my %array;
while(<>){
# parse input line (no error checking)
/(\S+)\t(\d+)\t(.+)/;
my ($lang, $count, $url) = ($1, $2, $3);
# add URL, if the count is the current minimum for the language
if ($count == ($language{$lang}//0)) {
# better, but longer version:
# if (defined $language{$lang} and $count == $language{$lang}) {
push @{$array{$lang}}, $url;
}
# create a new entry for the language, if there is a new minimum
if ($count < ($language{$lang}//65536)) {
# better, but longer version:
# if (not defined $language{$lang} or $count < $language{$lang}) {
$language{$lang} = $count;
$array{$lang} = [$url];
}
}
# Sort the minimal values in numerical descending order and
# get the first entry as maximum.
my $maximum = (sort { $b <=> $a } values %language)[0];
# Loop over all URLs of minimal answers for the language,
# but print only the entries for the languages with the largest
# minima.
foreach my $lang (keys %language) {
foreach my $url (@{$array{$lang}}) {
if ($language{$lang} == $maximum) {
print "$lang\t$maximum\t$url\n";
}
}
}
__END__
Java - 556
import java.util.*;class G{public static void main(String[]x){TreeMap<?,TreeMap>m=new TreeMap();try{Scanner s=new Scanner(System.in);for(;;){String[]a=s.nextLine().split("\t");a(new Long(a[1]),a(a[0],m)).put(a[2],a);}}catch(Exception e){}TreeMap<?,Map<?,String[]>>n=new TreeMap();for(TreeMap o:m.values())a(o.firstEntry().getKey(),n).putAll((Map)o.firstEntry().getValue());for(String[]o:n.lastEntry().getValue().values())System.out.println(o[0]+"\t"+o[1]+"\t"+o[2]);}static<T>Map a(T t,Map m){if(m.get(t)==null)m.put(t,new TreeMap());return(Map)m.get(t);}}
Program will read from STDIN.
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
class G {
public static void main(String[] x) {
TreeMap<?, TreeMap> m = new TreeMap();
try {
Scanner s = new Scanner(System.in);
for (; ; ) {
String[] a = s.nextLine().split("\t");
a(new Long(a[1]), a(a[0], m)).put(a[2], a);
}
} catch (Exception e) {
}
TreeMap<?, Map<?, String[]>> n = new TreeMap();
for (TreeMap o : m.values())
a(o.firstEntry().getKey(), n).putAll((Map) o.firstEntry().getValue());
for (String[] o : n.lastEntry().getValue().values())
System.out.println(o[0] + "\t" + o[1] + "\t" + o[2]);
}
static <T> Map a(T t, Map m) {
if (m.get(t) == null)
m.put(t, new TreeMap());
return (Map) m.get(t);
}
}
- Programm will read line by line until an exception occours (either
ArrayIndexOutOfBoundsException
when a blank line is encountered orNoSuchElementException
if input ends without trailing new line). Each line read is added to theTreeMap m
, which could have been defined asTreeMap<String, TreeMap<Long, TreeMap<String,String[]>>>
(left-to-right: language, code-size, URL, input). - Then a result-
TreeSet<Long, TreeSet<String, String[]>> n
(left-to-right: code-size, URL, input) is build where the contents of every languagesfirstEntry()
are aggregated. lastEntry()
of the aggregatedTreeMap
contains our result - we only need to print it.
Try on ideone.com (switched last two lines of input to show that all lines are read)
Python 378 377 372
import sys
d=__import__("collections").defaultdict(list)
o={}
x=int
n="\n"
for i,l,u in[a.split()for a in sys.stdin.read().strip().split(n)]:d[i]+=[(l,u)]
for e,b in d.items():o[e]=[i for i in b if i[0]==str(min([x(i[0])for i in b]))]
print("".join(n.join("\t".join([u,s[0],s[1]])for s in y if x(s[0])==max(x(i[0][0])for i in o.values()))+n for u,y in o.items()).strip())
Input on the stdin:
C:\Users\gcq\Documents\python>type m.txt | python test.py
C# 208 http://short.url/answer/poi
C# 208 http://short.url/answer/yac
Java 208 http://short.url/answer/jav
And this is what i had before starting to compress it down, at 551 chars:
from collections import defaultdict
import sys
d = defaultdict(list)
for language, length, url in [a.split() for a in sys.stdin.read().strip().split("\n")]:
d[language].append((length, url))
o = {}
for language, data in d.items():
winval = data[0][0]
for i in data:
if int(i[0]) < int(winval):
winval = i[0]
o[language] = [i for i in data if i[0] == winval]
maxlen = max(int(i[0][0]) for i in o.values())
for language, dataa in o.items():
for data in dataa:
if int(data[0]) == maxlen:
print("\t".join([language, data[0], data[1]]))