This particular question was easy yet many people failed to do this correctly. So that motivated me to upload my solution on this blog.
You can go and visit http://www.facebook.com/hackercup/ for solutions online of other participants.
As Said:
The Facebook Hacker Cup is an annual worldwide programming competition where hackers compete against each other for fame, fortune, glory and a shot at the coveted Hacker Cup.
#/usr/bin/perl -e
##############
#Puneet Singh#
##############
use POSIX;
open(MYINPUTFILE, "<$ARGV[0]");
@text=<MYINPUTFILE>;
$T= shift(@text);
$i=1;
foreach (@text)
{
$line=$_;
$H=0;
$H = ($line =~ tr/H//);
$A = ($line =~ tr/A//);
$C = ($line =~ tr/C//);
$K = ($line =~ tr/K//);
$E = ($line =~ tr/E//);
$R = ($line =~ tr/R//);
$U = ($line =~ tr/U//);
$P = ($line =~ tr/P//);
$C=int($C/2);
$final=min($H,$A);
$final=min($final,$C);
$final=min($final,$K);
$final=min($final,$E);
$final=min($final,$R);
$final=min($final,$U);
$final=min($final,$P);
print "Case #$i: ".$final."\n";
$i++;
}
sub min{
if($_[0]<$_[1])
{
return $_[0];
}
else
{
return $_[1];
}
}
I hope you will understand this code all by yourself. I wrote it in perl(for the ease). If your find any difficulty you can write it in comments.
A very straight forward solution. Be happy that the input text is limited because you are searching 8-times for a character instead sorting the wanted character into a hashmap.
ReplyDeletehttp://united-coders.com/christian-harms/facebook-hacker-cup-2012-qualification-round-alphabet-soup
Yes simple and easy to code for small set.
ReplyDeleteIf input type would be given very large I would have hashed all in one go. ;)
BTW I checked it for a big wiki article gives result within one second. But obviously it is not an awesome approach.