<?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>Hrishikesh Barua &#124; code@deepinspace &#187; programming</title>
	<atom:link href="http://code.deepinspace.net/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.deepinspace.net</link>
	<description></description>
	<lastBuildDate>Wed, 25 Jan 2012 14:23:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Some string matching</title>
		<link>http://code.deepinspace.net/2011/07/20/some-string-matching/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=some-string-matching</link>
		<comments>http://code.deepinspace.net/2011/07/20/some-string-matching/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 16:52:36 +0000</pubDate>
		<dc:creator>hrish</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[levenshtein]]></category>
		<category><![CDATA[string matching]]></category>

		<guid isPermaLink="false">http://code.deepinspace.net/?p=258</guid>
		<description><![CDATA[This puzzle from Facebook&#8217;s engineering puzzles page is a good introduction to string matching. A solution is possible using an algorithm called Levenshtein distance. Let&#8217;s take the example on the puzzle page &#8211; TIHS SENTENTCNES ISS NOUT VARRRY GOUD It&#8217;s a munged up version of THIS SENTENCE IS NOT VERY GOOD The original problem statement [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.facebook.com/careers/puzzles.php?puzzle_id=17" target="_blank">This puzzle</a> from Facebook&#8217;s engineering puzzles page is a good introduction to string matching.<br />
A solution is possible using an algorithm called <a href="http://en.wikipedia.org/wiki/Levenshtein_distance" target="_blank">Levenshtein distance</a>.</p>
<p>Let&#8217;s take the example on the puzzle page &#8211; </p>
<blockquote><p>TIHS SENTENTCNES ISS NOUT VARRRY GOUD</p></blockquote>
<p>It&#8217;s a munged up version of</p>
<blockquote><p>THIS SENTENCE IS NOT VERY GOOD</p></blockquote>
<p>The original problem statement involves minimizing the score (number of changes necessary) to transform each word in the munged up sentence into words that are in a predefined <a href="http://www.facebook.com/jobs_puzzles/twl06.txt" target="_blank">wordlist</a>. It says nothing about grammatical correctness of the result.</p>
<p>So going by that, the score and the output sentence from my <a href="https://github.com/talonx/python-scribbles/blob/master/fb/breathalyzer.py" target="_blank">implementation</a> are respectively, 8 and</p>
<blockquote><p>TICS SENTENCES IDS NOT VAGARY GAUD</p></blockquote>
<p>From the standpoint of the original problem, this solution is correct.<br />
But not from a grammatical point of view. After some poking around, I realized it&#8217;s not possible for this program to spew out the correct version of the sentence without it being aware of English grammar rules. </p>
<p><strong>P.S. </strong>Without getting into grammar correction, of which I have no idea how to implement, I made a small change in the program as an experiment. The <a href="https://github.com/talonx/python-scribbles/blob/master/fb/breathalyzer2.py" target="_blank">second version</a> favours a smaller word whenever the scores for two are equal.<br />
And the result is (Score remains the same)</p>
<blockquote><p>TIS SENTENCES IS NOT VARY GOD</p></blockquote>
<script type="text/javascript" src="http://cdn.socialtwist.com/2008071112-2/script.js"></script><a class="st-taf" href="http://tellafriend.socialtwist.com" onclick="return false;" style="border:0;padding:0;margin:0;"><img alt="SocialTwist Tell-a-Friend" style="border:0;padding:0;margin:0;" src="http://images.socialtwist.com/2008071112-2/button.png"onmouseout="STTAFFUNC.hideHoverMap(this)" onmouseover="STTAFFUNC.showHoverMap(this, '2008071112-2', 'http%3A%2F%2Fcode.deepinspace.net%2F2011%2F07%2F20%2Fsome-string-matching%2F', 'Some+string+matching')" onclick="STTAFFUNC.cw(this, {id:'2008071112-2', link: 'http%3A%2F%2Fcode.deepinspace.net%2F2011%2F07%2F20%2Fsome-string-matching%2F', title: 'Some+string+matching' });"/></a>]]></content:encoded>
			<wfw:commentRss>http://code.deepinspace.net/2011/07/20/some-string-matching/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Working through UNP</title>
		<link>http://code.deepinspace.net/2011/01/09/working-through-unp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=working-through-unp</link>
		<comments>http://code.deepinspace.net/2011/01/09/working-through-unp/#comments</comments>
		<pubDate>Sun, 09 Jan 2011 06:45:24 +0000</pubDate>
		<dc:creator>hrish</dc:creator>
				<category><![CDATA[c]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[network programming]]></category>

		<guid isPermaLink="false">http://code.deepinspace.net/?p=210</guid>
		<description><![CDATA[As I wrote previously, I&#8217;ve been working through Richard Stevens&#8217; Unix Network Programming (3rd Ed) Vol 1. It covers the basics of the Sockets API in UNIX and similar OSs. Unfortunately I&#8217;ve been able to devote time for this mostly on the weekends. This translates to slow progress because UNP goes into a lot of [...]]]></description>
			<content:encoded><![CDATA[<p>As I wrote <a href="http://code.deepinspace.net/2010/11/23/falling-through/" target="_blank">previously</a>, I&#8217;ve been working through Richard Stevens&#8217; Unix Network Programming (3rd Ed) Vol 1. It covers the basics of the Sockets API in UNIX and similar OSs.</p>
<p>Unfortunately I&#8217;ve been able to devote time for this mostly on the weekends. This translates to slow progress because UNP goes into a lot of depth about everything. This is a good thing, but it also means that I&#8217;ve to reread and review the last few pages everytime I try to pick up where I left off. This does not really help when I&#8217;m trying to understand concepts in depth. So what&#8217;s the solution? I&#8217;ll try to do a bit atleast 3-4 days a week from now on.</p>
<p>UNP is an amazingly detailed book &#8211; and as one of my colleagues said &#8211; &#8220;If you read that book properly, Stevens makes sure that there&#8217;s nothing left for you to know on the topic&#8221;. I agree.</p>
<p>Stevens wrote his own wrapper functions over common socket functions and used them in all the code examples in the book. The wrappers handle all error codes and portability issues (like IPv4/IPv6). These are included in a header unp.h (available in the back of the book as well as online on <a href="http://www.unpbook.com/src.html">http://www.unpbook.com/src.html</a>).</p>
<p>Some reviewers of the book gripe about this and say that this is an obstacle to learning the actual functions. But I think that there was no other way to do it without littering every example snippet with code for portability and error handling. The wrapper strategy makes it easier to follow the examples, and at the same time &#8211; as I found out &#8211; it makes you write those wrappers yourself. True, you can just include the unp.h header as you try the examples, but then you&#8217;ll never know what those functions are doing. I&#8217;ve found that creating my own header and writing the functions as I come across them, after looking at the book&#8217;s source code, works great. Most of them will end up identical to those in unp.h.</p>
<p>I&#8217;m pushing the examples I&#8217;m trying out into <a href="https://github.com/talonx/C-Scribbles/tree/master/net">github</a> &#8211; it&#8217;s a scratchpad so not everything might compile.</p>
<p>I&#8217;ve added a generic <a href="https://github.com/talonx/C-Scribbles/blob/master/net/mynet.h">startserver function</a> to my header &#8211; this takes a pointer to a function as an argument. The generic function starts a server socket (bind/listen/accept), forks a child when a client connects and calls the function that was earlier passed as an argument, abstracting out the actual serving part. The function pointer syntax was not hard to figure out &#8211; I&#8217;d read <a href="http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp/0131774298">Peter van der Linden</a>&#8216;s algo on unscrambling declarations in C last week. Interesting how things add up!</p>
<script type="text/javascript" src="http://cdn.socialtwist.com/2008071112-2/script.js"></script><a class="st-taf" href="http://tellafriend.socialtwist.com" onclick="return false;" style="border:0;padding:0;margin:0;"><img alt="SocialTwist Tell-a-Friend" style="border:0;padding:0;margin:0;" src="http://images.socialtwist.com/2008071112-2/button.png"onmouseout="STTAFFUNC.hideHoverMap(this)" onmouseover="STTAFFUNC.showHoverMap(this, '2008071112-2', 'http%3A%2F%2Fcode.deepinspace.net%2F2011%2F01%2F09%2Fworking-through-unp%2F', 'Working+through+UNP')" onclick="STTAFFUNC.cw(this, {id:'2008071112-2', link: 'http%3A%2F%2Fcode.deepinspace.net%2F2011%2F01%2F09%2Fworking-through-unp%2F', title: 'Working+through+UNP' });"/></a>]]></content:encoded>
			<wfw:commentRss>http://code.deepinspace.net/2011/01/09/working-through-unp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Instance Initializers in Java</title>
		<link>http://code.deepinspace.net/2010/05/22/instance-initializers-in-java/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=instance-initializers-in-java</link>
		<comments>http://code.deepinspace.net/2010/05/22/instance-initializers-in-java/#comments</comments>
		<pubDate>Sat, 22 May 2010 16:27:11 +0000</pubDate>
		<dc:creator>hrish</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://code.deepinspace.net/?p=101</guid>
		<description><![CDATA[Take a look at this simple code Code Snippet 1 What do you think the output is? It&#8217;s this - In the beginning was the command line Created an instance The &#8216;hanging&#8217; braces at the start of the class definition are instance initializers. Most of us are more familiar with static initializers - Code Snippet [...]]]></description>
			<content:encoded><![CDATA[<p>Take a look at this simple code</p>
<p><span style="color: #0000ff;">Code Snippet 1</span></p>
<pre class="brush: java; title: ; notranslate">
public class Init {
    {
        System.out.println(&quot;In the beginning was the command line&quot;);
    }

    public Init()
    {
        System.out.println(&amp;quot;Created an instance&amp;quot;);
    }

    public static void main(String[] args)
    {
        Init init = new Init();
    }
}
</pre>
<p>What do you think the output is? It&#8217;s this -</p>
<pre>    In the beginning was the command line
    Created an instance
</pre>
<p>The &#8216;hanging&#8217; braces at the start of the class definition are instance initializers. Most of us are more familiar with static initializers -</p>
<p><span style="color: #0000ff;">Code Snippet 2</span></p>
<pre class="brush: java; title: ; notranslate">
static
{
    //Do stuff
}
</pre>
<p>Instance initializers (II) are not seen often in everyday Java code &#8211; so they might seem odd at first. They are executed every time an instance of that class is created, before the statements in the constructor are executed. (See <em>The Java Language Specification 3 </em>section 8.6).</p>
<p>One use of IIs can be to execute something whenever an instance is created, and the class has multiple constructors, without calling it in every single constructor.<br />
Another one which has become popular is populating collections during declaration, in the style of Ruby or Python single-line initializers -</p>
<p><span style="color: #0000ff;">Code Snippet 3</span></p>
<pre class="brush: java; title: ; notranslate">
private Set&lt;String&gt; names = new HashSet&lt;String&gt;() {
    {
        add(&quot;Rigel&quot;);
        add(&quot;Vega&quot;);
        add(&quot;Antares&quot;);
    }
};
</pre>
<p>This idiom was how I encountered IIs first while reading somebody&#8217;s blog.<br />
What is actually happening here?</p>
<ol>
<li>An anonymous inner class is created.</li>
<li>An instance initializer block is added to the anon inner class.</li>
<li>Objects are added to the instance of that class when the names variable is initialized.</li>
</ol>
<p>Now take this scenario<br />
<span style="color: #0000ff;">Code Snippet 4</span></p>
<pre class="brush: java; title: ; notranslate">
public class WrongUsage {

    private Set&lt;String&gt; names;

    {
        add(&quot;pleiades&quot;);
    }

    public void WrongUsage()
    {
        names = new HashSet&lt;String&gt;();
    }

    public void add(String name)
    {
        names.add(name);
    }
}
</pre>
<p>Based on what we have seen above, the names set is used before it&#8217;s initialized. So this throws a NullPointerException.<br />
Let&#8217;s take another case &#8211; similar to the above but involving inheritance.</p>
<p><span style="color: #0000ff;">Code Snippet 5</span></p>
<pre class="brush: java; title: ; notranslate">
public class MyHashSet extends HashSet {
    {
        add(&quot;pleiades&quot;);
        System.out.println(&quot;Added&quot;);
    }

    public MyHashSet()
    {
        super();
        System.out.println(&quot;After calling super&quot;);
    }

    public static void main(String[] args)
    {
        Set set = new MyHashSet();
    }
}
</pre>
<p>This runs, with the output being</p>
<pre>    Added
    After calling super
</pre>
<p>In this case, add() internally uses the inner HashMap inside HashSet which is initialized in the HashSet constructor. This implies that the instance initializer is invoked before the class constructor, but after the superclass constructor (The super call is redundant here. It will be called anyway).</p>
<p>So the sequence is</p>
<ol>
<li> Superclass initialization (this includes superclass instance initializers and constructor)</li>
<li> Current class&#8217;s Instance initializers</li>
<li> Current class&#8217;s Constructor</li>
</ol>
<p>This is why the code in Code Snippet 3 does not throw an NPE &#8211; because it&#8217;s a case of inheritance (the anon inner class is a subclass of HashSet)</p>
<script type="text/javascript" src="http://cdn.socialtwist.com/2008071112-2/script.js"></script><a class="st-taf" href="http://tellafriend.socialtwist.com" onclick="return false;" style="border:0;padding:0;margin:0;"><img alt="SocialTwist Tell-a-Friend" style="border:0;padding:0;margin:0;" src="http://images.socialtwist.com/2008071112-2/button.png"onmouseout="STTAFFUNC.hideHoverMap(this)" onmouseover="STTAFFUNC.showHoverMap(this, '2008071112-2', 'http%3A%2F%2Fcode.deepinspace.net%2F2010%2F05%2F22%2Finstance-initializers-in-java%2F', 'Instance+Initializers+in+Java')" onclick="STTAFFUNC.cw(this, {id:'2008071112-2', link: 'http%3A%2F%2Fcode.deepinspace.net%2F2010%2F05%2F22%2Finstance-initializers-in-java%2F', title: 'Instance+Initializers+in+Java' });"/></a>]]></content:encoded>
			<wfw:commentRss>http://code.deepinspace.net/2010/05/22/instance-initializers-in-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A Ruby script to search bookstores online</title>
		<link>http://code.deepinspace.net/2009/12/26/a-ruby-script-to-search-bookstores-online/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=a-ruby-script-to-search-bookstores-online</link>
		<comments>http://code.deepinspace.net/2009/12/26/a-ruby-script-to-search-bookstores-online/#comments</comments>
		<pubDate>Sat, 26 Dec 2009 12:11:09 +0000</pubDate>
		<dc:creator>hrish</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[projects]]></category>

		<guid isPermaLink="false">http://code.deepinspace.net/?p=72</guid>
		<description><![CDATA[I started dabbling in Ruby some weeks back. The initial interest was sparked after reading &#8220;Treating Code as an Essay&#8221; (Yukihiro Matsumoto) &#8211; one of the chapters in Beautiful Code. So I started doing these bootstrapping exercises in Ruby. Some of the exercises are good &#8211; but nothing beats doing a small project to learn [...]]]></description>
			<content:encoded><![CDATA[<p>I started dabbling in Ruby some weeks back. The initial interest was sparked after reading &#8220;Treating Code as an Essay&#8221; (Yukihiro Matsumoto) &#8211; one of the chapters in <a title="Beautiful Code" href="http://www.amazon.com/Beautiful-Code-Leading-Programmers-Practice/dp/0596510047" target="_blank">Beautiful Code</a>. So I started doing these <a id="tgx1" title="www.knowing.net" href="http://www.knowing.net/index.php/2006/06/16/15-exercises-to-know-a-programming-language-part-1/" target="_blank">bootstrapping</a> exercises in Ruby. Some of the exercises are good &#8211; but nothing beats doing a small project to learn a new language.</p>
<p>I buy a lot of books, mostly online. There are a few good online bookstores in India, notably Flipkart.com, Infibeam.com and Indiaplaza.in (Sadly, Amazon does not have full-fledged shipping to India yet). The way I usually search for a book in online bookstores is (was, till now)</p>
<ol>
<li>Go to books.google.com and enter the book title</li>
<li>Click on the best match</li>
<li>Click on &#8216;All Sellers&#8217; on the left of the page</li>
<li>The Indian bookstores are usually listed towards the bottom. It does not include all stores, and sometimes the prices are not listed. I have to go to each individual site and check them out.</li>
</ol>
<p>I wanted to collapse these steps into one &#8211; a simple script that would accept the name of the book and show results from all these bookstores, with comparative pricing. And the result was this</p>
<p><a id="cb6." title="http://github.com/talonx/book-search" href="http://github.com/talonx/book-search" target="_blank">http://github.com/talonx/book-search</a></p>
<p>It&#8217;s in Ruby, runs from the command line and writes the output to an HTML in the same directory called &#8216;search.html&#8217;. Much needs to be done, like</p>
<ul>
<li>Price based listing with the lowest on top</li>
<li>A web interface for the search</li>
<li>Add more bookstores &#8211; it&#8217;s only Flipkart.com, Infibeam.com, Indiaplaza and Bookadda.com right now.</li>
</ul>
<p>To run the script, type this (you need Ruby 1.8.x, available from <a title="http://www.ruby-lang.org/en/downloads/" href="http://www.ruby-lang.org/en/downloads/" target="_blank">http://www.ruby-lang.org/en/downloads/</a> and the Hpricot HTML parser library, available from <a title="http://github.com/whymirror/hpricot" href="http://github.com/whymirror/hpricot" target="_blank">http://github.com/whymirror/hpricot</a>)</p>
<blockquote><p>ruby lib\book-search.rb &#8220;&lt;book title (in quotes if it has spaces)&gt;&#8221;</p></blockquote>
<script type="text/javascript" src="http://cdn.socialtwist.com/2008071112-2/script.js"></script><a class="st-taf" href="http://tellafriend.socialtwist.com" onclick="return false;" style="border:0;padding:0;margin:0;"><img alt="SocialTwist Tell-a-Friend" style="border:0;padding:0;margin:0;" src="http://images.socialtwist.com/2008071112-2/button.png"onmouseout="STTAFFUNC.hideHoverMap(this)" onmouseover="STTAFFUNC.showHoverMap(this, '2008071112-2', 'http%3A%2F%2Fcode.deepinspace.net%2F2009%2F12%2F26%2Fa-ruby-script-to-search-bookstores-online%2F', 'A+Ruby+script+to+search+bookstores+online')" onclick="STTAFFUNC.cw(this, {id:'2008071112-2', link: 'http%3A%2F%2Fcode.deepinspace.net%2F2009%2F12%2F26%2Fa-ruby-script-to-search-bookstores-online%2F', title: 'A+Ruby+script+to+search+bookstores+online' });"/></a>]]></content:encoded>
			<wfw:commentRss>http://code.deepinspace.net/2009/12/26/a-ruby-script-to-search-bookstores-online/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

