<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>be e.nigma</title>
	<atom:link href="http://e.nigma.be/feed/" rel="self" type="application/rss+xml" />
	<link>http://e.nigma.be</link>
	<description>random rumble, humble mumble</description>
	<lastBuildDate>Mon, 06 Dec 2010 20:11:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>What happened?</title>
		<link>http://e.nigma.be/2010/12/06/what-happened/</link>
		<comments>http://e.nigma.be/2010/12/06/what-happened/#comments</comments>
		<pubDate>Mon, 06 Dec 2010 20:11:36 +0000</pubDate>
		<dc:creator>Andrea</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://e.nigma.be/?p=39</guid>
		<description><![CDATA[Dataloss makes me bored, not anymore. That would be the six words version of the story. I had problems with my host and didn&#8217;t have the latest backup of the blog, but I discovered it only after putting everything back online on another host. Other than that there was to fix all the 3rd domain [...]]]></description>
			<content:encoded><![CDATA[<p>Dataloss makes me bored, not anymore.</p>
<p>That would be the <a title="Six Word Stories" href="http://www.sixwordstories.net/">six words</a> version of the story. I had problems with my host and didn&#8217;t have the latest backup of the blog, but I discovered it only after putting everything back online on another host. Other than that there was to fix all the 3rd domain names, upload images and build again a theme: it pissed me off for a while.</p>
<p>I left the blog online as a zombie, there are few posts left and I plan to leave them there. I&#8217;m starting to polish the blog starting with the theme and going to post some interesting stuff in the near future, this could be a fresh<sup><a href="http://e.nigma.be/2010/12/06/what-happened/#footnote_0_39" id="identifier_0_39" class="footnote-link footnote-identifier-link" title="Let&amp;#8217;s say almost fresh. Did you see? I installed WP-Footnotes too.">1</a></sup> start.</p>
<p>Let&#8217;s see if any old feed reader pops out to say hello!</p>
<ol class="footnotes"><li id="footnote_0_39" class="footnote">Let&#8217;s say <i>almost</i> fresh. Did you see? I installed WP-Footnotes too.</li></ol>]]></content:encoded>
			<wfw:commentRss>http://e.nigma.be/2010/12/06/what-happened/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A geeky toy, for a geeky boy.</title>
		<link>http://e.nigma.be/2009/03/03/a-geeky-toy-for-a-geeky-boy/</link>
		<comments>http://e.nigma.be/2009/03/03/a-geeky-toy-for-a-geeky-boy/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 22:21:54 +0000</pubDate>
		<dc:creator>Andrea</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">http://e.nigma.be/?p=29</guid>
		<description><![CDATA[I&#8217;m sure all of you know what the rubik&#8217;s cube is, if you don&#8217;t&#8230; I mean, really? man what was you thinking until now? Well If you REALLY don&#8217;t know it then you should at least google for it. Wikipedia will work as well. I always thought it was a logic puzzle, and after some [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure all of you know what the rubik&#8217;s cube is, if you don&#8217;t&#8230; I mean, really? man what was you thinking until now? Well If you REALLY don&#8217;t know it then you should at least google for it. Wikipedia will work as well.</p>
<p><img src="http://img125.imageshack.us/img125/7224/rubikcube.png" border="0" alt="rubik's cube" width="300" height="300" /></p>
<p>I always thought it was a logic puzzle, and after some unsuccessful tries I decided to <em>cheat</em>, googled for &#8220;rubik&#8217;s cube solution&#8221; and I found out a beautiful site, which explains everything you need to solve the cube in a very simple and fast way, it is of course the <a href="http://www.lar5.com/cube/">Lars Petrus</a> method.</p>
<p>Once learnt a solution method there is no more &#8220;logic&#8221; in it. Where is the fun part? What&#8217;s the challenge?<br />
I was terribly wrong. It is not a <em>logic</em>-puzzle, it is a <strong>memory</strong>-puzzle, and a <strong>hand-dexterity</strong>-puzzle.</p>
<p>You need to remember the moves of your solving methods, they are almost always the same (in the petrus method there are 4 basic moves), but the most challenging part is <strong>how fast can you solve the cube</strong>?</p>
<p>Hey &#8220;<em>how-fast</em>&#8221; sounds damn challenging, how can you measure that? You&#8217;ll need a stopwatch. If you are at least half geeky as I am you&#8217;d be already on google at the moment and starting to type something like &#8220;online stopwatch&#8221; or &#8220;online timer&#8221;.</p>
<p>There are tons of them, but all have a very annoying interface that make you use the mouse. Given that you are a very good speedcuber you don&#8217;t want to loose 0.3s to start to solve and an other 0.3s to stop the timer, do you?</p>
<p>Well I wouldn&#8217;t care as I need 1&#8217;30&#8221;+ to solve the cube, but I found fun to code a stopwatch designed to work with Miss Spacebar instead of your loveable mice.</p>
<p><a href="http://rubik.nigma.be/"><img class="alignnone" src="http://img255.imageshack.us/img255/7697/rubiknigmatimer.png" border="0" alt="nigma timer" width="394" height="191" /></a></p>
<p>the simple <a href="http://rubik.nigma.be/">nigma timer</a> is written in simple html/css/js and as a very simple interface, I hope you&#8217;ll enjoy it.</p>
<p>return &#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://e.nigma.be/2009/03/03/a-geeky-toy-for-a-geeky-boy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to convert Microsoft Word .doc files to PDF from command line</title>
		<link>http://e.nigma.be/2009/01/14/how-to-convert-microsoft-word-doc-files-to-pdf-from-command-line/</link>
		<comments>http://e.nigma.be/2009/01/14/how-to-convert-microsoft-word-doc-files-to-pdf-from-command-line/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 23:41:50 +0000</pubDate>
		<dc:creator>Andrea</dc:creator>
				<category><![CDATA[bash scripting]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[commandline]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[openoffice]]></category>
		<category><![CDATA[pdf]]></category>

		<guid isPermaLink="false">http://e.nigma.be/?p=27</guid>
		<description><![CDATA[I know lot of people need it, Google is full of requests by hundred, maybe thousands of users asking for a doc2pdf converter or this kind of thing. I need it too. It is useful to have all files in pdf format (and maybe all merged in one file only) and if you have a [...]]]></description>
			<content:encoded><![CDATA[<p>I know lot of people need it, Google is full of requests by hundred, maybe thousands of users asking for a doc2pdf converter or this kind of thing. I need it too. It is useful to have all files in pdf format (and maybe all merged in one file only) and if you have <em>a lot</em> of files to convert by hand, believe me, you&#8217;re not going to have a nice day.</p>
<h2>The easy way</h2>
<p>It is pretty easy:</p>

<div class="wp_syntax"><div class="code"><pre class="j" style="font-family:monospace;">$ abiword --to=pdf filename.doc</pre></div></div>

<p>I don&#8217;t think there is so much to explain here. It converts filename.doc to filename.pdf and saves it in the current directory. It was too easy. Why should you need an hard way? I don&#8217;t know, I&#8217;m sure I need one.  Unfortunately abiword&#8217;s Microsoft doc file support is not so good, in fact it lacks of the math and image/clipart features. I&#8217;m not sure if this affects all versions of abiword but it is sure for the one that comes with ubuntu (actually it doesn&#8217;t come with it, you&#8217;ve to <em>apt-get install</em> it). </p>
<p>Anyway I really need to see plots and formulas. What you said? <em>OpenOffice supports them. Check it out.</em> Yes I know that, OpenOffice can read <em>almost</em> always plots and images in doc files. Bad luck seems to be here again, OpenOffice lacks of the same command line interface abiword has, so the only way is to open doc files one by one and click on the <em>Export as PDF</em> button. It is very frustrating. So, here is the hard way.</p>
<h2>The hard way</h2>
<p><strong>Short version</strong> (for whom doesn&#8217;t like read me be but want to read so much): check the <a title="Python-UNO" href="http://udk.openoffice.org/python/python-bridge.html#examples">Python-UNO</a> site.  </p>
<p><strong>Long version</strong>. You need to know what  <a title="Python-UNO" href="http://udk.openoffice.org/python/python-bridge.html">Python-UNO</a> is</p>
<blockquote><p>The Python-UNO bridge allows to</p>
<ul>
<li> use the standard OpenOffice.org API from the well known python scripting language.</li>
<li> to develop UNO components in python, thus python UNO components may be run within the OpenOffice.org process and can be called from Java, C++ or the built in StarBasic scripting language.</li>
<li> <a href="http://udk.openoffice.org/python/scriptingframework/index.html">create and invoke scripts</a> with the office scripting framework (OOo 2.0 and later).</li>
</ul>
<p>You can find the most current version of this document from <a href="http://udk.openoffice.org/python/python-bridge.html"> http://udk.openoffice.org/python/python-bridge.html</a></p></blockquote>
<p><em>Oh no! I&#8217;ll have to download this Python-UNO, read manuals to learn how to use those API and who knows if it&#8217;ll work&#8230;</em>&#8230; No. Just <a href="http://en.wikipedia.org/wiki/The_Answer_to_Life,_the_Universe,_and_Everything#Don.27t_Panic">don&#8217;t panic</a>. I&#8217;m going to tell you something that will make this a <em>not-so-hard</em> way.  The first thing is that if you have installed OpenOffice you&#8217;re at 50% of the work, in fact Pyhton-UNO comes with OpenOffice since version 1.1.</p>
<ul>
<li>Pyhton-UNO comes with OpenOffice since version 1.1. You don&#8217;t have to download and install anything</li>
<li>Pyhton-UNO&#8217;s guys are so cool that in their <a title="Python-UNO code examples" href="http://udk.openoffice.org/python/python-bridge.html#examples">code examples</a> there is <strong>all of what we need</strong>.</li>
</ul>
<p>From the examples page you can download the <a title="ooextract.py" href="http://udk.openoffice.org/python/samples/ooextract.py">ooextract.py</a> script. It has a very simple usage, we need to use it in this way:</p>
<pre style="font-size:1.4em;background:#EEEEEE;border:1px">$ openoffice -invisible "-accept=socket,host=localhost,port=2002;urp;"
$ python ooextract.py --pdf filename.doc</pre>
<p>The result is almost the same of the one of the easy way but this will use OpenOffice for the conversion, so it will do it better. You also may like to write a little shell script to automate the conversion of a bunch of files, so there it is a very simple version:</p>
<pre style="font-size:1.4em;background:#EEEEEE;border:1px">#!/bin/bash

openoffice -invisible "-accept=socket,host=localhost,port=2002;urp;"
for i in *.doc; do
	python ooextract.py --pdf $i
done</pre>
<p>Remember to kill OpenOffice when it ends <img src='http://e.nigma.be/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> ) OpenOffice has now batteries included.</p>
]]></content:encoded>
			<wfw:commentRss>http://e.nigma.be/2009/01/14/how-to-convert-microsoft-word-doc-files-to-pdf-from-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Chrome and proxies</title>
		<link>http://e.nigma.be/2008/11/11/google-chrome-and-proxies/</link>
		<comments>http://e.nigma.be/2008/11/11/google-chrome-and-proxies/#comments</comments>
		<pubDate>Mon, 10 Nov 2008 23:41:45 +0000</pubDate>
		<dc:creator>Andrea</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[google chrome]]></category>

		<guid isPermaLink="false">http://e.nigma.be/?p=26</guid>
		<description><![CDATA[I&#8217;m paranoid. I don&#8217;t want my family be able to use Internet Explorer, at least not from my computer! To be sure they don&#8217;t I did one simple thing: set up internet explorer proxy to 1.1.1.1:1 :^D Unfortunately Chrome won&#8217;t work, just like IE, this is because of Chrome and IE share the same proxy [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m paranoid. I don&#8217;t want my family be able to use Internet Explorer, at least not from my computer! To be sure they don&#8217;t I did one simple thing: set up internet explorer proxy to 1.1.1.1:1 :^D</p>
<p>Unfortunately Chrome won&#8217;t work, just like IE, this is because of Chrome and IE share the same proxy configuration. That&#8217;s not so good for me, because I like to surf with firefox AND chrome (most of times to see differences between them). My question was simple: <strong>how can I set up a different proxy (none in this case) for Chrome and IE?</strong></p>
<p>Chrome accept proxy settings as command line parameters, so you can change the shortcut from something like <code>"C:\Documents and Settings\usr\Local Data\Application Data\Google\Chrome\Application\chrome.exe"</code></p>
<p>to: <code>"C:\Documents and Settings\usr\Local Data\Application Data\Google\Chrome\Application\chrome.exe" -proxy-server=</code></p>
<p>that means &#8220;do not use proxy at all&#8221; :^D</p>
]]></content:encoded>
			<wfw:commentRss>http://e.nigma.be/2008/11/11/google-chrome-and-proxies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GCJ &#8211; Practice Contest &#8211; Old Magician</title>
		<link>http://e.nigma.be/2008/10/13/gcj-practice-contest-old-magician/</link>
		<comments>http://e.nigma.be/2008/10/13/gcj-practice-contest-old-magician/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 19:56:18 +0000</pubDate>
		<dc:creator>Andrea</dc:creator>
				<category><![CDATA[GCJ 2008]]></category>
		<category><![CDATA[Google Code Jam]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[gcj]]></category>

		<guid isPermaLink="false">http://e.nigma.be/?p=25</guid>
		<description><![CDATA[Wow, an other practice contest came out :^D here&#8217;s the first problem, very simple solution :^) from __future__ import with_statement &#160; def solve&#40;w,b&#41;: return &#34;BLACK&#34; if b%2==1 else &#34;WHITE&#34; &#160; def eatFile&#40;fin, fout&#41;: with file&#40;fin,'r'&#41; as f1: N = int&#40;f1.readline&#40;&#41;.replace&#40;&#34;\n&#34;,''&#41;&#41; with file&#40;fout,'w'&#41; as f2: for case in xrange&#40;1,N+1&#41;: w,b = &#91;int&#40;i&#41; for i in f1.readline&#40;&#41;.replace&#40;&#34;\n&#34;,''&#41;.split&#40;' [...]]]></description>
			<content:encoded><![CDATA[<p>Wow, an other practice contest came out :^D here&#8217;s the first problem, very simple solution :^)</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">__future__</span> <span style="color: #ff7700;font-weight:bold;">import</span> with_statement
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> solve<span style="color: black;">&#40;</span>w,b<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;BLACK&quot;</span> <span style="color: #ff7700;font-weight:bold;">if</span> b<span style="color: #66cc66;">%</span>2==<span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #483d8b;">&quot;WHITE&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> eatFile<span style="color: black;">&#40;</span>fin, fout<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #008000;">file</span><span style="color: black;">&#40;</span>fin,<span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> f1:
        N = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>f1.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,<span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #008000;">file</span><span style="color: black;">&#40;</span>fout,<span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> f2:
            <span style="color: #ff7700;font-weight:bold;">for</span> case <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,N+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
                w,b = <span style="color: black;">&#91;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> f1.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,<span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' '</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
                s = solve<span style="color: black;">&#40;</span>w,b<span style="color: black;">&#41;</span>
                <span style="color: #808080; font-style: italic;">#print &quot;Case #%d: %s&quot; % (case, s)</span>
                f2.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Case #%d: %s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>case, s<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
eatFile<span style="color: black;">&#40;</span><span style="color: #483d8b;">'A-large-practice.in'</span>,<span style="color: #483d8b;">'A-large-practice.out.txt'</span><span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://e.nigma.be/2008/10/13/gcj-practice-contest-old-magician/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GCJ 2008 &#8211; Round 1A: Minimum Scalar Product</title>
		<link>http://e.nigma.be/2008/08/05/gcj-2008-round-1a-minimum-scalar-product/</link>
		<comments>http://e.nigma.be/2008/08/05/gcj-2008-round-1a-minimum-scalar-product/#comments</comments>
		<pubDate>Tue, 05 Aug 2008 21:19:46 +0000</pubDate>
		<dc:creator>Andrea</dc:creator>
				<category><![CDATA[GCJ 2008]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[gcj]]></category>
		<category><![CDATA[Google Code Jam]]></category>

		<guid isPermaLink="false">http://e.nigma.be/?p=24</guid>
		<description><![CDATA[This one was really really really too simple. The easiest problem of round 1. Unfortunately I didn&#8217;t took part at 1A, I did 1B and 1C and I did not pass :^( Anyway I&#8217;ll keep doing those problem just to have some fun :^) def solve&#40;a,b&#41;: a.sort&#40;&#41; b.sort&#40;reverse = True&#41; return sum&#40;&#91;i&#91;0&#93;*i&#91;1&#93; for i in [...]]]></description>
			<content:encoded><![CDATA[<p>This one was really really really too simple. The easiest problem of round 1. Unfortunately I didn&#8217;t took part at 1A, I did 1B and 1C and I did not pass :^( Anyway I&#8217;ll keep doing those problem just to have some fun :^)</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> solve<span style="color: black;">&#40;</span>a,b<span style="color: black;">&#41;</span>:
    a.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    b.<span style="color: black;">sort</span><span style="color: black;">&#40;</span>reverse = <span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>i<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span>i<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">zip</span><span style="color: black;">&#40;</span>a,b<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>You have to pass <strong>a</strong> and <strong>b</strong> as lists, or if you prefer vectors&#8230; I think there is no more to comment here&#8230; I did it just thinking about it and I find the solution out in about 3 minutes, but I discovered that mathematicians already thought about that and gave it a name :^D</p>
<p>You can search on wikipedia for <a href="http://en.wikipedia.org/wiki/Rearrangement_inequality">Rearrangement inequality</a>. I mean, 4 lines of fully readable python&#8230; it was quite simple, wasn&#8217;t it?</p>
<p>return YAWN;</p>
]]></content:encoded>
			<wfw:commentRss>http://e.nigma.be/2008/08/05/gcj-2008-round-1a-minimum-scalar-product/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Symfony without ssh and pear</title>
		<link>http://e.nigma.be/2008/08/04/symfony-without-ssh-and-pear/</link>
		<comments>http://e.nigma.be/2008/08/04/symfony-without-ssh-and-pear/#comments</comments>
		<pubDate>Sun, 03 Aug 2008 22:23:21 +0000</pubDate>
		<dc:creator>Andrea</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[symfony]]></category>

		<guid isPermaLink="false">http://e.nigma.be/?p=23</guid>
		<description><![CDATA[I&#8217;m currently working on a new project and I wanted to study something new to do it. It will be a web application, written in php. No matter what it will be useful for but I&#8217;ve found symfony, a great php framework. I read The Book in these 3 days, I&#8217;ll start tomorrow developing my [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working on a new project and I wanted to study something new to do it. It will be a web application, written in php. No matter what it will be useful for but I&#8217;ve found <a href="http://www.symfony-project.org">symfony</a>, a great php framework. I read <a href="http://www.symfony-project.org/book/">The Book</a> in these 3 days, I&#8217;ll start tomorrow developing my app, I should be able to finish the model part in few days, I hope symfony helps :^)</p>
<p>Use it in local is quite simple, there are lot of tutorials and how-to on the symfony site. The painful part is to use it on a common hosting service.</p>
<p>In fact a very powerful tool of symfony is the command  line interface, and you usually don&#8217;t have ssh support on your hosting service. There is a great tutorial <a href="http://trac.symfony-project.org/wiki/InstallingSymfonyOnSharedHostNoSsh">here</a> to make things work, and it&#8217;s quite simple, really! I did it today, now it&#8217;s working both  on the  server and on my pc. Look at it ;^)</p>
<p>return &#8216;bye&#8217;;</p>
]]></content:encoded>
			<wfw:commentRss>http://e.nigma.be/2008/08/04/symfony-without-ssh-and-pear/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GCJ 2008: Fly Swatter</title>
		<link>http://e.nigma.be/2008/07/19/gcj-2008-fly-swatter/</link>
		<comments>http://e.nigma.be/2008/07/19/gcj-2008-fly-swatter/#comments</comments>
		<pubDate>Sat, 19 Jul 2008 12:16:29 +0000</pubDate>
		<dc:creator>Andrea</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[GCJ 2008]]></category>
		<category><![CDATA[gcj]]></category>
		<category><![CDATA[Google Code Jam]]></category>
		<category><![CDATA[mistakes]]></category>

		<guid isPermaLink="false">http://e.nigma.be/?p=15</guid>
		<description><![CDATA[Consider this: Believe it or not, but that&#8217;s a racquet! In fact the third problem of the Qualification Round of Google Code Jam 2008 is about balls and racquets (or rackets if you prefer)&#8230; well&#8230; actually it&#8217;s about probability :^D You can find the whole text of the problem here, I&#8217;m reporting just the interesting [...]]]></description>
			<content:encoded><![CDATA[<p>Consider this:<br />
<a href='http://e.nigma.be/wp-content/uploads/2008/07/racchetta.png'><img src="http://e.nigma.be/wp-content/uploads/2008/07/racchetta.png" alt="GCJ 2008 - Problem C - raquet" title="GCJ 2008 - Problem C - raquet" width="308" height="267" class="alignnone size-full wp-image-16" style="border:0px" /></a></p>
<p>Believe it or not, but that&#8217;s a racquet! In fact the third problem of the Qualification Round of Google Code Jam 2008 is about balls and racquets (or rackets if you prefer)&#8230; well&#8230; actually it&#8217;s about probability :^D</p>
<p>You can find the whole text of the problem <a href="http://code.google.com/codejam/contest/dashboard?c=agdjb2RlamFtcg8LEghjb250ZXN0cxjqOQw">here</a>, I&#8217;m reporting just the interesting part (IMHO):</p>
<blockquote><p>Problem</p>
<p>What are your chances of hitting a fly with a tennis racquet?</p>
<p>To start with, ignore the racquet&#8217;s handle. Assume the racquet is a perfect ring, of outer radius <strong>R</strong> and thickness <strong>t</strong> (so the inner radius of the ring is <strong>R-t</strong>).</p>
<p>The ring is covered with horizontal and vertical strings. Each string is a cylinder of radius <strong>r</strong>. Each string is a chord of the ring (a straight line connecting two points of the circle). There is a gap of length <strong>g</strong> between neighbouring strings. The strings are symmetric with respect to the center of the racquet i.e. there is a pair of strings whose centers meet at the center of the ring.</p>
<p>The fly is a sphere of radius <strong>f</strong>. Assume that the racquet is moving in a straight line perpendicular to the plane of the ring. Assume also that the fly&#8217;s center is inside the outer radius of the racquet and is equally likely to be anywhere within that radius. Any overlap between the fly and the racquet (the ring or a string) counts as a hit. </p></blockquote>
<p>The input file consists in <strong>N</strong> lines containing values of <strong>f</strong>, <strong>R</strong>, <strong>t</strong>, <strong>r</strong> and <strong>g</strong> separated by spaces.</p>
<p>I&#8217;m now trying to explain my approach. The probability we&#8217;re looking for it&#8217;s given by <em>total_hitting_area</em>/<em>total_racquet_area</em>, or <em>1 &#8211; total_<strong>not</strong>_hitting_area</em>/<em>total_racquet_area</em>&#8230;</p>
<p>total racquet area it&#8217;s simple:<br />
<a href='http://e.nigma.be/wp-content/uploads/2008/07/big.png'><img src="http://e.nigma.be/wp-content/uploads/2008/07/big.png" alt="GCJ 2008 - Problem C - big" title="GCJ 2008 - Problem C - big" width="308" height="267" class="alignnone size-full wp-image-16" style="border:0px" /></a><br />
I&#8217;ll call it <strong>big</strong>, and it&#8217;s given by pi*R^2</p>
<p>The problem is the other area. I&#8217;ll try to calculate the <em><strong>not</strong> hitting area</em>. Calculus gives a solution to get area of any kind of shape: <em>integrals</em>, but I don&#8217;t think it&#8217;s necessary to use them in this case. First for all we need to simplify. I don&#8217;t care about the ball, I&#8217;ll <strong>care about the center of the ball</strong>. The center of the ball has to be a distance <strong>f</strong> (the ball radius) from the border of the racquet in order to do<em>not hit</em> it&#8230;</p>
<p>This is so the interesting area:<br />
<a href='http://e.nigma.be/wp-content/uploads/2008/07/small.png'><img src="http://e.nigma.be/wp-content/uploads/2008/07/small.png" alt="GCJ 2008 - Problem C - small" title="GCJ 2008 - Problem C - small" width="308" height="267" class="alignnone size-full wp-image-18" style="border:0px" /></a><br />
I&#8217;ll call it <strong>small</strong>, and has a radius of <em>R-t-f</em> so the area is given by pi*(<em>R-t-f</em>)^2</p>
<p>The probability of the center of the ball to be in the small area is given by <em>small</em>/<em>big</em>, and I call it <strong>q1</strong></p>
<p>Now consider this:<br />
<a href='http://e.nigma.be/wp-content/uploads/2008/07/square.png'><img src="http://e.nigma.be/wp-content/uploads/2008/07/square.png" alt="GCJ 2008 - Problem C - square" title="GCJ 2008 - Problem C - square" width="308" height="267" class="alignnone size-full wp-image-19" style="border:0px" /></a></p>
<p>The two red areas are identical, they are both composed by the same amount of green and white area, I can consider any of them as a <strong>square</strong>, its area is given by (<em><strong>g</strong>+2*<strong>r</strong></em>)^2</p>
<p>Note that <strong>small</strong>  is composed by <strong>square</strong>s. Some of them are cut off, but that&#8217;s <strong>should not be</strong>  a problem&#8230; <em>it should</em>&#8230;</p>
<p>Now consider an <em>hole</em>:<br />
<a href='http://e.nigma.be/wp-content/uploads/2008/07/gap.png'><img src="http://e.nigma.be/wp-content/uploads/2008/07/gap.png" alt="GCJ 2008 - Problem C - gap" title="GCJ 2008 - Problem C - gap" width="308" height="267" class="alignnone size-full wp-image-20" style="border:0px" /></a><br />
I&#8217;ll call the red area <strong>gap</strong>, it&#8217;s a little square with sides long <em><strong>g</strong>-2*<strong>f</strong></em> as I subtract 2 times the ball radius. In fact the center of the ball has to be at least at a distance of <strong>f</strong> from the sides of the white area in order to do not hit a string. So a <strong>gap</strong> is (<em><strong>g</strong>-2*<strong>f</strong></em>)^2</p>
<p>The probability of the center of the ball to pass through a <strong>gap</strong> contained in a <strong>square</strong> is given by <strong>gap</strong>/<strong>square</strong>  and I&#8217;ll call it <strong>q2</strong></p>
<p>In order to do not hit the racquet to events have to happen <em>at the same time</em>:</p>
<ul>
<ol>
<li>The center of the ball has to be in the <strong>small</strong> area</li>
<li>The center of the ball has to be in a <strong>gap</strong> area</li>
</ol>
</ul>
<p>These to events have probability <strong>q1</strong> and <strong>q2</strong> respectively.<br />
The probability they are both verified is <strong>q</strong> = <strong>q1</strong> * <strong>q2</strong>, and that&#8217;s the not successful case.</p>
<p>The successful case is <strong>p</strong> = 1 &#8211; <strong>q</strong>&#8230; the probability we were looking for&#8230;</p>
<p>It should work&#8230; it should&#8230; But it doesn&#8217;t.</p>
<p>I tried an other approach so. I calculate how many <strong>square</strong>s there are in the <strong>small</strong> area. That number is <strong>nos</strong> = <strong>small</strong>/<strong>square</strong> (where <strong>nos</strong> stands for <strong>n</strong>umber <strong>o</strong>f <strong>s</strong>quares)</p>
<p>We know there are as many <strong>gap</strong> as <strong>squares</strong>, that&#8217;s just because there is one <strong>gap</strong> inside each <strong>square</strong>.</p>
<p>In this way I can calculate the <em>total gap area</em>, that is just <strong>nos</strong>*<strong>gap</strong> and I call it <strong>totalgap</strong>&#8230;</p>
<p>Do you remember the <em>1 &#8211; total_<strong>not</strong>_hitting_area</em>/<em>total_racquet_area</em>&#8230; well the <em>total_<strong>not</strong>_hitting_area</em> is just our <strong>totalgap</strong>!</p>
<p>So we have q = <strong>totalgap</strong>/<strong>big</strong>&#8230; and <strong>p</strong> = 1 &#8211; <strong>q</strong></p>
<p>I&#8217;ve found the same <strong>p</strong> as the one above&#8230; and it&#8217;s wrong.</p>
<p>I went so close the solution but there is something wrong, just to show you, these are the sample cases:<br />
<code>0.25 1.0 0.1 0.01 0.5<br />
0.25 1.0 0.1 0.01 0.9<br />
0.00001 10000 0.00001 0.00001 1000<br />
0.4 10000 0.00001 0.00001 700<br />
1 100 1 1 10</code></p>
<p>The Google&#8217;s results for these cases are:<br />
<code>Case #1: 1.000000<br />
Case #2: 0.910015<br />
Case #3: 0.000000<br />
Case #4: 0.002371<br />
Case #5: 0.573972</code></p>
<p>But mines are:<br />
<code>Case #1: <strong>1.000000</strong><br />
Case #2: <strong>0.9</strong>20132<br />
Case #3: <strong>0.000000</strong><br />
Case #4: <strong>0.0023</strong>64<br />
Case #5: <strong>0.573</strong>156</code></p>
<p>They are <strong>so</strong> close.</p>
<p>You can download my solution file by clicking <a href='http://e.nigma.be/wp-content/uploads/2008/07/gcj_2008_c.py'>here</a>.<br />
That&#8217;s my implementation in python:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> solve<span style="color: black;">&#40;</span>f,R,t,r,g<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>f <span style="color: #66cc66;">&gt;</span>= g: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">1.0</span>
    t += f
    g -= <span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>f
    r += f
&nbsp;
&nbsp;
    big = pi<span style="color: #66cc66;">*</span>R<span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span>
    small = pi<span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span>R-t<span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span>
    square = <span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span>r+g<span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span>
    gap = g<span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Way 1</span>
&nbsp;
    q1 = <span style="color: #ff4500;">1.0</span> <span style="color: #66cc66;">*</span> small/big
    q2 = <span style="color: #ff4500;">1.0</span> <span style="color: #66cc66;">*</span> gap/square
    q = q1<span style="color: #66cc66;">*</span>q2
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;"># Way 2</span>
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    nos = 1.0*small/square
    totalgap = gap*nos
    q1 = 1.0 * small/big
    q2 = 1.0 * totalgap/small
&nbsp;
    q = q1*q2
    '</span><span style="color: #483d8b;">''</span>
&nbsp;
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff4500;">0</span> <span style="color: #66cc66;">&lt;</span>= q:
        <span style="color: #ff7700;font-weight:bold;">if</span> q <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">1</span>:
            p = <span style="color: #ff4500;">1</span>-q
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            p = <span style="color: #ff4500;">0.0</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        p = <span style="color: #ff4500;">1.0</span>
    <span style="color: #808080; font-style: italic;">#print 1-q</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> p
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> sfs<span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">'solve from string'</span>
    s = s.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' '</span><span style="color: black;">&#41;</span>
    f,R,t,r,g = <span style="color: black;">&#91;</span><span style="color: #008000;">float</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> s<span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> solve<span style="color: black;">&#40;</span>f,R,t,r,g<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>I did one in C too, I was thinking python did something wrong with math&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#define pi 3.1415926535897931</span>
&nbsp;
<span style="color: #993333;">double</span> solve<span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> f<span style="color: #339933;">,</span><span style="color: #993333;">double</span> R<span style="color: #339933;">,</span><span style="color: #993333;">double</span> t<span style="color: #339933;">,</span><span style="color: #993333;">double</span> r<span style="color: #339933;">,</span><span style="color: #993333;">double</span> g<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">double</span> big<span style="color: #339933;">,</span>small<span style="color: #339933;">,</span>square<span style="color: #339933;">,</span>gap<span style="color: #339933;">,</span>q1<span style="color: #339933;">,</span>q2<span style="color: #339933;">,</span>q<span style="color: #339933;">,</span>p<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>f <span style="color: #339933;">&gt;=</span>g <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
         <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    t <span style="color: #339933;">=</span> t <span style="color: #339933;">+</span> f<span style="color: #339933;">;</span>
    g <span style="color: #339933;">=</span> g <span style="color: #339933;">-</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>f<span style="color: #339933;">;</span>
    r <span style="color: #339933;">=</span> r <span style="color: #339933;">+</span>f<span style="color: #339933;">;</span>
&nbsp;
    big <span style="color: #339933;">=</span> pi<span style="color: #339933;">*</span>R<span style="color: #339933;">*</span>R<span style="color: #339933;">;</span>
    small <span style="color: #339933;">=</span> pi<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>R<span style="color: #339933;">-</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span>R<span style="color: #339933;">-</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    square <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>r<span style="color: #339933;">+</span>g<span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>r<span style="color: #339933;">+</span>g<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    gap <span style="color: #339933;">=</span> g<span style="color: #339933;">*</span>g<span style="color: #339933;">;</span>
&nbsp;
    q1 <span style="color: #339933;">=</span> small<span style="color: #339933;">/</span>big<span style="color: #339933;">;</span>
    q2 <span style="color: #339933;">=</span> gap<span style="color: #339933;">/</span>square<span style="color: #339933;">;</span>
    q <span style="color: #339933;">=</span> q1<span style="color: #339933;">*</span>q2<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">&lt;=</span>q<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>q<span style="color: #339933;">&lt;=</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            p <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">-</span>q<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> p <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> p <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> p<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Case #1: %lf<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>solve<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.25</span><span style="color: #339933;">,</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.1</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.01</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Case #2: %lf<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>solve<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.25</span><span style="color: #339933;">,</span> <span style="color:#800080;">1.0</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.1</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.01</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.9</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Case #3: %lf<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>solve<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.00001</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">10000</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.00001</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.00001</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Case #4: %lf<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>solve<span style="color: #009900;">&#40;</span><span style="color:#800080;">0.4</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">10000</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.00001</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.00001</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">700</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Case #5: %lf<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>solve<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">100</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>There&#8217;s no need to say that the result was the very same.</p>
<p>I don&#8217;t know what&#8217;s wrong with my approach, all the others&#8217; code I read used something involving integrals, sometime already solved and they just did the &#8220;definite&#8221; part.</p>
<p>Did you read someone&#8217;s code that is not using integrals but pure geometry and probability approach?<br />
I think it&#8217;s possible, I think there is something very bastard I&#8217;m missing&#8230; don&#8217;t know what&#8230; do you?</p>
<p>return &#8220;grr&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://e.nigma.be/2008/07/19/gcj-2008-fly-swatter/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>GCJ 2008: Train Timetable</title>
		<link>http://e.nigma.be/2008/07/19/gcj-2008-train-timetable/</link>
		<comments>http://e.nigma.be/2008/07/19/gcj-2008-train-timetable/#comments</comments>
		<pubDate>Sat, 19 Jul 2008 00:01:18 +0000</pubDate>
		<dc:creator>Andrea</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[GCJ 2008]]></category>
		<category><![CDATA[gcj]]></category>
		<category><![CDATA[Google Code Jam]]></category>

		<guid isPermaLink="false">http://e.nigma.be/?p=13</guid>
		<description><![CDATA[This one was the title of the second problem of Google Code Jam Qualification Round 2008. This time my code is a bit messy, but I won&#8217;t adjust it for you :^D It&#8217;s the disadvantage of coding fast, at least for me. I often wrote something not necessary, someone call it gold planting, it&#8217;s just [...]]]></description>
			<content:encoded><![CDATA[<p>This one was the title of the second problem of Google Code Jam Qualification Round 2008.</p>
<p>This time my code is a bit messy, but I won&#8217;t adjust it for you :^D It&#8217;s the disadvantage of coding fast, at least for me. I often wrote something not necessary, someone call it gold planting, it&#8217;s just what I did in the <a href="http://e.nigma.be/archives/7">maze problem</a> with the draw method&#8230;</p>
<p>You can download my solution for the <a href='http://e.nigma.be/wp-content/uploads/2008/07/gcj_2008_b.py'>Train Timetable problem</a> I wrote.</p>
<p>Here is the commend code.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">__future__</span> <span style="color: #ff7700;font-weight:bold;">import</span> with_statement
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">copy</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">copy</span></pre></td></tr></table></div>

<p>Don&#8217;t ask me why I imported <em>copy</em>,  I think I did it while testing&#8230; maybe. I don&#8217;t remember it&#8217;s not useful<br />
The first 3 are classes but I did not spent so much time projecting their structure. Tempo just means <em>time </em>in italian, I didn&#8217;t want to override the python name (I know that&#8217;s the name of a lib, it&#8217;s only an excuse, I&#8217;m italian, let me name things in italian sometime :^P)</p>
<p><strong>tempo</strong> is not so useful  in that form&#8230; anyway converts the format &#8216;hh:mm&#8217; to an int representing minutes</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> tempo<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, s<span style="color: black;">&#41;</span>:
        a = s.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">':'</span><span style="color: black;">&#41;</span>
        x = <span style="color: black;">&#91;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> a<span style="color: black;">&#93;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">m</span> = x<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">60</span> + x<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span></pre></td></tr></table></div>

<p><strong>Trip</strong> represents a trip, with starting and ending time.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Trip<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,start,end<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">start</span> = tempo<span style="color: black;">&#40;</span>start<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">end</span> = tempo<span style="color: black;">&#40;</span>end<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">duration</span> = <span style="color: #008000;">self</span>.<span style="color: black;">end</span>.<span style="color: black;">m</span> - <span style="color: #008000;">self</span>.<span style="color: black;">start</span>.<span style="color: black;">m</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">busy</span> = <span style="color: #008000;">False</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__cmp__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,x<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">cmp</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">start</span>.<span style="color: black;">m</span>,x.<span style="color: black;">start</span>.<span style="color: black;">m</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> use<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">busy</span> = <span style="color: #008000;">True</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> isfree<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">busy</span> == <span style="color: #008000;">False</span>
&nbsp;
    is_free = <span style="color: #008000;">property</span><span style="color: black;">&#40;</span>isfree<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>There is no need to put the <em>duration</em> attribute, but it did, thinking on it now it&#8217;s pretty useless&#8230; The interesting part is the flag <em>busy</em>, or <em>is_free </em>(just a joke with python property function&#8230;). A Trip <strong>is free if you did not use it yet</strong>. I added <em>__cmp__</em>  method because I&#8217;ll need to &#8220;sort&#8221; them, if trip X starts earlier than trip Y well&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">X<span style="color: #66cc66;">&lt;</span>y == <span style="color: #008000;">True</span></pre></div></div>

<p>It will be useful to understand what&#8217;s the first Trip to use&#8230;</p>
<p>A <strong>Train</strong> is identified by the time of the last thing it did (<em>self.Time</em>) and it has done something or not (<em>self.init</em>)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Train<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,Time<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">Time</span> = tempo<span style="color: black;">&#40;</span>Time<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">init</span> = <span style="color: #008000;">False</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> TripTrough<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, trip, turnaround<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">'Returns True if the train can trip (and it does)'</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>trip.<span style="color: black;">is_free</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">self</span>.<span style="color: black;">Time</span>.<span style="color: black;">m</span>+turnaround <span style="color: #66cc66;">&lt;</span>= trip.<span style="color: black;">start</span>.<span style="color: black;">m</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #008000;">self</span>.<span style="color: black;">init</span> ==<span style="color: #008000;">False</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">init</span> = <span style="color: #008000;">True</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">Time</span> = trip.<span style="color: black;">end</span>
            trip.<span style="color: black;">use</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span></pre></td></tr></table></div>

<p>The TripTrough method is very important (yes I know, it&#8217;s Through not Trough&#8230; my english sucks, sorry for that :^D )&#8230; Anyway&#8230;  How does it work? It&#8217;s simple. It wants a trip and the turnaround time. the flag <em>init</em> it&#8217;s useful because if the trip starts at 0:03 and you have a turnaround of 5 you have to don&#8217;t care about the turnaround, you have no need to <em>turn around</em>&#8230; my check can&#8217;t work without an init flag&#8230; I don&#8217;t know if it&#8217;s clear..</p>
<p>Now something magical happens, I&#8217;m doing it bad I think, I&#8217;m playing with the &#8220;all name are references&#8221; of python&#8230; I changed the trip flag as the train used it, I could do it out of that method but maybe I was tired, bored, drunk, whatever&#8230; and I did it in this way :^D. I updated the time of the train too, obviously.</p>
<p> <strong>Table</strong> has the algorithm! Let me explain&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Table<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,A,B,T<span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;"># trip lists... and turnaround</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">A</span> = A
        <span style="color: #008000;">self</span>.<span style="color: black;">B</span> = B
        <span style="color: #008000;">self</span>.<span style="color: black;">T</span> = T <span style="color: #808080; font-style: italic;"># turnaraund</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> solver<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, starting,ending,train<span style="color: black;">&#41;</span>:
        starting.<span style="color: black;">sort</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        s = starting
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> s:
            <span style="color: #ff7700;font-weight:bold;">if</span> train.<span style="color: black;">TripTrough</span><span style="color: black;">&#40;</span>i,<span style="color: #008000;">self</span>.<span style="color: black;">T</span><span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">solver</span><span style="color: black;">&#40;</span>ending,starting,train<span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># else...</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> train
&nbsp;
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> solve<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        tfa = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
        tfb = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
        check = <span style="color: #008000;">True</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> check:
            freeA = <span style="color: black;">&#91;</span>i <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">A</span> <span style="color: #ff7700;font-weight:bold;">if</span> i.<span style="color: black;">is_free</span><span style="color: black;">&#93;</span>
            freeB = <span style="color: black;">&#91;</span>i <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">B</span> <span style="color: #ff7700;font-weight:bold;">if</span> i.<span style="color: black;">is_free</span><span style="color: black;">&#93;</span>
            <span style="color: #808080; font-style: italic;">#print freeA, freeB</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>freeB<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>freeA<span style="color: black;">&#41;</span><span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span>:
                mins = <span style="color: #008000;">min</span><span style="color: black;">&#40;</span>freeA<span style="color: black;">&#41;</span>,<span style="color: #008000;">min</span><span style="color: black;">&#40;</span>freeB<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>freeB<span style="color: black;">&#41;</span> == <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>freeA<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>:
                <span style="color: #ff7700;font-weight:bold;">break</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>freeA<span style="color: black;">&#41;</span><span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>freeB<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">or</span> mins<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">&lt;</span>mins<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>: <span style="color: #808080; font-style: italic;">#start from A</span>
                tfa.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">solver</span><span style="color: black;">&#40;</span>freeA,freeB,Train<span style="color: black;">&#40;</span><span style="color: #483d8b;">'00:00'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                tfb.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">solver</span><span style="color: black;">&#40;</span>freeB,freeA,Train<span style="color: black;">&#40;</span><span style="color: #483d8b;">'00:00'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>tfa<span style="color: black;">&#41;</span>,<span style="color: #008000;">len</span><span style="color: black;">&#40;</span>tfb<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<ul>
<li>A is the list of trips starting from station A going to B</li>
<li>B is the list of trips starting from station B going to A</li>
<li>T is the turnaround time of the case</li>
<li><strong>solver</strong> it&#8217;s a recursive method.<br />
It takes 3 parameters:</p>
<ol>
<li><strong>starting</strong> it&#8217;s the list of the trips <em>starting</em> from a station </li>
<li><strong>ending</strong> it&#8217;s the list of the trips starting from the other station </li>
<li><strong>train</strong> is the train that&#8217;s traveling forward and backward </li>
</ol>
<p>Well&#8230; I sort the trip list, in this way come first the one that will start first, then I begin to look for the first one that the train can travel through. If it can I can look (recursively part)  for a return trip, just inverting <em>starting </em>with <em>ending </em>calling once again the <em>solver</em> method.</li>
<li><strong>solve</strong> is the method you need to call to get the final result.
<ul>
<li><strong>tfa </strong>is the list of trains starting from the A station</li>
<li><strong>tfb </strong>is the list of trains starting from the B one</li>
<li>The part

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>65
66
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">        check = <span style="color: #008000;">True</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> check:</pre></td></tr></table></div>

<p>it&#8217;s just useless&#8230; I could use while True&#8230; I won&#8217;t change <em>check</em>&#8230;</li>
<li><strong>freeA </strong>is the list of the trip starting in A that are not busy</li>
<li><strong>freeB </strong>it&#8217;s the same as the above one but in B, obviously</li>
<li>if there is something in <em>freeA </em>and <em>freeB </em> I&#8217;d like to know what&#8217;s the trip starting earlier in <em>freeA </em>and the one in <em>freeB</em></li>
<li>else if there is <strong>nothing </strong>in <em>freeA </em>and <strong>nothing </strong>in <em>freeB </em>there is <strong>no need to continue</strong>&#8230;</li>
<li>if there is <strong>something </strong>in freeA and there is <strong>nothing </strong>in freeB <strong>or</strong> just the earliest trip in <strong>freeA starts before</strong> the earliest one in <strong>freeB </strong>I can set up a <strong>new train</strong> and make it travel <strong>starting </strong>from the earliest trip in <strong>freeA </strong>until it stops&#8230;</li>
<li>else I can set up a <strong>new train</strong> and make it travel <strong>starting from</strong> the earliest trip in <strong>freeB </strong>until it stops&#8230;</li>
<li>I added these trains to <strong>tfa </strong>and <strong>tfb</strong></li>
<li>I <strong>return</strong> the number of items of <strong>tfa </strong>and <strong>tfb</strong>, as the problem asks..</li>
</ul>
</li>
</ul>
<p>You can download the solution <a href='http://e.nigma.be/wp-content/uploads/2008/07/gcj_2008_b.py'>here</a> with the file processing too.</p>
<p>The algorithm it&#8217;s correct but the implementation sucks, I think I can code a better one with less line of code, I&#8217;ve just to spend some time on it&#8230; It was a time challenge and I was not thinking so much of optimization&#8230; I don&#8217;t know If I&#8217;ll post a revisited one&#8230; For now it&#8217;s all folks!</p>
<p>return &#8216;Bye&#8217;</p>
]]></content:encoded>
			<wfw:commentRss>http://e.nigma.be/2008/07/19/gcj-2008-train-timetable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GCJ 2008: Saving the Universe</title>
		<link>http://e.nigma.be/2008/07/18/gcj-2008-saving-the-universe/</link>
		<comments>http://e.nigma.be/2008/07/18/gcj-2008-saving-the-universe/#comments</comments>
		<pubDate>Fri, 18 Jul 2008 11:15:18 +0000</pubDate>
		<dc:creator>Andrea</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[GCJ 2008]]></category>
		<category><![CDATA[gcj]]></category>
		<category><![CDATA[Google Code Jam]]></category>

		<guid isPermaLink="false">http://e.nigma.be/?p=11</guid>
		<description><![CDATA[That&#8217;s was the title of the first and very funny problem of Google Code Jam Qualification Round 2008 The urban legend goes that if you go to the Google homepage and search for &#8220;Google&#8221;, the universe will implode. We have a secret to share&#8230; It is true! Please don&#8217;t try it, or tell anyone. All [...]]]></description>
			<content:encoded><![CDATA[<p>That&#8217;s was the title of the first and very funny problem of Google Code Jam Qualification Round 2008</p>
<blockquote><p>The urban legend goes that if you go to the Google homepage and search for &#8220;Google&#8221;, the universe will implode. We have a secret to share&#8230; It is true! Please don&#8217;t try it, or tell anyone. All right, maybe not. We are just kidding. </p></blockquote>
<p>You can find (I think you need to register) the whole problem <a href="http://code.google.com/codejam/contest/dashboard?c=agdjb2RlamFtcg8LEghjb250ZXN0cxjqOQw">here</a>.</p>
<p>I did it recursively and for the large input I had to  <em>setrecursionlimit</em> up to 2000. I&#8217;ll show you why&#8230;</p>
<p>You can download my solution <a href='http://e.nigma.be/wp-content/uploads/2008/07/gcj_2008_a.py'>here</a>.</p>
<p>That&#8217;s my implementation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #dc143c;">sys</span>.<span style="color: black;">setrecursionlimit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">20000</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> process<span style="color: black;">&#40;</span>se, q, switch = <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>q<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> switch
    rank = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
&nbsp;
    notin = <span style="color: #008000;">False</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> se:
        <span style="color: #ff7700;font-weight:bold;">if</span> q.<span style="color: black;">count</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span><span style="color: #66cc66;">&gt;</span><span style="color: #ff4500;">0</span>:
            rank<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> = q.<span style="color: black;">index</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            notin = i
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> notin == <span style="color: #008000;">False</span>:
        a,b = rank<span style="color: black;">&#91;</span>se<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>,<span style="color: #ff4500;">0</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> rank:
            <span style="color: #ff7700;font-weight:bold;">if</span> rank<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: #66cc66;">&gt;</span>b:
                a = i
                b = rank<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>
        switch += <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> process<span style="color: black;">&#40;</span>se,q<span style="color: black;">&#91;</span>b:<span style="color: black;">&#93;</span>,switch<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> switch</pre></td></tr></table></div>

<p>In words&#8230;</p>
<p><strong>se</strong> is the list ( ['Google','Yahoo',...] ) of <strong>s</strong>earch <strong>e</strong>ngines<br />
<strong>q</strong> is the list (like the one above) of queries</p>
<ul>
<li>At the beginning I did no switches (<strong>switch </strong>= 0), but it&#8217;s a default value, when I&#8217;ll call it I&#8217;ll set it</li>
<li>If there is no query I don&#8217;t need to switch anymore, I can return the number of switches (<strong>switch</strong>)</li>
<li><strong>rank</strong> is a dictionary. It&#8217;s just like &#8216;Search Engine&#8217;: &#8216;position of the first query with the same name of this search engine&#8217;</li>
<li>if the name of a search engine does <strong>not </strong>appear <strong>in </strong>the query list I may do all searches with that search engines (the <strong>notin </strong>test)</li>
<li>else I look in the dictionary for what is the search engine that appear as later as possible in the query list and I use it, I add 1 to the switches counter, and return (recursively) the process giving the same <strong>se</strong>, the current switches number but a different query list!</li>
</ul>
<p>The new query list is just the remaining queries. Example:</p>
<p>se = Yeehaw, NSM, Dont Ask, B9, Googol<br />
q = Yeehaw,Yeehaw,Googol,B9,Googol,NSM,B9,NSM,Dont Ask,Googol</p>
<p><strong>Call #1</strong><br />
We did no switch, so <em>switch = 0</em><br />
All search engines appears in the query list&#8230;  Here is the list of each <strong>se</strong> with the index of the first query with the same name:</p>
<p>Yeehaw: 0, NSM: 5, <strong>Dont Ask: 8</strong>, B9: 3, Googol: 2</p>
<p><em>Dont Ask</em> has an higher index so we can use that to search up to that index with it, and  process the next call with the new query list:</p>
<p>se = Yeehaw, NSM, Dont Ask, B9, Googol<br />
q = Dont Ask,Googol</p>
<p><strong>Call #2</strong><br />
We did one switch, so <em>switch = 1</em></p>
<p>Yeehaw, NSM and B9 do <strong>not</strong> appear <strong>in</strong> the query list, I can use any of them without do any other change, I return the current number of switches ;^) Quite simple ;^)</p>
<p>In the big file there was a query list of 999 items Like this:</p>
<p>se = ['A', 'B']<br />
q = ['A', 'B','A', 'B','A', 'B','A', 'B','A', 'B',...]</p>
<p>ok?  It did lots of recursion and went over the limit, I had to adjust that :^D</p>
<p>return &#8216;Bye&#8217;</p>
]]></content:encoded>
			<wfw:commentRss>http://e.nigma.be/2008/07/18/gcj-2008-saving-the-universe/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

