<?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>Shut up and code</title>
	<atom:link href="http://lepunk.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://lepunk.co.uk</link>
	<description>lePunk&#039;s thoughts about life, universe and everything</description>
	<lastBuildDate>Sun, 19 May 2013 17:14:47 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Ugly, dirty, but it works</title>
		<link>http://lepunk.co.uk/ugly-dirty-but-it-works/</link>
		<comments>http://lepunk.co.uk/ugly-dirty-but-it-works/#comments</comments>
		<pubDate>Sun, 19 May 2013 17:10:59 +0000</pubDate>
		<dc:creator>lePunk</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Geek]]></category>
		<category><![CDATA[autohotkey]]></category>
		<category><![CDATA[crack]]></category>
		<category><![CDATA[macro]]></category>

		<guid isPermaLink="false">http://lepunk.co.uk/?p=108</guid>
		<description><![CDATA[A couple of days ago my uncle pinged me on Facebook asking for a favor. He is doing his final semester at the university, studying electrical engineering (or whatnot) and his final paper was due in less than 24 hours. The problem occurred when the software he made his paper in suddenly started showing a [...]]]></description>
				<content:encoded><![CDATA[<p>A couple of days ago my uncle pinged me on Facebook asking for a favor. He is doing his final semester at the university, studying electrical engineering (or whatnot) and his final paper was due in less than 24 hours. The problem occurred when the software he made his paper in suddenly started showing a dialog asking for a PIN code locking my uncle out of accessing the document. Naturally he asked me to try &#8220;cracking&#8221; it since I&#8217;m &#8220;good with computers&#8221;.</p>
<p>As a web developer, this sort of job is completely out of my league. I remember reading tutorials on how to crack software using hex editors and stuff but there is no way I can do it in 24 hours. So my first thought was to turn him down telling him to buy the software (which is freaking expensive).</p>
<p>But then I started to think outside the box. The PIN code the software is asking for is only 6 digits, only contains numbers and you can try it as many times as you wish. Ideal for brute forcing. Also my uncle wasn&#8217;t interested in the actual PIN code just wanted to get the document.</p>
<p>I remembered that at the early stage of the company I&#8217;m working for we used a visual scraping software called <a href="http://kapowsoftware.com/solutions/web-intelligence/web-data-extraction.php">Kapow Robots</a>. Kapow is a bit overkill and expensive for this job , but it made me search for open source programmable macro software. Thats how I found <a href="http://autohotkey.com">AutoHotKey</a> which is a very nice little tool using an incredibly ridiculous language (with great documentation). So I came up with this little snippet of code in like 15 minutes:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;">test <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100000</span> <span style="color: #666666; font-style: italic;">#starting code
</span>
Loop <span style="color: #cc66cc;">899999</span>
<span style="color: #009900;">&#123;</span>
 test<span style="color: #339933;">++</span>
 Click <span style="color: #cc66cc;">90</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">120</span> <span style="color: #666666; font-style: italic;">#click in the input box
</span> Send <span style="color: #339933;">%</span>test<span style="color: #339933;">%</span> <span style="color: #666666; font-style: italic;">#type in the value of test
</span> Click <span style="color: #cc66cc;">250</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">120</span> <span style="color: #666666; font-style: italic;">#click in the submit button
</span> Click <span style="color: #cc66cc;">300</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">250</span> <span style="color: #666666; font-style: italic;">#click on the ok button in the error message
</span> Click <span style="color: #cc66cc;">90</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">120</span> <span style="color: #666666; font-style: italic;">#back to the input field
</span> Send <span style="color: #009900;">&#123;</span>bs <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">#delete whatever is in the input
</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Yeah, it is crappy as hell. Couple of problems:</p>
<ul>
<li>If the PIN starts with 0 it doesn&#8217;t work. It is easy to fix but the language doesn&#8217;t have any string functions and it was way over midnight so i figured 10% chance is not too much to risk.</li>
<li>It doesn&#8217;t stop after it found the right combo so it will most likely will leave a massive mess on the desktop</li>
<li>It doesn&#8217;t log the PIN but we don&#8217;t need it anyway</li>
</ul>
<p>Is it ugly and dirty? Hell Yes! Did it work? Oh yeah</p>
<p>3 clichés which are very true: Think outside of the box, Use the right tool for the job and Don&#8217;t try to reinvent the weel when it is not necessary.</p>
]]></content:encoded>
			<wfw:commentRss>http://lepunk.co.uk/ugly-dirty-but-it-works/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scraping the sh*t out of the interwebz – Part #2</title>
		<link>http://lepunk.co.uk/scraping-the-sht-out-of-the-interwebz-part-2/</link>
		<comments>http://lepunk.co.uk/scraping-the-sht-out-of-the-interwebz-part-2/#comments</comments>
		<pubDate>Sun, 21 Apr 2013 16:00:55 +0000</pubDate>
		<dc:creator>lePunk</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[crawling]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[scraping]]></category>

		<guid isPermaLink="false">http://lepunk.co.uk/?p=99</guid>
		<description><![CDATA[If you haven&#8217;t read the first part of my php curl tutorial you should do so here. As in the previous one we will use my open source curl.class.php but this time we will do something more exciting than just scraping ebay. Today we will log into HackerNews. When it comes to scraping a website [...]]]></description>
				<content:encoded><![CDATA[<p>If you haven&#8217;t read the first part of my php curl tutorial you <a href="http://lepunk.co.uk/scraping-the-sht-out-of-the-interwebz-part-1/">should do so here</a>. As in the previous one we will use my <a href="https://github.com/lepunk/curl.class.php">open source curl.class.php</a> but this time we will do something more exciting than just scraping ebay. Today we will log into <a href="https://news.ycombinator.com/news">HackerNews.</a></p>
<p>When it comes to scraping a website we are always aiming to emulate the same thing what a browser does. There are a whole bunch of great tools available for recording the requests your browser makes but I think the <a href="https://addons.mozilla.org/en-US/firefox/addon/live-http-headers/">Live HTTP headers</a> Firefox extension is outstanding. So go ahead and download it. A new menu item should appear in your Firefox under the Tools menu. Click it and select the Generator tab, untick everything and tick &#8220;redirects&#8221; and &#8220;requests&#8221;. Now the extension is set to record all the POST and GET requests + 30x redirects your browser makes. Now go to HackerNews and login as you would do normally. The extension should record something similar to this:</p>
<p><a href="http://lepunk.co.uk/wp-content/uploads/2013/04/live_http_headers.png"><img class="alignnone size-medium wp-image-100" alt="live_http_headers" src="http://lepunk.co.uk/wp-content/uploads/2013/04/live_http_headers-300x200.png" width="300" height="200" /></a></p>
<p>Great. It all seems easy enough. The only interesting part of the login POST request is this fnid hash. Well by examining the login form it turns out that the fnid is a hidden input field. No problems then. We can just grab it. Let me show you the full code then I will explain it</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #990000;">set_time_limit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;../curl.class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000088;">$hn_user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$hn_pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #000088;">$curl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Curl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #000088;">$curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSsl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCookieFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cookie.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;https://news.ycombinator.com/newslogin?whence=news&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">libxml_use_internal_errors</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadHTML</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fnid</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$inputs</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'input'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$inputs</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">length</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$inputs</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;fnid&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$fnid</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$inputs</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$fnid</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;can't find fnid<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>	<span style="color: #0000ff;">&quot;fnid&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$fnid</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;u&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$hn_user</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;p&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$hn_pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;https://news.ycombinator.com/y&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>So the first thing we notice is HackerNews operates on https. Which means we have to tell the curl class to handle the connection accordingly. Also since most sites are using cookies for logged in users it is always a good idea to prepare a cookie file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$curl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Curl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setSsl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCookieFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cookie.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Since last time I learned a handy function in PHP which makes DOMDocument less bitchy about invalid HTML so I won&#8217;t need to use the @ sign when using loadHTML</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">libxml_use_internal_errors</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Then we download the login page and get the fnid as we learned it in the last tutorial</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$fnid</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$inputs</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'input'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$inputs</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">length</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$inputs</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;fnid&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$fnid</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$inputs</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$fnid</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;can't find fnid<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Cool. Now the last step is to do the login POST request</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>	<span style="color: #0000ff;">&quot;fnid&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$fnid</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;u&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$hn_user</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">&quot;p&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$hn_pass</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;https://news.ycombinator.com/y&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The printed out $page should show your username which means you have a valid session. It was easy enough, wasn&#8217;t it? You can use a similar script to get quick account info from a service which doesn&#8217;t have an API or to automate a regular daily task.</p>
<p>DISCLOSURE: please don&#8217;t use your new knowledge for evil. Respect other people&#8217;s work and ask for permission if you can scrape content from their site.</p>
]]></content:encoded>
			<wfw:commentRss>http://lepunk.co.uk/scraping-the-sht-out-of-the-interwebz-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tools for productivity</title>
		<link>http://lepunk.co.uk/tools-for-productivity/</link>
		<comments>http://lepunk.co.uk/tools-for-productivity/#comments</comments>
		<pubDate>Sat, 16 Mar 2013 20:22:46 +0000</pubDate>
		<dc:creator>lePunk</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Geek]]></category>
		<category><![CDATA[asana]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[opera]]></category>
		<category><![CDATA[productivity tools]]></category>
		<category><![CDATA[streak]]></category>
		<category><![CDATA[total commander]]></category>

		<guid isPermaLink="false">http://lepunk.co.uk/?p=94</guid>
		<description><![CDATA[These are not necessarily productivity tools rather a list of software I use day-to-day to manage my workload. I prefer having as few windows open as possible so I&#8217;m very picky when it comes to choose my tools. Opera browser Hands down the best browser out there, period. I was very surprised when they announced [...]]]></description>
				<content:encoded><![CDATA[<p>These are not necessarily productivity tools rather a list of software I use day-to-day to manage my workload. I prefer having as few windows open as possible so I&#8217;m very picky when it comes to choose my tools.</p>
<ol>
<li><a href="http://www.opera.com/">Opera browser</a><br />
Hands down the best browser out there, period. I was very surprised when they announced that they will switch to webkit engine, but it is not a big deal. A browser is not just the rendering engine, but a set of tools which makes my life way easier. Opera features a bunch of great features like the superior download manager, built in irc client, grouped tabs, speed dial, RSS reader, built in mail client and so on. My favorite one must be Opera sync which automatically syncs your history, bookmarks and your passwords.</li>
<li><a href="http://www.ghisler.com/">Total Commander</a><br />
I grew up in the era of 2-pane file managers like Norton Commander and DOS navigator so when Total Commander became available on Windows (in the 90s I guess) it was an instant get for me. With this tool I can access and manipulate files super quickly. Also it features a great multi-session FTP client which means one less window to have open. I love it so much that I even run it on my Mac using Wine</li>
<li><a href="http://www.eclipse.org/">Eclipse</a><br />
I love Eclipse and I&#8217;m not ashamed to admit it. You can tell me all about how superior vim and emacs are but for me nothing can beat Eclipse, especially with the right plugins. I agree, it is a bit greedy for memory but memory is cheap these days. When I have to edit a file quickly I use my secondary editor, Programmers notepad</li>
<li>A notebook<br />
I can&#8217;t live without lists so I tend to create a physical todo list in my notebook. It is just feels great when you can cross something off.</li>
<li><a href="https://www.wunderlist.com/">Wunderlist</a><br />
Again, can&#8217;t live without lists. Wunderlist is a great, simple todo list app with support for multiple projects and users. They have native apps for pretty much everything so I can check my list on my phone, my ipad and it is always open on my desktop.</li>
<li><a href="http://www.mindmeister.com/">MindMeister</a><br />
Mind maps are fun and great way to plan ideas. I&#8217;m using MindMeister to plan out bigger projects.</li>
<li><a href="http://asana.com/">Asana</a><br />
We are using Asana at the company I work for. It is a brilliant, lightweight project management tool with a butt load of great features for bigger teams.</li>
<li><a href="http://mention.net/">Mention.net</a><br />
I came across this service last week. They claim to be a better alternative for Google Alerts. So far it is very promising. It is an amazing way to keep track the mentions of my projects</li>
<li><a href="http://www.streak.com/">Streak</a><br />
I deal with a bunch of customer emails lately and Streak is a great way to get your customers organized right in your gmail inbox. It basically creates a new tab in your inbox where you can manage your customers, place them into boxes related to your sales / support flow and assign custom attributes to them. It also comes with a bunch of tiny hacks like my favorite one: &#8220;scheduled emails&#8221;. The only downside is it currently only works with Chrome</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://lepunk.co.uk/tools-for-productivity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scraping the sh*t out of the interwebz &#8211; Part #1</title>
		<link>http://lepunk.co.uk/scraping-the-sht-out-of-the-interwebz-part-1/</link>
		<comments>http://lepunk.co.uk/scraping-the-sht-out-of-the-interwebz-part-1/#comments</comments>
		<pubDate>Sun, 10 Mar 2013 14:44:36 +0000</pubDate>
		<dc:creator>lePunk</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[domdocument]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[scraping]]></category>

		<guid isPermaLink="false">http://lepunk.co.uk/?p=83</guid>
		<description><![CDATA[PHP&#8217;s curl library is by far my favorite tool. Combined with DOMDocument it provides a powerful API to gather content from remote sites or manipulate remote forms programmatically. In the upcoming couple of articles / tutorials I will show you how to use it to scrape website content, login to remote interfaces, use it as a [...]]]></description>
				<content:encoded><![CDATA[<p>PHP&#8217;s curl library is by far my favorite tool. Combined with DOMDocument it provides a powerful API to gather content from remote sites or manipulate remote forms programmatically. In the upcoming couple of articles / tutorials I will show you how to use it to scrape website content, login to remote interfaces, use it as a gateway for dealing with APIs and at the end how to do cool (and dodgy) stuff like breaking captchas.</p>
<p>In my examples I will use my own wrapper class (<a href="https://github.com/lepunk/curl.class.php">curl.class.php</a>) which I have been gradually building during the past couple years, expanding it with additional features as I needed to use them.</p>
<p>Ok, so the first example will be a pretty basic one: The goal is to get listings from Ebay based on a keyword passed in to our script.</p>
<p>The whole script will look something like this. I will explain it line by line below:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'keyword'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'keyword'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$keyword</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'keyword'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$keyword</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;usage: php <span style="color: #006699; font-weight: bold;">{$argv[0]}</span> [keyword]<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;../curl.class.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$curl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Curl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.ebay.com/sch/i.html?_trksid=p2050601.m570.l1313&amp;_nkw=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$keyword</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;_sacat=0&amp;_from=R40&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHttpCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;=</span><span style="color: #cc66cc;">200</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHttpCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">400</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">@</span><span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadHTML</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$tables</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'table'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$tables</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">length</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tables</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;itemtype&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;http://schema.org/Offer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$h4s</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tables</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'h4'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$h4s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">length</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$links</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$h4s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$links</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">length</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$item_title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$links</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">textContent</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$item_url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$links</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;href&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item_title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$item_url</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unexpected error occured<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Basic enough, right? The top couple of lines doesn&#8217;t need any explanation I guess. Check if a &#8220;keyword&#8221; parameter is passed in via the GET request or via the command line. If not prompt the user an error message.</p>
<p>The first thing we need to do when scraping a site is to check for the URL structure. If you go to ebay.com and do a search for something you will see the keyword appears in the URL. Therefor:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$curl</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Curl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.ebay.com/sch/i.html?_trksid=p2050601.m570.l1313&amp;_nkw=&quot;</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$keyword</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;_sacat=0&amp;_from=R40&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This will download the page and store the html in the $page variable. Now it is unlikely but possible that ebay.com is down so it is better to check the response code. We will accept any 2xx, 3xx response code (my curl class handles redirects by default):</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHttpCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&gt;=</span><span style="color: #cc66cc;">200</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$curl</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHttpCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;</span><span style="color: #cc66cc;">400</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span></pre></td></tr></table></div>

<p>Neat, we have the html, lets initialize the DOM parser:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #339933;">@</span><span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadHTML</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Notice the @ sign infront of the loadHTML method call. I know its ugly and I should really use my own error handler, etc. but unfortunately i&#8217;m lazy and DOMDocument is like a teenage girl at Valentines day: she wants everything to be perfect and the exact same way she imagined, otherwise she starts to complain. Anyway, now it&#8217;s time to have a look at ebay&#8217;s html structure via Firebug.</p>
<p><a href="http://lepunk.co.uk/wp-content/uploads/2013/03/curl_1.png"><img class="alignnone size-medium wp-image-86" alt="curl_1" src="http://lepunk.co.uk/wp-content/uploads/2013/03/curl_1-300x240.png" width="300" height="240" /></a></p>
<p>Good, it will be easy. It looks like each results are in their own table with itemtype=&#8221;http://schema.org/Offer&#8221; (god bless the micro formats). Also the title and the URL could be gathered by getting the one and only H4 tag&#8217;s first link&#8217;s text content and href attribute. Some people prefer using xpath to get to the target element directly but I found drilling down the DOM more reliable:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;">	<span style="color: #000088;">$tables</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'table'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$tables</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">length</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tables</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;itemtype&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">!=</span><span style="color: #0000ff;">&quot;http://schema.org/Offer&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$h4s</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tables</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'h4'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$h4s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">length</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$links</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$h4s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$links</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">length</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$item_title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$links</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">textContent</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$item_url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$links</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;href&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item_title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$item_url</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And we are done. It is a basic example but as you can tell it is a really powerful way to gather data from remote sites. What can you use it for? Well, pretty much anything. Add stock prices to your site by scraping Google finance, add customized Ebay affiliate widget to your sidebar based on your keywords, sky is the limit.</p>
]]></content:encoded>
			<wfw:commentRss>http://lepunk.co.uk/scraping-the-sht-out-of-the-interwebz-part-1/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>A way to learn</title>
		<link>http://lepunk.co.uk/a-way-to-learn/</link>
		<comments>http://lepunk.co.uk/a-way-to-learn/#comments</comments>
		<pubDate>Thu, 07 Mar 2013 22:18:16 +0000</pubDate>
		<dc:creator>lePunk</dc:creator>
				<category><![CDATA[Rants / Opinions]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[notstalgia]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[too many words]]></category>
		<category><![CDATA[way to learn]]></category>

		<guid isPermaLink="false">http://lepunk.co.uk/?p=79</guid>
		<description><![CDATA[Being a coder is a unique profession I guess. I mean probably this is one of the few jobs you can do and be good at without learning it in school. If you think about it there are not so many professions which you can gain experience in, in an unprofessional environment. Someone who want [...]]]></description>
				<content:encoded><![CDATA[<p>Being a coder is a unique profession I guess. I mean probably this is one of the few jobs you can do and be good at without learning it in school. If you think about it there are not so many professions which you can gain experience in, in an unprofessional environment. Someone who want to be an architect can&#8217;t just go ahead and build houses from home for practice, but we can do code in our dorm. This is unique</p>
<p>The other day a friend of mine (who is learning to be an architect) asked me if I can give him some pointers on where to start learning to code. He wants to learn programming as a fallback in case he doesn&#8217;t get a job in his profession of choice. It made me think, where did I start?</p>
<p>I&#8217;ve never been huge fan of books never mind mainstream education. I spent 6 years at university supposedly studying programming and mathematics on a 3 years long course and learned nothing useful. Yet, I&#8217;m doing programming at a successful company and I believe I&#8217;m quite good at it.</p>
<p>My father was never good with computers but he was a very clever person and I guess he saw that computers are the future. When I was six (at 1991) he bought me an XT with 640kb of ram and 10Mb of HDD (with a fucking turbo button) for Christmas and it changed my life. Of course at the first couple of years I was only using it for games as any kid would do</p>
<p>My first memory of doing anything serious with computers dates back to elementary school. I&#8217;m from Hungary which is an ex-soviet country in the middle of eastern Europe (FIY) and such ass it was bit late on teaching modern science. But I was lucky, my class was an experimental class and we got 45 minutes every 2nd week in the computer lab. And my teacher was the greatest influence in my life. He agreed to stay in the computer lab after school hours and teach some of us code in Pascal. It was great, he taught us how to count sheeps with for loops how to create procedures and functions, etc. I was hooked. Then at some point he showed us how to use the graph library. Now we are talking. Endless possibilities opened</p>
<p>In Hungary those days having a &#8220;western board game&#8221; like Monopoly was a big deal. I was very jealous of my friend who&#8217;s father worked in Germany for having one. When I learned about what is possible with Pascal&#8217;s graph library I decided to make my own Monopoly board game on the computer. Of course I had no idea how will I accomplish it but I started anyway. Back then there was no Google, I had no money to buy books so my only pointers were my teacher and the built in help in Borland Pascal. It took me one year but I did it. I had a shitty game. But during that year I learned a lot about how programming works, techniques, data structures, all because I had a goal and in order to achieve it I had to learn them. My program was inefficient, ugly, but it was mine.</p>
<p>After this &#8220;project&#8221; I made a bunch of little games and pranks (i made a fake windows 95 pascal program which draw the windows desktop based on the .ini files but when you clicked on anything it wrote &#8220;Tamas Rulez&#8221; on the screen when you clicked on antyhing) for my own and my friend&#8217;s amusement. I fucking loved it</p>
<p>Then came the internet age and I was in high school. My father subscribed me to a magazine which was pretty much the only programming related magazine at the end of the 90&#8242;s in Hungary. In one of the issues I read about PHP3 and how it deals with mySQL. My mind started to think: &#8220;I can do something with this&#8221;. There was a very popular tamagochi kind of site back then where you can have your own pet camel and teach him some random shit. I was a mohawk wearing teen so I decided to make a site where you can have your own punk and teach / buy him stuff. So I started building it knowing nothing about server-side programming. Spent about 6 months and never actually finished it but learned a lot about the world of the internet which is invaluable.</p>
<p>Ok, this post is becoming very long. I guess the point I&#8217;m trying to make is in my opinion the best way to learn is to set an almost unachievable goal and try to achieve it. Even if you fail you learn so much, it worth it.  Now you have the ability to google anything, which I never had. Try making it in your own way, by your own logic. It might not be something what changes the world but you gain experience. If a problem bothers you enough, it keeps you up at night thinking about it, then you born to do this</p>
]]></content:encoded>
			<wfw:commentRss>http://lepunk.co.uk/a-way-to-learn/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Monitoring what http requests your iPhone makes</title>
		<link>http://lepunk.co.uk/monitoring-what-http-requests-your-iphone-makes/</link>
		<comments>http://lepunk.co.uk/monitoring-what-http-requests-your-iphone-makes/#comments</comments>
		<pubDate>Sun, 24 Feb 2013 01:49:47 +0000</pubDate>
		<dc:creator>lePunk</dc:creator>
				<category><![CDATA[Geek]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[pocket sniff]]></category>
		<category><![CDATA[requests]]></category>

		<guid isPermaLink="false">http://lepunk.co.uk/?p=76</guid>
		<description><![CDATA[The other day I was curious if there is a way to see what http requests my iphone makes. I thought if i can do it, it might uncover some cool hidden APIs. I was especially curious about what my IMDB app does since IMDB doesn&#8217;t have a public API (i wrote my own but [...]]]></description>
				<content:encoded><![CDATA[<p>The other day I was curious if there is a way to see what http requests my iphone makes. I thought if i can do it, it might uncover some cool hidden APIs. I was especially curious about what my IMDB app does since IMDB doesn&#8217;t have a public API (i wrote my own but it relies on curling the site which keeps changing every 2 weeks or so)</p>
<p>Turns out there is a solution. It is called <a href="http://www.charlesproxy.com/">Charles Proxy</a>. It is a nifty little thin proxy layer which is built for debugging and monitoring your network traffic in a visual way.</p>
<p>The setup is easy:</p>
<ol>
<li>Install Charles Proxy (it&#8217;s available for windows, linux and osx)</li>
<li>Connect your iPhone to the wifi network your computer is on</li>
<li>Under the wifi connection&#8217;s configuration set the manual proxy to be your computer&#8217;s ip and Charles Proxy&#8217;s port</li>
<li>Boom, all set. Now all your mobile traffic will go through the proxy and gets logged</li>
</ol>
<p>Unfortunately as I expected most of the traffic is through https so it&#8217;s encoded. Not sure if its a requirement from Apple or just the apps I&#8217;m using are trying to be secure, but it is still a cool little hack.</p>
]]></content:encoded>
			<wfw:commentRss>http://lepunk.co.uk/monitoring-what-http-requests-your-iphone-makes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tech support tips for developers</title>
		<link>http://lepunk.co.uk/tech-support-tips-for-developers/</link>
		<comments>http://lepunk.co.uk/tech-support-tips-for-developers/#comments</comments>
		<pubDate>Sat, 16 Feb 2013 16:47:56 +0000</pubDate>
		<dc:creator>lePunk</dc:creator>
				<category><![CDATA[Rants / Opinions]]></category>
		<category><![CDATA[customer]]></category>
		<category><![CDATA[tech support]]></category>

		<guid isPermaLink="false">http://lepunk.co.uk/?p=68</guid>
		<description><![CDATA[My support guy&#8217;s laptop broke after I released the latest version of my CMS about 3 weeks ago which means I have to answer customer emails. As a developer I&#8217;m not really a people person so this is a real challenge for me. The workload is not huge, it is about 50 emails / day [...]]]></description>
				<content:encoded><![CDATA[<p>My support guy&#8217;s laptop broke after I released the latest version of my CMS about 3 weeks ago which means I have to answer customer emails. As a developer I&#8217;m not really a people person so this is a real challenge for me. The workload is not huge, it is about 50 emails / day and about 75% of them are pre-sales questions which are generally easy to answer. The problem is the remaining 25%. The script itself is not meant for newbies, but this doesn&#8217;t stop newbies to buy it and they require a special kind of customer care. During this short time of providing support I gained some knowledge on how the mind of this strange animal&#8217;s, the customer&#8217;s mind works. I realized that there are some easy steps to follow to keep them happy even when you can&#8217;t solve their problems immediately:</p>
<ul>
<li><strong>Call them by their name<br />
</strong>Yeah, I know this supposed to be the basic etiquette, but in some cases its difficult. I&#8217;m dealing with customers who can barely speak English, or they just started using the internet so they don&#8217;t sign their Emails. In those cases I figured looking up their email in our DB for their name or even a quick facebook search (creepy?) helps a lot to set the tone to be a bit more personal. For some reason even the angriest customer becomes bit calmer when you call them by their first name</li>
<li><strong>The illusion of a big company</strong><br />
By using the word &#8220;we&#8221; instead of &#8220;I&#8221; gives the customer the feeling that he is dealing with an organization rather than an individual developer and people tend to be more polite and understanding with a corporate then to a guy in his bedroom. Also I&#8217;m signing my mails as &#8220;Account manager&#8221;. You get the idea</li>
<li><strong>Don&#8217;t know the answer? Reply immediately</strong><br />
Sometimes the customers are reporting bugs which I don&#8217;t have an immediate answer for. In those cases I get back to them as soon as possible with some bullshit like &#8220;Thank you for reporting this problem. I&#8217;ve escalated this issue to the highest priority and our developer is working on the resolution&#8221;. This will give you enough time to actually solve the problem without being bombarded with emails by the customer</li>
<li><strong>Know the answer? Hold back</strong><br />
When I know the exact solution I usually wait at least 2 hours to reply? Why? Because otherwise the customer will think this is a real time conversation and you will end up with an endless thread of questions. A two hours delay won&#8217;t make him loose faith in your company but it is enough time to let him realize that you have other customers to take care of.</li>
<li><strong>Is he an idiot?</strong><br />
As a developer it is easy to tell if a customer is&#8230; well &#8220;impaired technologically&#8221;. In those cases I rather go the extra mile, ask for his ftp / cpanel details and fix his problem by myself. I could give him the instructions to get it fixed by him but that would result in way more emails, pointless conversation and general lose of faith in humanity.</li>
</ul>
<p>I know these techniques are basic but they helped me to get through the past 3 weeks. And while I have you attention let me grab the moment to praise the nameless heroes known as &#8220;tech support&#8221; who deal with morons as a profession: your patience guys is extraordinary, no idea how you do it</p>
]]></content:encoded>
			<wfw:commentRss>http://lepunk.co.uk/tech-support-tips-for-developers/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>Facebook Hacker Cup 2013: My solutions</title>
		<link>http://lepunk.co.uk/facebook-hacker-cup-2013-my-solutions/</link>
		<comments>http://lepunk.co.uk/facebook-hacker-cup-2013-my-solutions/#comments</comments>
		<pubDate>Mon, 28 Jan 2013 11:56:19 +0000</pubDate>
		<dc:creator>lePunk</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Geek]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[hacker cup 2013]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://lepunk.co.uk/?p=52</guid>
		<description><![CDATA[Task 1: Beautiful strings When John was a little kid he didn&#8217;t have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could&#8230; he evaluated the beauty of strings in a quest to discover the most beautiful string in the world. Given a [...]]]></description>
				<content:encoded><![CDATA[<h2>Task 1: Beautiful strings</h2>
<blockquote><p>When John was a little kid he didn&#8217;t have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could&#8230; he evaluated the beauty of strings in a quest to discover the most beautiful string in the world.</p>
<p>Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it.</p>
<p>The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn&#8217;t care about whether letters are uppercase or lowercase, so that doesn&#8217;t affect the beauty of a letter. (Uppercase &#8216;F&#8217; is exactly as beautiful as lowercase &#8216;f&#8217;, for example.)</p>
<p>You&#8217;re a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?</p>
<p><strong>Input</strong><br />
The input file consists of a single integer m followed by m lines.</p>
<p><strong>Output</strong><br />
Your output should consist of, for each test case, a line containing the string &#8220;Case #x: y&#8221; where x is the case number (with 1 being the first case in the input file, 2 being the second, etc.) and y is the maximum beauty for that test case.</p>
<p><strong>Constraints</strong><br />
5 ≤ m ≤ 50<br />
2 ≤ length of s ≤ 500</p></blockquote>
<p>My solution for this problem was fairly straight forward. Not the fastest solution I guess, but it works: get the string, count the unique alpha characters in it and add up the &#8220;beauty&#8221;</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;input.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>/i&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row_count</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row_count</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;invalid input<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;output.txt&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;w+&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$case</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #000088;">$case</span><span style="color: #339933;">&lt;=</span><span style="color: #000088;">$row_count</span><span style="color: #339933;">;</span><span style="color: #000088;">$case</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$case</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/[^a-zA-Z]/i&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Case #<span style="color: #006699; font-weight: bold;">$case</span>: 0<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Case #<span style="color: #006699; font-weight: bold;">$case</span>: 0<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$letters</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$j</span><span style="color: #339933;">&lt;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$j</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$letters</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$letters</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$letters</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000088;">$total_beauty</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$current_beauty</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">26</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">arsort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$letters</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$letters</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$letter</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$total_beauty</span><span style="color: #339933;">+=</span><span style="color: #000088;">$count</span><span style="color: #339933;">*</span><span style="color: #000088;">$current_beauty</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$current_beauty</span><span style="color: #339933;">--;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Case #<span style="color: #006699; font-weight: bold;">$case</span>: <span style="color: #006699; font-weight: bold;">$total_beauty</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Case #<span style="color: #006699; font-weight: bold;">$case</span>: <span style="color: #006699; font-weight: bold;">$total_beauty</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h2>Task 2: Balanced Smileys</h2>
<blockquote><p>
Your friend John uses a lot of emoticons when you talk to him on Messenger. In addition to being a person who likes to express himself through emoticons, he hates unbalanced parenthesis so much that it makes him go <img src='http://lepunk.co.uk/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>Sometimes he puts emoticons within parentheses, and you find it hard to tell if a parenthesis really is a parenthesis or part of an emoticon.</p>
<p>A message has balanced parentheses if it consists of one of the following:<br />
- An empty string &#8220;&#8221;<br />
- One or more of the following characters: &#8216;a&#8217; to &#8216;z&#8217;, &#8216; &#8216; (a space) or &#8216;:&#8217; (a colon)<br />
- An open parenthesis &#8216;(&#8216;, followed by a message with balanced parentheses, followed by a close parenthesis &#8216;)&#8217;.<br />
- A message with balanced parentheses followed by another message with balanced parentheses.<br />
- A smiley face &#8220;:)&#8221; or a frowny face &#8220;:(&#8221;</p>
<p>Write a program that determines if there is a way to interpret his message while leaving the parentheses balanced.<br />
Input</p>
<p> The first line of the input contains a number T (1 ≤ T ≤ 50), the number of test cases.<br />
 The following T lines each contain a message of length s that you got from John.<br />
Output</p>
<p> For each of the test cases numbered in order from 1 to T, output &#8220;Case #i: &#8221; followed by a string stating whether or not it is possible that the message had balanced parentheses. If it is, the string should be &#8220;YES&#8221;, else it should be &#8220;NO&#8221; (all quotes for clarity only)<br />
Constraints<br />
1 ≤ length of s ≤ 100</p></blockquote>
<p>I spent quite a lot of time, about 40 minutes on this one. I ended up with a recursive function which relies on a regular expression: &#8220;/(\(.*)(:\))?(:\()?(\))/U&#8221;. This regex will match the parts of the string which are in brackets even if the parts are containing a smiley. Then it is just a loop for counting the opening and closing parentheses.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;input.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>/i&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$row_count</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row_count</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;invalid input<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> replace_once<span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$needle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$replacement</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$pos</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$pos</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">substr_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$haystack</span><span style="color: #339933;">,</span> <span style="color: #000088;">$replacement</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pos</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$needle</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$haystack</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> is_balanced<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #990000;">preg_match_all</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/(\(.*)(:\))?(:\()?(\))/U&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$remaining</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$balanced</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$remaining</span> <span style="color: #339933;">=</span> replace_once<span style="color: #009900;">&#40;</span><span style="color: #000088;">$remaining</span><span style="color: #339933;">,</span> <span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$val</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$balanced</span> <span style="color: #339933;">=</span> is_balanced<span style="color: #009900;">&#40;</span><span style="color: #000088;">$val</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$balanced</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> is_balanced<span style="color: #009900;">&#40;</span><span style="color: #000088;">$remaining</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;:)&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;:(&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$open</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$open</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$open</span><span style="color: #339933;">--;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;(&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$open</span><span style="color: #339933;">++;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$open</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;output.txt&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;w+&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$case</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><span style="color: #000088;">$case</span><span style="color: #339933;">&lt;=</span><span style="color: #000088;">$row_count</span><span style="color: #339933;">;</span><span style="color: #000088;">$case</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$case</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>is_balanced<span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Case #<span style="color: #006699; font-weight: bold;">$case</span>: YES<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Case #<span style="color: #006699; font-weight: bold;">$case</span>: YES<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Case #<span style="color: #006699; font-weight: bold;">$case</span>: NO<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;Case #<span style="color: #006699; font-weight: bold;">$case</span>: NO<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Task 3: Find the min</h2>
<blockquote><p>
After sending smileys, John decided to play with arrays. Did you know that hackers enjoy playing with arrays? John has a zero-based index array, m, which contains n non-negative integers. However, only the first k values of the array are known to him, and he wants to figure out the rest. </p>
<p> John knows the following: for each index i, where k <= i < n, m[i] is the minimum non-negative integer which is *not* contained in the previous *k* values of m. </p>
<p> For example, if k = 3, n = 4 and the known values of m are [2, 3, 0], he can figure out that m[3] = 1. </p>
<p> John is very busy making the world more open and connected, as such, he doesn't have time to figure out the rest of the array. It is your task to help him. </p>
<p> Given the first k values of m, calculate the nth value of this array. (i.e. m[n - 1]). </p>
<p> Because the values of n and k can be very large, we use a pseudo-random number generator to calculate the first k values of m. Given positive integers a, b, c and r, the known values of m can be calculated as follows:<br />
m[0] = a<br />
m[i] = (b * m[i - 1] + c) % r, 0 < i < k</p>
<p>Input<br />
 The first line contains an integer T (T <= 20), the number of test cases.<br />
 This is followed by T test cases, consisting of 2 lines each.<br />
 The first line of each test case contains 2 space separated integers, n, k (1 <= k <= 10^5, k < n <= 10^9).<br />
 The second line of each test case contains 4 space separated integers a, b, c, r (0 <= a, b, c <= 10^9, 1 <= r <= 10^9). </p>
<p>Output<br />
 For each test case, output a single line containing the case number and the nth element of m.</p></blockquote>
<p>Well, I failed this task. Not because my solution wasn&#8217;t working. It wasn&#8217;t fast enough in some cases. I considered seed (it is not my first time on the Hacker cup) and tested it with large arrays but I missed a case when my script fails. The key speed up was when I realized that the analyzed array slices are repeating. They are repeating after every Kth element, therefor:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$k</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$n</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$k</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>It gave me a huge speed up, but apparently not enough. I tried to run my script with the test cases provided by Facebook&#8217;s system and it didn&#8217;t finish in the 6 minutes limit even on the high-compute Amazon EC2 instances. Epic fail. Well next time. Anyway, my solution for task #3:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #990000;">set_time_limit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;memory_limit&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;2048M&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;input.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>/i&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$rows</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$task_count</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$task_count</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;invalid input<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$case</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$case</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$task_count</span><span style="color: #339933;">;</span> <span style="color: #000088;">$case</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000088;">$m</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">+</span><span style="color: #000088;">$case</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$n</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// count($m);</span>
	<span style="color: #000088;">$k</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// known length</span>
&nbsp;
	<span style="color: #000088;">$tmp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$rows</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">+</span><span style="color: #000088;">$case</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tmp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$k</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$b</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$c</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$r</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// reduce n</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$k</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$n</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$k</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$k</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$n</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #000088;">$k</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">&lt;</span><span style="color: #000088;">$n</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #000088;">$current_slice</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_slice</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$m</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #339933;">-</span><span style="color: #000088;">$k</span><span style="color: #339933;">,</span> <span style="color: #000088;">$k</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #990000;">asort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_slice</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$min</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_slice</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$min</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$min</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000088;">$test</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$current_slice</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$test</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$min</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$min</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$min</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$min</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$test</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$m</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$min</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Case #&quot;</span><span style="color: #339933;">.</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$case</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;: &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$min</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://lepunk.co.uk/facebook-hacker-cup-2013-my-solutions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>We are the worst people so far</title>
		<link>http://lepunk.co.uk/we-are-the-worst-people-so-far/</link>
		<comments>http://lepunk.co.uk/we-are-the-worst-people-so-far/#comments</comments>
		<pubDate>Fri, 25 Jan 2013 22:00:49 +0000</pubDate>
		<dc:creator>lePunk</dc:creator>
				<category><![CDATA[Rants / Opinions]]></category>
		<category><![CDATA[community]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[iq]]></category>
		<category><![CDATA[stupid people]]></category>

		<guid isPermaLink="false">http://lepunk.co.uk/?p=46</guid>
		<description><![CDATA[We have the most amazing technology&#8230; &#8220;and it is wasted on the sh*ttiest generation of piece of sh*t assholes of all time&#8221; People are stupid, they really are. Especially users. Usually I don&#8217;t mind if someone asks me stupid questions, or react to something I do rudely or stupidly. What pisses me off is when [...]]]></description>
				<content:encoded><![CDATA[<p><iframe width="550" height="415" src="http://www.youtube.com/embed/KpUNA2nutbk" frameborder="0" allowfullscreen></iframe></p>
<p>We have the most amazing technology&#8230; &#8220;and it is wasted on the sh*ttiest generation of piece of sh*t assholes of all time&#8221;</p>
<p>People are stupid, they really are. Especially users. Usually I don&#8217;t mind if someone asks me stupid questions, or react to something I do rudely or stupidly. What pisses me off is when someone is not making an effort to figure out the answer by himself before asking. Or making an opposing comment on something I post without reasoning.</p>
<p>I own a couple of quite large communities and the users sometimes scare me. One of the communities is a <a href="http://www.facebook.com/bandwarsdotorg">Facebook fan</a> page with like 350k, mostly Hungarian fans. To be honest the page is not aimed to the most educated audience but sometimes their stupidity freaks me out. For example yesterday I posted a funny picture about a laughing Jesus with a caption &#8220;Yes, your girlfriend is saying my name while having sex&#8221; (i believe in loads of things Jesus and the Christian god is not one of them). I expected some Christian comments but not this. One of the guys commented:</p>
<p>&#8220;<strong>God will punish you! If I would know who are you I would beat you to death with crowbar</strong>&#8221;</p>
<p>WTF man? That is very Christian of you. This way we will truly get your point. Rock on.</p>
<p>My other community is a social network built around TV shows and movies. Members can watch them, comment them, like them, etc. About 800.000 people using it monthly and the feedback is mostly positive. Then sometimes I get &#8220;error reports&#8221; like this:</p>
<p><strong>&#8220;You f*cking asshole. I can&#8217;t watch Twilight on my mobile. You better fix it&#8221;</strong></p>
<p>How on earth I owe you something? The service is free of charge, you haven&#8217;t lost any money by not being able to watch (the lamest ever) movie. Of course if you ask me politely I will try to figure out a solution for you but not like this.</p>
<p>I think these people are not behaving like this in the &#8220;real world&#8221;. I think most people don&#8217;t understand that when you are communicating with someone online is exactly the same as talking to someone on the street. You don&#8217;t say &#8220;Nice tits&#8221; to a hot girl on the street at broad daylight, but some people do comment stuff like that on Facebook. You don&#8217;t go up on the stage after an amazing guitar solo to tell the guitarist &#8220;you are a wanker, Slash is way better&#8221;, but some (apparently most) people do it on Youtube.</p>
<p>Anyway, sometimes I wish browsers would implement an initial IQ or EQ test before letting anyone to surf the web, even if ad revenues would drop (stupid people tend to click more ads)</p>
]]></content:encoded>
			<wfw:commentRss>http://lepunk.co.uk/we-are-the-worst-people-so-far/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Howto: Install OSX Lion under Windows 7 and VMware</title>
		<link>http://lepunk.co.uk/howto-install-osx-lion-under-windows-7-and-vmware/</link>
		<comments>http://lepunk.co.uk/howto-install-osx-lion-under-windows-7-and-vmware/#comments</comments>
		<pubDate>Thu, 17 Jan 2013 21:01:38 +0000</pubDate>
		<dc:creator>lePunk</dc:creator>
				<category><![CDATA[Geek]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vmware]]></category>

		<guid isPermaLink="false">http://lepunk.co.uk/?p=41</guid>
		<description><![CDATA[So I decided to learn objective C, partly because it&#8217;s been a long time since I developed anything besides web apps and partly because I have a couple app ideas in my mind. Anyway, it turns out there is no option to code for the iPhone on Windows 7 (besides Visual Studio. Thanks, but no [...]]]></description>
				<content:encoded><![CDATA[<p>So I decided to learn objective C, partly because it&#8217;s been a long time since I developed anything besides web apps and partly because I have a couple app ideas in my mind. Anyway, it turns out there is no option to code for the iPhone on Windows 7 (besides Visual Studio. Thanks, but no thanks) so I decided to install OSX Lion using VMware. I read a whole bunch of tutorials on how to do it and finally succeeded. Here is a simple step by step guide so you can try it yourself</p>
<p><strong>What will you need?</strong></p>
<ol>
<li>You need a bootable OSX Lion image. Not sure how to legally acquire it but here is a link for a <a href="http://kat.ph/mac-os-x-lion-retail-bootable-vmdk-for-vmware-t5685072.html">torrent file</a></li>
<li>VMWare Workstation. Now this is a paid software but a quick search on <a href="http://isohunt.com">isohunt</a> will help you out. I recommend getting the latest version (9.0) if you can</li>
<li>VMWare hard drive files. You can download them <a href="http://www.mediafire.com/?r1qata1nccpkb3h">from here</a> (self-extracting exe)</li>
<li>&#8220;Additional files&#8221;. <a href="http://www.mediafire.com/?a42mf9t1z2sz266">Various tools</a></li>
</ol>
<p><strong>Ready? Let&#8217;s install!</strong></p>
<ol>
<li>Install VMware workstation. Don&#8217;t run it just yet.</li>
<li>From &#8220;Additional files&#8221; extract &#8220;macosx_guest_vmware_7.tar.gz&#8221; and run &#8220;windows.bat&#8221; as an Administrator. This will patch VMware workstation to enable you to run OSX</li>
<li>Start the self-extracting &#8220;Mac OS X Lion VMware Files.exe&#8221; (point #3 in the &#8220;What will you need section&#8221;</li>
<li>Start VMware workstation and click on &#8220;Open a Virtual Machine&#8221; and browse to the folder extracted in the previous step. Open &#8220;Mac OSX Lion/Mac OS X Lion.vmx&#8221;</li>
<li>Click on &#8220;Edit virtual machine settings&#8221; and remove the CD / DVD drive</li>
<li>Still in &#8220;Edit virtual machine settings&#8221; click &#8220;Add&#8230;&#8221; then &#8220;Hard Disk&#8221; then &#8220;Use an existing virtual disk and browse to the downloaded OSX Lion image (the big one)</li>
<li>Click &#8220;Power on this virtual machine&#8221; to launch the virtual machine. If it asks you a random question (can&#8217;t remember what exactly was it) answer &#8220;I copied it&#8221;</li>
<li>The installer should start in a couple of moments. Just follow the installation steps and you are ready to go</li>
</ol>
<p><strong>Couple of problems / tweaks</strong></p>
<ol>
<li>The image by default will only work with 1024&#215;768 resolution which can be really annoying. To solve this copy &#8220;darvin.iso&#8221; from your &#8220;Sysprobs Lion Files&#8221; to an usb drive and install it on your OSX system. After the install and the reboot you will be able to choose whatever resolution you want</li>
<li>Sound doesn&#8217;t work. I&#8217;m still trying to figure out how to solve this</li>
<li>I tried to install OSX on my work computer which has an AMD processor but it gave me errors. There are some tutorials around the web to solve that problem, but its time consuming so I just didn&#8217;t bother. Just google the exact error message and you will be able to solve it if you really want. The method described here works perfectly for my Intel i7 desktop and my Intel i5 laptop (I guess it should work on any Intel chipset systems)</li>
</ol>
<p>Have fun</p>
]]></content:encoded>
			<wfw:commentRss>http://lepunk.co.uk/howto-install-osx-lion-under-windows-7-and-vmware/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
