Google Code Jam Qualifier in one line of Bash
So a coworker of mine, Lucas Fernando Amorim (http://www.lfamorim.com/), went to Google and participated on a development challenge to enter Google Code Jam (http://code.google.com/codejam/), the assignment wasn’t very difficult.
You had to develop a generator that would spit N random numbers to stdout and another program that would receive the numbers list on stdin and return the number wich had the most ocurrences to stdout.
You could use any language you wanted. After he said 3 people couldn’t and some people implemented it in such a way that the program took upwards to 15 minutes to run, I thought “what the hell… this ain’t that hard how come people couldn’t finish this!” and started coding it on Perl, that I know would be quite easy and fun, but since it’s been more than three years I’ve coded in Perl, it wasn’t as fast as I thought it would be.
I got it working quickly, but once I tuned the generated numbers to 100000 (the quantity Google asked), things started breaking, the program had 5 lines of code and ran quite fast, but the results were strange. I spent half an hour trying to correct it before I gave up.
On the next morning I decided to do it on Bash, researched a little and in 10 minutes I had the solution:
awk ‘BEGIN{ print 100000;for (i=1;i<=100000;i++) print int(100000*rand())}’| sort | uniq -c| sort -r | head -n1 | awk ‘{print $2}’
Works perfectly and runs in a little bit more than a second. =]

