munduruku un point de vue naif.

un blog sur ce qui m'intéresse, le cybermonde, la science, la politique.

mercredi, mai 21, 2008

Google Treasurehunt 2

tadam ... new question today :

Unzip the archive, then process the resulting files to obtain a numeric result. You'll be taking the sum of lines from files matching a certain description, and multiplying those sums together to obtain a final result. Note that files have many different extensions, like '.pdf' and '.js', but all are plain text files containing a small number of lines of text.

Sum of line 5 for all files with path or name containing def and ending in .xml
Sum of line 2 for all files with path or name containing ghi and ending in .txt
Hint: If the requested line does not exist, do not increment the sum.

Multiply all the above sums together and enter the product below.


Well, as I said before, I'm gonna try to do it with my single ubuntu laptop, and the net, and my brain.

well, first this seems to be a job for awk, and regular expression :

First get the list of all files, using a regular expression to match the requirement

find -regex .*def.*xml
then pipe it to an awk that for each line (that is a file name) prints it, with the line number

awk '{system("cat -n "$0)}'
And then, make a good old awk to get the line number 5 (here) and print the second column

awk 'BEGIN{s=0}/ 5/ {s+=$2} END{print s}'
All in one it makes

find -regex .*def.*xml|awk '{system("cat -n "$0)}'|awk 'BEGIN{s=0}/ 5/ {s+=$2} END{printf "http://gmplib.org/cgi-bin/gmp_calc.pl?expr="s"*">"mund"}'
find -regex .*ghi.*txt|awk '{system("cat -n "$0)}'|awk 'BEGIN{s=0}/ 2/ {s+=$2;} END{print s>>"mund"}'
wget -q -O - -i mund

I've first tried to make the multiplication in awk directely, but the number is too big, and awk return a floating point value. So I've build an url to perform the multiplication using GMPLib.
the URL is built in a file called mund, and wget is used to retrieve the value.


89383*25647



89 383 * 25 647 = 2 292 405 801


that's it.

The previous question required some math skills, but the solution was elegant, some nice website, and that's it.

This one is much more tricky, and require much more coding skills. I've fulfilled my duty, I 've done it, in my laptop only, but, I'm glad to run ubuntu, 'cause on a window system, this would have be, MUCH more difficult.
I'm feeling like missing something. I'm not very familiar with awk, but I'm still finding the "system("cat -n "$0)" ugly. Do you ?

Edit : looking around in the web I've found this nice improvement
find -regex .*def.*xml|xargs -n 1 sed -n '5p'|awk '{s+=$0}END{print s}'

Libellés : ,

Google Treasurehunt

You know about Google Treasure Hunt ?

As a programmer (java, C etc.) I've got amazing tools available in my working computer. This would be unfare, and not fun to go that fight using all those weapons.

That the reason why, I've decided to play this game, using nothing but my personal laptop (running ubuntu), and the net... and the skills I can't leave at the office.

Let's try it.

The first question is/was to get the number of path from the topleft point of a n,m grid to the bottomright. Beeing allowed to move right or down, only.

This is a so easy "mathematical" exercice, that I've made a mistake at the first time ;-)

First the math : one can code a single path like that ( rddrrrddr), where 'r' represent 'right' and 'd' 'down'.
  1. First, notice that, as the grid is n width, there is N-1 'r' (on a 2-width grid there is only one step right to do) (this was my silly mistake, ugly trees).It's the same for the height.
  2. second notice that the whole path is always (n-1)+(m-1) moves length.
Well, sorry for non math aware readers, but the number of such path is well known to be the number of combinations. i.e (n-1) choose (n-1+m-1). (means that you choose where to put the 'r' somewhere in the whole path, all others moves are 'd'.

Haha, got you, google, too easy !

Mine was a 60x35 grid so there was 34 'r' to choose among (60+35-2)=93

let's try it on google calc : 93 choose 34 , and ... damit :

93 choose 34 = 2.82526868 × 1025


but they wanted an exact integer.... Come on, google, next time try to provide a game that does not defeat your own tools!

well, let's think a little : the number is huge but not so much,I need what we call biginteger in Java, or arbitrary precision arithmetic. A glance at google results, a fast jump to GNU related tools, and here I'am, at gmplib :

there is an online tool, too cool. But they do not have the choose function. It doesn't matter, this is a nuclear weapon to kill an ant : choose function (as given in wikipedia) gives
(59+34)!/(59!*34!) :28252686811008134325892860

nice shot ! got the right answer.

And what about you ?

Libellés : ,