<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/rss2full.xsl" type="text/xsl" media="screen"?><?xml-stylesheet href="http://feeds.feedburner.com/~d/styles/itemcontent.css" type="text/css" media="screen"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Prashanth Ellina</title>
	
	<link>http://blog.prashanthellina.com</link>
	<description>( to ) ? be : ! be;</description>
	<pubDate>Tue, 04 Nov 2008 05:31:10 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/prashanthellina" type="application/rss+xml" /><item>
		<title>The Deep Slumber</title>
		<link>http://feeds.feedburner.com/~r/prashanthellina/~3/441746405/</link>
		<comments>http://blog.prashanthellina.com/2008/11/04/the-deep-slumber/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 05:31:10 +0000</pubDate>
		<dc:creator>prashanthellina</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.prashanthellina.com/2008/11/04/the-deep-slumber/</guid>
		<description><![CDATA[Snore &#8230;. Snore &#8230;. Zzzz &#8230;.

Knock &#8230; Knock &#8230;

Bleh! Bleeeeh!

I&#8217;m awake folks. Will resume posting soon  
]]></description>
			<content:encoded><![CDATA[<p>Snore &#8230;. Snore &#8230;. Zzzz &#8230;.</p>
<p>
Knock &#8230; Knock &#8230;</p>
<p>
Bleh! Bleeeeh!</p>
<p>
I&#8217;m awake folks. Will resume posting soon <img src='http://blog.prashanthellina.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<p><a href="http://feeds.feedburner.com/~a/prashanthellina?a=BBjVpq"><img src="http://feeds.feedburner.com/~a/prashanthellina?i=BBjVpq" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/prashanthellina?a=KzJmN"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=KzJmN" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=fCaRn"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=fCaRn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=LFUjn"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=LFUjn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=4UKFn"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=4UKFn" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=NeNFN"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=NeNFN" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/prashanthellina/~4/441746405" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.prashanthellina.com/2008/11/04/the-deep-slumber/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.prashanthellina.com/2008/11/04/the-deep-slumber/</feedburner:origLink></item>
		<item>
		<title>Blockworld’s Periodic Table</title>
		<link>http://feeds.feedburner.com/~r/prashanthellina/~3/400924168/</link>
		<comments>http://blog.prashanthellina.com/2008/09/23/blockworlds-periodic-table/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 16:11:44 +0000</pubDate>
		<dc:creator>prashanthellina</dc:creator>
		
		<category><![CDATA[python]]></category>

		<category><![CDATA[block]]></category>

		<category><![CDATA[compound]]></category>

		<category><![CDATA[game]]></category>

		<category><![CDATA[material]]></category>

		<category><![CDATA[periodic table]]></category>

		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=72</guid>
		<description><![CDATA[Related PostRahul and I are working on a game. As a part of the game, material construction from fundamental building blocks needs to be implemented. That is somewhat like building &#8220;materials&#8221; using LEGO blocks. The requirement is to have the fundamental building blocks combine in non-trivial and interesting ways.
I did not intend to talk much about [...]]]></description>
			<content:encoded><![CDATA[Related Post<p><a href="http://blog.arahul.in/" onclick="javascript:urchinTracker ('/outbound/article/blog.arahul.in');">Rahul</a> and I are working on a game. As a part of the game, material construction from fundamental building blocks needs to be implemented. That is somewhat like building &#8220;materials&#8221; using LEGO blocks. The requirement is to have the fundamental building blocks combine in non-trivial and interesting ways.</p>
<p>I did not intend to talk much about the game but I could keep it to myself after taking a look at my very own &#8220;Periodic Table&#8221;!.</p>
<p>Click on the image to see a detailed statistics page.</p>
<p><a href="http://www.prashanthellina.com/docs/blockworld/ptable_sep08.html"><br />
<img src="http://www.prashanthellina.com/images/blockworld_attenuation.jpg" alt="blockworld attenuation periodic table"/></a></p>
<h3></h3><ul class="related_post"><li><a href="http://blog.prashanthellina.com/2008/07/27/determining-the-difficulty-of-arithmetic-operations/" title="Determining the difficulty of Arithmetic Operations">Determining the difficulty of Arithmetic Operations</a></li><li><a href="http://blog.prashanthellina.com/2008/07/11/even-a-python-can-be-abused/" title="Even a python can be abused">Even a python can be abused</a></li><li><a href="http://blog.prashanthellina.com/2008/05/22/nose-tdd-python/" title="Nose - TDD - Python">Nose - TDD - Python</a></li><li><a href="http://blog.prashanthellina.com/2008/05/04/n-gram-data-from-project-gutenberg/" title="N-gram data from Project Gutenberg">N-gram data from Project Gutenberg</a></li><li><a href="http://blog.prashanthellina.com/2008/04/22/alexa-rank-a-script-to-get-the-rank-for-any-site/" title="Alexa rank: A script to get the rank for any site">Alexa rank: A script to get the rank for any site</a></li><li><a href="http://blog.prashanthellina.com/2008/02/06/all-ncert-text-books/" title="All NCERT text books!">All NCERT text books!</a></li><li><a href="http://blog.prashanthellina.com/2008/01/07/interfacing-python-with-c-using-ctypes/" title="Interfacing Python with C using ctypes">Interfacing Python with C using ctypes</a></li><li><a href="http://blog.prashanthellina.com/2007/12/21/topic-extraction-using-wikipedia-data/" title="Topic extraction using Wikipedia data">Topic extraction using Wikipedia data</a></li><li><a href="http://blog.prashanthellina.com/2007/11/14/generating-call-graphs-for-understanding-and-refactoring-python-code/" title="Generating call graphs for understanding and refactoring python code">Generating call graphs for understanding and refactoring python code</a></li><li><a href="http://blog.prashanthellina.com/2007/10/17/ways-to-process-and-use-wikipedia-dumps/" title="Ways to process and use Wikipedia dumps">Ways to process and use Wikipedia dumps</a></li><li><a href="http://blog.prashanthellina.com/2007/08/26/word-arithmetic-puzzle-generator/" title="Word arithmetic puzzle generator">Word arithmetic puzzle generator</a></li><li><a href="http://blog.prashanthellina.com/2007/08/24/drawing-on-your-desktop/" title="Drawing on your Desktop">Drawing on your Desktop</a></li><li><a href="http://blog.prashanthellina.com/2007/08/22/matrix-desktop/" title="Matrix Desktop">Matrix Desktop</a></li></ul>
<p><a href="http://feeds.feedburner.com/~a/prashanthellina?a=7FBheB"><img src="http://feeds.feedburner.com/~a/prashanthellina?i=7FBheB" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/prashanthellina?a=P9JpL"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=P9JpL" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=S9agl"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=S9agl" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=Dm5ml"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=Dm5ml" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=pLwrl"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=pLwrl" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=4idBL"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=4idBL" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/prashanthellina/~4/400924168" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.prashanthellina.com/2008/09/23/blockworlds-periodic-table/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.prashanthellina.com/2008/09/23/blockworlds-periodic-table/</feedburner:origLink></item>
		<item>
		<title>Determining the difficulty of Arithmetic Operations</title>
		<link>http://feeds.feedburner.com/~r/prashanthellina/~3/347565811/</link>
		<comments>http://blog.prashanthellina.com/2008/07/27/determining-the-difficulty-of-arithmetic-operations/#comments</comments>
		<pubDate>Sun, 27 Jul 2008 16:51:22 +0000</pubDate>
		<dc:creator>prashanthellina</dc:creator>
		
		<category><![CDATA[math]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[python]]></category>

		<category><![CDATA[arithmetic]]></category>

		<category><![CDATA[arithmetic algorithms]]></category>

		<category><![CDATA[borrow]]></category>

		<category><![CDATA[carry]]></category>

		<category><![CDATA[long division]]></category>

		<category><![CDATA[problem difficulty]]></category>

		<category><![CDATA[problem generator]]></category>

		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=69</guid>
		<description><![CDATA[Related PostI am trying to write a program to test my arithmetic skills. The program should pose arithmetic problems involving the four basic operations - addition, subtraction, multiplication and division. When the testing session starts, the program should issue problems of less difficulty and the difficulty should be ramped up gradually. A score should be computed [...]]]></description>
			<content:encoded><![CDATA[Related Post<p><img src="http://www.prashanthellina.com/images/kid_math_problem.gif" align="left" alt="kid math problem" padding="5"/>I am trying to write a program to test my arithmetic skills. The program should pose arithmetic problems involving the four basic operations - addition, subtraction, multiplication and division. When the testing session starts, the program should issue problems of less difficulty and the difficulty should be ramped up gradually. A score should be computed based on number of questions and the difficulty of questions. The hope is that if I keep using this program for a little time every day, I&#8217;ll be able to improve my abysmal arithmetic performance <img src='http://blog.prashanthellina.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Note that you will need to have <a href="http://www.python.org" onclick="javascript:urchinTracker ('/outbound/article/www.python.org');">Python</a> installed to try out the script and the examples in this article. I believe that the code is simple enough to be understood with minimal or no understanding of the Python language. If any part of the article is not clear, feel free to point it out to me. Thank you.</p>
<h2>Estimating difficulty</h2>
<p>Let us take a look at the following problems.</p>
<p><strong>Problem 1</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff4500;">5</span> +
<span style="color: #ff4500;">3</span></pre></div></div>

<p><br/></p>
<p><strong>Problem 2</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff4500;">99999</span> +
<span style="color: #ff4500;">12345</span></pre></div></div>

<p><br/></p>
<p>Now, which problem is more difficult? It is quite obvious that Problem 2 is more difficult. But why? One answer would be - &#8220;because Problem 2 involves the addition of two large numbers&#8221;. Fine. Consider this now &#8230;</p>
<p><strong>Problem 3</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff4500;">1000000</span> +
<span style="color: #ff4500;">1000001</span></pre></div></div>

<p><br/></p>
<p>Is Problem 3 more difficult than Problem 2 because the numbers undergoing addition are bigger? The intuitive answer is &#8220;No&#8221;. But why? Because the time it takes to compute the result is lesser. Why? Because the number of operations performed during the course of solving Problem 3 are lesser than those of Problem 2? No.</p>
<p>Problem 3 is easier than Problem 2 because the nature of the operations performed in Problem 3. The operations there are simpler.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff4500;">0</span> + <span style="color: #ff4500;">1</span>,
<span style="color: #ff4500;">0</span> + <span style="color: #ff4500;">0</span>,
<span style="color: #ff4500;">1</span> + <span style="color: #ff4500;">1</span></pre></div></div>

<p><br/></p>
<p>are arguably simpler operations than</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff4500;">9</span> + <span style="color: #ff4500;">5</span>,
<span style="color: #ff4500;">9</span> + <span style="color: #ff4500;">2</span>,
<span style="color: #ff4500;">4</span> + <span style="color: #ff4500;">9</span></pre></div></div>

<p><br/></p>
<p>I&#8217;ve attempted to capture the basic operations we perform while doing arithmetic and assign a level of difficulty to each. I&#8217;ve then emulated the algorithms we follow to compute solutions to such problems. By doing the following, it becomes possible to estimate the &#8220;difficulty&#8221; of an arithmetic problem (involving the basic operations).</p>
<p><center><img src="http://www.prashanthellina.com/images/math_cartoon_yesterday_x.jpg" alt="math cartoon yesterday x"/></center></p>
<h2>Operation difficulties</h2>
<p><br/></p>
<h3>Addition</h3>

<div class="wp_syntax"><div class="code"><pre class="python">addition_difficulties = <span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'digit_zero'</span> : <span style="color: #ff4500;">1</span>,   <span style="color: #808080; font-style: italic;"># any digit added to zero</span>
    <span style="color: #483d8b;">'even_even'</span>  : <span style="color: #ff4500;">2</span>,   <span style="color: #808080; font-style: italic;"># sum of even digits</span>
    <span style="color: #483d8b;">'odd_odd'</span>    : <span style="color: #ff4500;">2</span>,   <span style="color: #808080; font-style: italic;"># sum of odd digits </span>
    <span style="color: #483d8b;">'even_odd'</span>   : <span style="color: #ff4500;">3</span>,   <span style="color: #808080; font-style: italic;"># sum of even and odd digits</span>
    <span style="color: #483d8b;">'carry'</span>      : <span style="color: #ff4500;">2</span>    <span style="color: #808080; font-style: italic;"># difficulty of carry (for remembering and then adding)</span>
<span style="color: black;">&#125;</span></pre></div></div>

<p><br/></p>
<p>Here is the function which computes the addition difficulty. It is a recursive function that can compute the addition difficulty for n numbers. It computes the sum and difficulty of the first two numbers and then inserts the sum at the head of the list of n numbers in place of the two numbers just summed. It then calls itself with the modified list.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> compute_addition_difficulty<span style="color: black;">&#40;</span>*numbers<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    Generates a difficulty number and sum for given
    list of numbers for the addition operation
    *args - integers
    returns: (sum, difficulty)
&nbsp;
    &gt;&gt;&gt; compute_addition_difficulty(0, 0)
    (0, 1)
&nbsp;
    &gt;&gt;&gt; compute_addition_difficulty(0, 1)
    (1, 1)
&nbsp;
    &gt;&gt;&gt; compute_addition_difficulty(1, 1)
    (2, 2)
&nbsp;
    &gt;&gt;&gt; compute_addition_difficulty(1, 2)
    (3, 3)
&nbsp;
    &gt;&gt;&gt; compute_addition_difficulty(2, 2)
    (4, 2)
&nbsp;
    &gt;&gt;&gt; compute_addition_difficulty(19, 9)
    (28, 5)
&nbsp;
    &gt;&gt;&gt; compute_addition_difficulty(99999, 12345)
    (112344, 22)
    '</span><span style="color: #483d8b;">''</span>
&nbsp;
    numbers = <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>
&nbsp;
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span>: <span style="color: #ff7700;font-weight:bold;">return</span> numbers<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, <span style="color: #ff4500;">0</span>
&nbsp;
    num1, num2 = numbers<span style="color: black;">&#91;</span>:<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
    num1 = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>num1<span style="color: black;">&#41;</span>
    num2 = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>num2<span style="color: black;">&#41;</span>
&nbsp;
    max_length = <span style="color: #008000;">max</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>num1<span style="color: black;">&#41;</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>num2<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    num1 = num1.<span style="color: black;">rjust</span><span style="color: black;">&#40;</span>max_length, <span style="color: #483d8b;">'0'</span><span style="color: black;">&#41;</span>
    num2 = num2.<span style="color: black;">rjust</span><span style="color: black;">&#40;</span>max_length, <span style="color: #483d8b;">'0'</span><span style="color: black;">&#41;</span>
&nbsp;
    difficulty = <span style="color: #ff4500;">0</span>
    carry = <span style="color: #ff4500;">0</span>
    r = <span style="color: #008000;">reversed</span>
    ad = addition_difficulties
    <span style="color: #008000;">sum</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> index, <span style="color: black;">&#40;</span>d1, d2<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span> izip<span style="color: black;">&#40;</span> r<span style="color: black;">&#40;</span>num1<span style="color: black;">&#41;</span>, r<span style="color: black;">&#40;</span>num2<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>:
        d1 = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>d1<span style="color: black;">&#41;</span>
        d2 = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>d2<span style="color: black;">&#41;</span>
&nbsp;
        d1_is_even = is_even<span style="color: black;">&#40;</span>d1<span style="color: black;">&#41;</span>
        d2_is_even = is_even<span style="color: black;">&#40;</span>d2<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> d1 <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff7700;font-weight:bold;">not</span> d2: difficulty += ad<span style="color: black;">&#91;</span><span style="color: #483d8b;">'digit_zero'</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> d1_is_even <span style="color: #ff7700;font-weight:bold;">and</span> d2_is_even: difficulty += ad<span style="color: black;">&#91;</span><span style="color: #483d8b;">'even_even'</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #ff7700;font-weight:bold;">not</span> d1_is_even <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> d2_is_even: difficulty += ad<span style="color: black;">&#91;</span><span style="color: #483d8b;">'odd_odd'</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> d1_is_even != d2_is_even: difficulty += ad<span style="color: black;">&#91;</span><span style="color: #483d8b;">'even_odd'</span><span style="color: black;">&#93;</span>
&nbsp;
        dsum = d1 + d2 + carry
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> dsum &gt; <span style="color: #ff4500;">9</span>:
            carry = <span style="color: #ff4500;">1</span>
            difficulty += ad<span style="color: black;">&#91;</span><span style="color: #483d8b;">'carry'</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            carry = <span style="color: #ff4500;">0</span>
&nbsp;
        <span style="color: #008000;">sum</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>dsum % <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> carry:
        <span style="color: #008000;">sum</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>carry<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #008000;">sum</span>.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">sum</span> = <span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">sum</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">sum</span> = <span style="color: #008000;">int</span> <span style="color: black;">&#40;</span><span style="color: #008000;">sum</span><span style="color: black;">&#41;</span>
&nbsp;
    numbers = <span style="color: black;">&#91;</span><span style="color: #008000;">sum</span><span style="color: black;">&#93;</span> + numbers<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #008000;">sum</span>, sub_difficulty = compute_addition_difficulty<span style="color: black;">&#40;</span>*numbers<span style="color: black;">&#41;</span>
&nbsp;
    difficulty += sub_difficulty
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">sum</span>, difficulty</pre></div></div>

<p><br/></p>
<h3>Subtraction</h3>

<div class="wp_syntax"><div class="code"><pre class="python">subtraction_difficulties = <span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'digit_zero'</span> : <span style="color: #ff4500;">1</span>,    <span style="color: #808080; font-style: italic;"># difference of zero and any digit</span>
    <span style="color: #483d8b;">'same_digits'</span>: <span style="color: #ff4500;">1</span>,    <span style="color: #808080; font-style: italic;"># difference of same values digits</span>
    <span style="color: #483d8b;">'even_even'</span>  : <span style="color: #ff4500;">2</span>,    <span style="color: #808080; font-style: italic;"># difference of even digits</span>
    <span style="color: #483d8b;">'odd_odd'</span>    : <span style="color: #ff4500;">2</span>,    <span style="color: #808080; font-style: italic;"># difference of odd digits</span>
    <span style="color: #483d8b;">'even_odd'</span>   : <span style="color: #ff4500;">3</span>,    <span style="color: #808080; font-style: italic;"># difference of even and odd digits</span>
    <span style="color: #483d8b;">'borrow'</span>     : <span style="color: #ff4500;">2</span>,    <span style="color: #808080; font-style: italic;"># doing a borrow</span>
    <span style="color: #483d8b;">'twodigit_digit'</span> : <span style="color: #ff4500;">4</span> <span style="color: #808080; font-style: italic;"># difference of two-digit number and digit</span>
<span style="color: black;">&#125;</span></pre></div></div>

<p><br/></p>
<p>The subtraction function is also recursive and operates similar to addition in that respect. Before looking at the subtraction algorithm, take a look at how &#8220;borrowing&#8221; is implemented. Let us say we are trying to perform the following,</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff4500;">200005</span> -
     <span style="color: #ff4500;">6</span></pre></div></div>

<p><br/></p>
<p>After borrowing the number would be, 19999(1)5. We would then subtract 6 from 15 and proceed. The do_borrow() function captures this aspect of substraction.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> do_borrow<span style="color: black;">&#40;</span>num, index<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> index == <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span><span style="color: #ff4500;">-1</span>: <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'cannot perform borrow'</span><span style="color: black;">&#41;</span>
&nbsp;
    num_borrows = <span style="color: #ff4500;">1</span>
    next_digit = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#91;</span>index<span style="color: #ff4500;">+1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> next_digit &gt; <span style="color: #ff4500;">0</span>:
        num<span style="color: black;">&#91;</span>index<span style="color: #ff4500;">+1</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>next_digit<span style="color: #ff4500;">-1</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        num<span style="color: black;">&#91;</span>index<span style="color: #ff4500;">+1</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">9</span><span style="color: black;">&#41;</span>
        num_borrows += do_borrow<span style="color: black;">&#40;</span>num, index<span style="color: #ff4500;">+1</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> num_borrows</pre></div></div>

<p><br/></p>
<p>This implementation is a bit confusing so check out this example (do_borrow() applied to the problem mentioned above).</p>

<div class="wp_syntax"><div class="code"><pre class="python">&gt;&gt;&gt; x = <span style="color: #008000;">list</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'200005'</span><span style="color: black;">&#41;</span>
&gt;&gt;&gt; x.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&gt;&gt;&gt; x
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'5'</span>, <span style="color: #483d8b;">'0'</span>, <span style="color: #483d8b;">'0'</span>, <span style="color: #483d8b;">'0'</span>, <span style="color: #483d8b;">'0'</span>, <span style="color: #483d8b;">'2'</span><span style="color: black;">&#93;</span>
&gt;&gt;&gt; do_borrow<span style="color: black;">&#40;</span>x, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">4</span>
&gt;&gt;&gt; <span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
<span style="color: #483d8b;">'509991'</span>
&gt;&gt;&gt; x.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&gt;&gt;&gt; x
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'1'</span>, <span style="color: #483d8b;">'9'</span>, <span style="color: #483d8b;">'9'</span>, <span style="color: #483d8b;">'9'</span>, <span style="color: #483d8b;">'0'</span>, <span style="color: #483d8b;">'5'</span><span style="color: black;">&#93;</span></pre></div></div>

<p><br/></p>
<p>Note that do_borrow returned 4, which indicates that the difficulty of performing this borrow operation is 4. Also note that do_borrow takes a list of digits in reversed order. It takes a list because the result is provided in-place (remember that Python strings are immutable). Now that you have seen how the borrowing works, all introduce the subtraction function.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> compute_subtraction_difficulty<span style="color: black;">&#40;</span>*numbers<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    Generates a difficulty number and result for given
    list of numbers for the subtraction operation
    *args (tuple) - integers
    returns: (difference, difficulty)
&nbsp;
    &gt;&gt;&gt; compute_subtraction_difficulty(0,0)
    (0, 1)
&nbsp;
    &gt;&gt;&gt; compute_subtraction_difficulty(1,0)
    (1, 1)
&nbsp;
    &gt;&gt;&gt; compute_subtraction_difficulty(1,1)
    (0, 1)
&nbsp;
    &gt;&gt;&gt; compute_subtraction_difficulty(2,1)
    (1, 3)
&nbsp;
    &gt;&gt;&gt; compute_subtraction_difficulty(4,2)
    (2, 2)
&nbsp;
    &gt;&gt;&gt; compute_subtraction_difficulty(4,3)
    (1, 3)
&nbsp;
    &gt;&gt;&gt; compute_subtraction_difficulty(100,1)
    (99, 10)
&nbsp;
    &gt;&gt;&gt; compute_subtraction_difficulty(5000007,9)
    (4999998, 22)
    '</span><span style="color: #483d8b;">''</span>
&nbsp;
    numbers = <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span>: <span style="color: #ff7700;font-weight:bold;">return</span> numbers<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, <span style="color: #ff4500;">0</span>
&nbsp;
    num1, num2 = numbers<span style="color: black;">&#91;</span>:<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> num1 &lt; num2: num1, num2 = num2, num1
&nbsp;
    num1 = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>num1<span style="color: black;">&#41;</span>
    num2 = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>num2<span style="color: black;">&#41;</span>
&nbsp;
    max_length = <span style="color: #008000;">max</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>num1<span style="color: black;">&#41;</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>num2<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    num1 = <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>num1.<span style="color: black;">rjust</span><span style="color: black;">&#40;</span>max_length, <span style="color: #483d8b;">'0'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    num2 = <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>num2.<span style="color: black;">rjust</span><span style="color: black;">&#40;</span>max_length, <span style="color: #483d8b;">'0'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    difficulty = <span style="color: #ff4500;">0</span>
    borrow = <span style="color: #ff4500;">0</span>
    sd = subtraction_difficulties
    difference = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    num1.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    num2.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> index, <span style="color: black;">&#40;</span>d1, d2<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span> izip<span style="color: black;">&#40;</span> num1, num2 <span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>:
        d1 = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>d1<span style="color: black;">&#41;</span>
        d2 = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>d2<span style="color: black;">&#41;</span>
&nbsp;
        d1_is_even = is_even<span style="color: black;">&#40;</span>d1<span style="color: black;">&#41;</span>
        d2_is_even = is_even<span style="color: black;">&#40;</span>d2<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> d1 &gt; d2:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> d1 <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff7700;font-weight:bold;">not</span> d2: difficulty += sd<span style="color: black;">&#91;</span><span style="color: #483d8b;">'digit_zero'</span><span style="color: black;">&#93;</span>
            <span style="color: #ff7700;font-weight:bold;">elif</span> d1_is_even <span style="color: #ff7700;font-weight:bold;">and</span> d2_is_even: difficulty += sd<span style="color: black;">&#91;</span><span style="color: #483d8b;">'even_even'</span><span style="color: black;">&#93;</span>
            <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #ff7700;font-weight:bold;">not</span> d1_is_even <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> d1_is_even: difficulty += sd<span style="color: black;">&#91;</span><span style="color: #483d8b;">'odd_odd'</span><span style="color: black;">&#93;</span>
            <span style="color: #ff7700;font-weight:bold;">elif</span> d1_is_even != d2_is_even: difficulty += sd<span style="color: black;">&#91;</span><span style="color: #483d8b;">'even_odd'</span><span style="color: black;">&#93;</span>
            ddiff = d1 - d2
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">elif</span> d1 &lt; d2:
            num_borrows = do_borrow<span style="color: black;">&#40;</span>num1, index<span style="color: black;">&#41;</span>
            difficulty += sd<span style="color: black;">&#91;</span><span style="color: #483d8b;">'borrow'</span><span style="color: black;">&#93;</span>*num_borrows
            ddiff = <span style="color: #ff4500;">10</span> + d1 - d2
            difficulty += sd<span style="color: black;">&#91;</span><span style="color: #483d8b;">'twodigit_digit'</span><span style="color: black;">&#93;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">elif</span> d1 == d2:
            difficulty += sd<span style="color: black;">&#91;</span><span style="color: #483d8b;">'same_digits'</span><span style="color: black;">&#93;</span>
            ddiff = <span style="color: #ff4500;">0</span>
&nbsp;
        difference.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>ddiff<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
&nbsp;
    difference.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    difference = <span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>difference<span style="color: black;">&#41;</span>
    difference = <span style="color: #008000;">int</span> <span style="color: black;">&#40;</span>difference<span style="color: black;">&#41;</span>
&nbsp;
    numbers = <span style="color: black;">&#91;</span>difference<span style="color: black;">&#93;</span> + numbers<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span>
&nbsp;
    difference, sub_difficulty = compute_subtraction_difficulty<span style="color: black;">&#40;</span>*numbers<span style="color: black;">&#41;</span>
&nbsp;
    difficulty += sub_difficulty
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> difference, difficulty</pre></div></div>

<p><br/></p>
<h3>Multiplication</h3>

<div class="wp_syntax"><div class="code"><pre class="python">multiplication_difficulties = <span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'carry'</span>  : <span style="color: #ff4500;">1</span>,   <span style="color: #808080; font-style: italic;"># carry operation, summation difficulty is seperate</span>
    <span style="color: #483d8b;">'offset'</span> : <span style="color: #ff4500;">1</span>,
<span style="color: black;">&#125;</span></pre></div></div>

<p><br/></p>
<p>The basic operation of multiplying big numbers is one digit by one digit multiplication. I&#8217;ve tried to capture the difficulty of this operation as follows</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> get_single_digit_multiplication_difficulty<span style="color: black;">&#40;</span>d1, d2<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    Max value of multiplication of two digits is 81 (9*9).
    Difficulty of d1*d2 increases as the resulting value
    of the multiplication increases. thereby 9*9=81 is most difficult
    and 0*0 is least difficult.
&nbsp;
    We will represent this difficult with value from 1-4 (inclusive)
&nbsp;
    Note that the difficulty will be incremented by 1 for presence
    of odd digit in the operands (except for 1 and 5 because it is arguably
    easier to multiply)
&nbsp;
    &gt;&gt;&gt; get_single_digit_multiplication_difficulty(0, 1)
    (0, 1)
    &gt;&gt;&gt; get_single_digit_multiplication_difficulty(1, 1)
    (1, 1)
    &gt;&gt;&gt; get_single_digit_multiplication_difficulty(2, 5)
    (10, 1)
    &gt;&gt;&gt; get_single_digit_multiplication_difficulty(2, 7)
    (14, 2)
    &gt;&gt;&gt; get_single_digit_multiplication_difficulty(2, 6)
    (12, 1)
    &gt;&gt;&gt; get_single_digit_multiplication_difficulty(8, 9)
    (72, 5)
    &gt;&gt;&gt; get_single_digit_multiplication_difficulty(8, 8)
    (64, 4)
    '</span><span style="color: #483d8b;">''</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> d1 <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff7700;font-weight:bold;">not</span> d2: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">1</span> 
&nbsp;
    res = d1 * d2
    difficulty = <span style="color: #dc143c;">math</span>.<span style="color: black;">ceil</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span>/<span style="color: #ff4500;">81</span>.<span style="color: black;">&#41;</span> * res<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># odd numbers are harder to multiply except 1 and 5</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> d1 <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">and</span> d2 <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: black;">&#40;</span>is_odd<span style="color: black;">&#40;</span>d1<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> is_odd<span style="color: black;">&#40;</span>d2<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
        difficulty += <span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> res, <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>difficulty<span style="color: black;">&#41;</span></pre></div></div>

<p><br/></p>
<p>At the next level, the operation would be multiplying a multi-digit number by a single digit, which I call simple multiplication. Over here I take into account the difficulty of carry. I hope the following code is self explanatory.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> compute_simple_multiplication_difficulty<span style="color: black;">&#40;</span>num, digit<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    &gt;&gt;&gt; compute_simple_multiplication_difficulty(1, 0)
    (0, 1)
    &gt;&gt;&gt; compute_simple_multiplication_difficulty(1, 1)
    (1, 1)
    &gt;&gt;&gt; compute_simple_multiplication_difficulty(2, 1)
    (2, 1)
    &gt;&gt;&gt; compute_simple_multiplication_difficulty(10, 1)
    (10, 2)
    &gt;&gt;&gt; compute_simple_multiplication_difficulty(15, 1)
    (15, 2)
    &gt;&gt;&gt; compute_simple_multiplication_difficulty(15, 5)
    (75, 7)
    &gt;&gt;&gt; compute_simple_multiplication_difficulty(999, 7)
    (6993, 25)
    '</span><span style="color: #483d8b;">''</span>
    num = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span>
&nbsp;
    result = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    md = multiplication_difficulties
    carry = <span style="color: #ff4500;">0</span>
    difficulty = <span style="color: #ff4500;">0</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> index, d <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span><span style="color: #008000;">reversed</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
        d = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>
        res, s_diff = get_single_digit_multiplication_difficulty<span style="color: black;">&#40;</span>d, digit<span style="color: black;">&#41;</span>
        difficulty += s_diff
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>carry<span style="color: black;">&#41;</span>:
            res, carry_sum_difficulty = compute_addition_difficulty<span style="color: black;">&#40;</span>res, carry<span style="color: black;">&#41;</span>
            difficulty += carry_sum_difficulty + md<span style="color: black;">&#91;</span><span style="color: #483d8b;">'carry'</span><span style="color: black;">&#93;</span>
&nbsp;
        carry = res/<span style="color: #ff4500;">10</span>
        result_digit = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>res<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">-1</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        result.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>result_digit<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> carry:
        result.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>carry<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
&nbsp;
    result.<span style="color: black;">reverse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    result = <span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span>
    result = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>result<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> result, difficulty</pre></div></div>

<p><br/></p>
<p>Simple multiplication is a basic operation of complex multiplication (multi-digit multiplied by multi-digit). Let us see an example of &#8220;complex multiplication&#8221; which the multiplication code follows.</p>

<div class="wp_syntax"><div class="code"><pre class="python">    <span style="color: #ff4500;">345</span> x
    <span style="color: #ff4500;">123</span>
  -----
   <span style="color: #ff4500;">1035</span>
   690x  <span style="color: #808080; font-style: italic;"># result is &quot;offset&quot; or multiplied by 10</span>
  345xx  <span style="color: #808080; font-style: italic;"># result if &quot;offset&quot; or multiplied by 100</span>
  -----
  <span style="color: #ff4500;">42435</span>  <span style="color: #808080; font-style: italic;"># result of summation of above numbers</span>
  -----</pre></div></div>

<p><br/></p>
<p>The first part of the multiplication is to perform simple multiplications - 345&#215;3, 345&#215;2, 345&#215;1 and to offset the numbers accordingly. The second part of the operation is to sum the numbers. I used the addition algorithm to achieve the second part. Check out the multiplication function.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> compute_multiplication_difficulty<span style="color: black;">&#40;</span>*numbers<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    Generates a difficulty number and result for given
    list of numbers for the multiplication operation
    *args (tuple) - integers
    returns: (product, difficulty)
&nbsp;
    &gt;&gt;&gt; compute_multiplication_difficulty(0,0)
    (0, 1)
    &gt;&gt;&gt; compute_multiplication_difficulty(1,0)
    (0, 1)
    &gt;&gt;&gt; compute_multiplication_difficulty(1,1)
    (1, 1)
    &gt;&gt;&gt; compute_multiplication_difficulty(2,1)
    (2, 1)
    &gt;&gt;&gt; compute_multiplication_difficulty(5,1)
    (5, 1)
    &gt;&gt;&gt; compute_multiplication_difficulty(5,2)
    (10, 1)
    &gt;&gt;&gt; compute_multiplication_difficulty(17,2)
    (34, 7)
    &gt;&gt;&gt; compute_multiplication_difficulty(17,29)
    (493, 20)
    &gt;&gt;&gt; compute_multiplication_difficulty(17,29,3)
    (1479, 31)
    &gt;&gt;&gt; compute_multiplication_difficulty(1776,29,3)
    (154512, 77)
    '</span><span style="color: #483d8b;">''</span>
&nbsp;
    numbers = <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span>: <span style="color: #ff7700;font-weight:bold;">return</span> numbers<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, <span style="color: #ff4500;">0</span>
&nbsp;
    num1, num2 = numbers<span style="color: black;">&#91;</span>:<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> num1 &lt; num2: num1, num2 = num2, num1
&nbsp;
    num2 = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>num2<span style="color: black;">&#41;</span>
&nbsp;
    difficulty = <span style="color: #ff4500;">0</span>
    borrow = <span style="color: #ff4500;">0</span>
    md = multiplication_difficulties
    m_numbers = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> index, d <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span><span style="color: #008000;">reversed</span><span style="color: black;">&#40;</span>num2<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
&nbsp;
        d = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>d<span style="color: black;">&#41;</span>
&nbsp;
        m_number, m_diff = compute_simple_multiplication_difficulty<span style="color: black;">&#40;</span>num1, d<span style="color: black;">&#41;</span>
        difficulty += m_diff
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> index:
            m_number = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>m_number * <span style="color: #dc143c;">math</span>.<span style="color: #008000;">pow</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span>, index<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            difficulty += md<span style="color: black;">&#91;</span><span style="color: #483d8b;">'offset'</span><span style="color: black;">&#93;</span>
&nbsp;
        m_numbers.<span style="color: black;">append</span><span style="color: black;">&#40;</span>m_number<span style="color: black;">&#41;</span>
&nbsp;
    m_numbers_sum, m_numbers_diff = compute_addition_difficulty<span style="color: black;">&#40;</span>*m_numbers<span style="color: black;">&#41;</span>
    difficulty += m_numbers_diff
&nbsp;
    numbers = <span style="color: black;">&#91;</span>m_numbers_sum<span style="color: black;">&#93;</span> + numbers<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>:<span style="color: black;">&#93;</span>
    product, m_diff = compute_multiplication_difficulty<span style="color: black;">&#40;</span>*numbers<span style="color: black;">&#41;</span>
&nbsp;
    difficulty += m_diff
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> product, difficulty</pre></div></div>

<p><br/></p>
<h3>Division</h3>

<div class="wp_syntax"><div class="code"><pre class="python">division_difficulties = <span style="color: black;">&#123;</span>
    <span style="color: #808080; font-style: italic;"># long division</span>
    <span style="color: #483d8b;">'use_digit'</span>       : <span style="color: #ff4500;">1</span>,  <span style="color: #808080; font-style: italic;"># brinding down digit from dividend</span>
    <span style="color: #483d8b;">'multiple_lookup'</span> : <span style="color: #ff4500;">1</span>,  <span style="color: #808080; font-style: italic;"># looking up precomputed multiple of divisor</span>
    <span style="color: #483d8b;">'quotient_update'</span> : <span style="color: #ff4500;">1</span>,  <span style="color: #808080; font-style: italic;"># updating quotient with digit or period</span>
<span style="color: black;">&#125;</span></pre></div></div>

<p><br/></p>
<p>Implementing long division posed a problem because my understanding of the mechanics behind the long division algorithm was minimal if not inexistent. I spent some time trying to &#8220;reverse-engineer&#8221; it. I came across &#8220;Egyptian Division&#8221; which made things clearer. With a little help, I managed to implement the following division algorithm. Please let me know, if you come up with a better approach.</p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> compute_division_difficulty<span style="color: black;">&#40;</span>dividend, divisor, precision<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    Generates a difficulty number, quotient and remainder
    for dividend / divisor
    @precision (int) -- max required number of digits after decimal point in quotient
    returns: (quotient, remainder, difficulty)
&nbsp;
    &gt;&gt;&gt; compute_division_difficulty(54, 5, 0)
    (10.0, 4, 6)
    &gt;&gt;&gt; compute_division_difficulty(50, 5, 0)
    (10.0, 0, 6)
    &gt;&gt;&gt; compute_division_difficulty(575, 6, 0)
    (95.0, 5, 60)
    &gt;&gt;&gt; compute_division_difficulty(575, 6, 1)
    (95.829999999999998, 20, 112)
    &gt;&gt;&gt; compute_division_difficulty(6, 9, 1)
    (0.66000000000000003, 60, 50)
    &gt;&gt;&gt; compute_division_difficulty(410, 2, 1)
    (205.0, 0, 54)
    '</span><span style="color: #483d8b;">''</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> divisor: <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Division by Zero'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> dividend: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">1</span>
&nbsp;
    dividend = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>dividend<span style="color: black;">&#41;</span>
&nbsp;
    divisor  = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>divisor<span style="color: black;">&#41;</span>
&nbsp;
    difficulty = <span style="color: #ff4500;">0</span>
    previous_multiples_difficulty = <span style="color: #ff4500;">0</span>
    precision_reached = <span style="color: #ff4500;">0</span>
    decimal_point_used = <span style="color: #ff4500;">0</span>
    dd = division_difficulties
    num = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    quotient = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
    num = dividend<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
    difficulty += dd<span style="color: black;">&#91;</span><span style="color: #483d8b;">'use_digit'</span><span style="color: black;">&#93;</span>
    index = <span style="color: #ff4500;">0</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff4500;">1</span>:
        q_digit = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span> / <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>divisor<span style="color: black;">&#41;</span>
&nbsp;
        multiple, multiple_difficulty = compute_multiples_difficulty<span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>divisor<span style="color: black;">&#41;</span>, q_digit<span style="color: black;">&#41;</span>
        difficulty += <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: black;">fabs</span><span style="color: black;">&#40;</span>multiple_difficulty - previous_multiples_difficulty<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> previous_multiples_difficulty: difficulty += dd<span style="color: black;">&#91;</span><span style="color: #483d8b;">'multiple_lookup'</span><span style="color: black;">&#93;</span>
        previous_multiples_difficulty = <span style="color: #008000;">max</span><span style="color: black;">&#40;</span>multiple_difficulty, previous_multiples_difficulty<span style="color: black;">&#41;</span>
&nbsp;
        quotient.<span style="color: black;">append</span><span style="color: black;">&#40;</span> <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>q_digit<span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
        difficulty += dd<span style="color: black;">&#91;</span><span style="color: #483d8b;">'quotient_update'</span><span style="color: black;">&#93;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> decimal_point_used:
            precision_reached += <span style="color: #ff4500;">1</span>
&nbsp;
        num, sub_difficulty = compute_subtraction_difficulty<span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span>, multiple<span style="color: black;">&#41;</span>
        difficulty += sub_difficulty
        num = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span>
&nbsp;
        index += <span style="color: #ff4500;">1</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> index == <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>dividend<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> precision == <span style="color: #ff4500;">0</span>: <span style="color: #ff7700;font-weight:bold;">break</span>
            quotient.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'.'</span><span style="color: black;">&#41;</span>
            difficulty += dd<span style="color: black;">&#91;</span><span style="color: #483d8b;">'quotient_update'</span><span style="color: black;">&#93;</span>
            decimal_point_used = <span style="color: #ff4500;">1</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> decimal_point_used:
            num += dividend<span style="color: black;">&#91;</span>index<span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            num += <span style="color: #483d8b;">'0'</span>
&nbsp;
        difficulty += dd<span style="color: black;">&#91;</span><span style="color: #483d8b;">'use_digit'</span><span style="color: black;">&#93;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> precision_reached &gt;= precision + <span style="color: #ff4500;">1</span>:
            <span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
    remainder = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>num<span style="color: black;">&#41;</span>
    quotient = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>quotient<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> quotient, remainder, difficulty</pre></div></div>

<p><br/></p>
<p>You must&#8217;ve noticed the usage of compute_multiples_difficulty(). In long division, at every step, you will try to find the largest multiple of the divisor less than or equal to the number in hand to proceed further. During the process of division, if you&#8217;ve computed the 5th (say) multiple of the divisor with difficulty N. The computation of 6th multiple at a later point in the division is difficulty(lookup of 5th multiple) + difficulty(6-5th multiple).</p>
<h2>Results</h2>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #808080; font-style: italic;">#Problem 1</span>
&gt;&gt;&gt; compute_addition_difficulty<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #ff4500;">8</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Problem 2</span>
&gt;&gt;&gt; compute_addition_difficulty<span style="color: black;">&#40;</span><span style="color: #ff4500;">99999</span>, <span style="color: #ff4500;">12345</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #ff4500;">112344</span>, <span style="color: #ff4500;">22</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Problem 3</span>
&gt;&gt;&gt; compute_addition_difficulty<span style="color: black;">&#40;</span><span style="color: #ff4500;">1000000</span>, <span style="color: #ff4500;">1000001</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#40;</span><span style="color: #ff4500;">2000001</span>, <span style="color: #ff4500;">8</span><span style="color: black;">&#41;</span></pre></div></div>

<p><br/></p>
<p>Problem 1: <strong>2</strong><br />
Problem 2: <strong>22</strong><br />
Problem 3: <strong>8</strong></p>
<h2>The End</h2>
<p>I wish I could have written a more detailed explanation instead of sprinkling this article with code. Time however prevents me from doing so :(. It is some relief that Python is a <a href="/2008/07/11/even-a-python-can-be-abused/">wonderful language for readability</a> and the code samples above are pretty close to pseudo-code.</p>
<p>I will post back when I complete the program to generate arithmetic problems by order of difficulty. Until then, Adios amigos. Almost forgot! Here is the <a href="http://code.prashanthellina.com/code/atrainer.py" >code</a>.</p>
<p><center><img src="http://www.prashanthellina.com/images/meaningoflife.gif" alt="meaning of life math cartoon"/></center></p>
<h3></h3><ul class="related_post"><li><a href="http://blog.prashanthellina.com/2008/09/23/blockworlds-periodic-table/" title="Blockworld's Periodic Table">Blockworld's Periodic Table</a></li><li><a href="http://blog.prashanthellina.com/2008/07/11/even-a-python-can-be-abused/" title="Even a python can be abused">Even a python can be abused</a></li><li><a href="http://blog.prashanthellina.com/2008/05/22/nose-tdd-python/" title="Nose - TDD - Python">Nose - TDD - Python</a></li><li><a href="http://blog.prashanthellina.com/2008/05/04/n-gram-data-from-project-gutenberg/" title="N-gram data from Project Gutenberg">N-gram data from Project Gutenberg</a></li><li><a href="http://blog.prashanthellina.com/2008/04/22/alexa-rank-a-script-to-get-the-rank-for-any-site/" title="Alexa rank: A script to get the rank for any site">Alexa rank: A script to get the rank for any site</a></li><li><a href="http://blog.prashanthellina.com/2008/02/06/all-ncert-text-books/" title="All NCERT text books!">All NCERT text books!</a></li><li><a href="http://blog.prashanthellina.com/2008/01/07/interfacing-python-with-c-using-ctypes/" title="Interfacing Python with C using ctypes">Interfacing Python with C using ctypes</a></li><li><a href="http://blog.prashanthellina.com/2007/12/21/topic-extraction-using-wikipedia-data/" title="Topic extraction using Wikipedia data">Topic extraction using Wikipedia data</a></li><li><a href="http://blog.prashanthellina.com/2007/11/14/generating-call-graphs-for-understanding-and-refactoring-python-code/" title="Generating call graphs for understanding and refactoring python code">Generating call graphs for understanding and refactoring python code</a></li><li><a href="http://blog.prashanthellina.com/2007/10/17/ways-to-process-and-use-wikipedia-dumps/" title="Ways to process and use Wikipedia dumps">Ways to process and use Wikipedia dumps</a></li><li><a href="http://blog.prashanthellina.com/2007/08/26/word-arithmetic-puzzle-generator/" title="Word arithmetic puzzle generator">Word arithmetic puzzle generator</a></li><li><a href="http://blog.prashanthellina.com/2007/08/24/drawing-on-your-desktop/" title="Drawing on your Desktop">Drawing on your Desktop</a></li><li><a href="http://blog.prashanthellina.com/2007/08/22/matrix-desktop/" title="Matrix Desktop">Matrix Desktop</a></li></ul>
<p><a href="http://feeds.feedburner.com/~a/prashanthellina?a=s14Wt8"><img src="http://feeds.feedburner.com/~a/prashanthellina?i=s14Wt8" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/prashanthellina?a=OeZ5tJ"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=OeZ5tJ" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=ADZ81j"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=ADZ81j" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=Z69CDj"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=Z69CDj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=DN0KUj"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=DN0KUj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=AqxhvJ"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=AqxhvJ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/prashanthellina/~4/347565811" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.prashanthellina.com/2008/07/27/determining-the-difficulty-of-arithmetic-operations/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.prashanthellina.com/2008/07/27/determining-the-difficulty-of-arithmetic-operations/</feedburner:origLink></item>
		<item>
		<title>Even a python can be abused</title>
		<link>http://feeds.feedburner.com/~r/prashanthellina/~3/332313587/</link>
		<comments>http://blog.prashanthellina.com/2008/07/11/even-a-python-can-be-abused/#comments</comments>
		<pubDate>Fri, 11 Jul 2008 03:16:47 +0000</pubDate>
		<dc:creator>prashanthellina</dc:creator>
		
		<category><![CDATA[programming]]></category>

		<category><![CDATA[python]]></category>

		<category><![CDATA[ubuntu]]></category>

		<category><![CDATA[veveo]]></category>

		<category><![CDATA[C]]></category>

		<category><![CDATA[coding]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[perl]]></category>

		<category><![CDATA[readability]]></category>

		<category><![CDATA[VB]]></category>

		<category><![CDATA[visual basic]]></category>

		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=67</guid>
		<description><![CDATA[Related PostThe first programming language I coded in is QuickBasic. I loved the simplicity and especially the IDE. It made things simple for a starter. Later I discovered Visual Basic which extended the same simplicity and added the &#8220;Visual&#8221; element with a splendid editor for GUI.
In between I did some projects using Java, C#, C, C++. [...]]]></description>
			<content:encoded><![CDATA[Related Post<p><img src="http://www.prashanthellina.com/images/python_abuse.jpg" alt="python abuse" align="left"/>The first programming language I coded in is <strong>QuickBasic</strong>. I loved the simplicity and especially the IDE. It made things simple for a starter. Later I discovered <strong>Visual Basic</strong> which extended the same simplicity and added the &#8220;Visual&#8221; element with a splendid editor for GUI.</p>
<p>In between I did some projects using Java, C#, C, C++. None of these impressed me too much. I hated Java&#8217;s imposition of stiff rules and it&#8217;s dogged adherence to &#8220;everything in a class&#8221; attitude. C# was better. C++ just turned me off because of the monster it is. I did not like C at all because of its total lack of automated memory handling (like GC). I&#8217;ve been doing a lot of coding in C now-a-days as part of my job and I must admit that I like it a lot for its simplicity in primitives and promise of &#8220;closeness to hardware&#8221; and hence the predictability and performance.</p>
<p>I did a small part of my final year project using Python. However, for some unfathomable reason, Python did not impress me at all then. When I started working at Veveo I used Python for a project and got hooked. It&#8217;s simplicity and &#8220;readability&#8221; got me. The power of wielding this tool got me drunk <img src='http://blog.prashanthellina.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Python was designed from the beginning to be a &#8220;easy to read&#8221; language. Most, if not all, of the syntax is intuitive. The indentation adds to the readability aspect. The policy of &#8220;only one way to do a thing&#8221; does wonders for readability. Everyone does a certain thing only the &#8220;one&#8221; way. If you are wondering why the heck I am talking so much about readability, you should consider the fact that an average programmer spends <strong>most</strong> of his time &#8220;reading&#8221; code. You have to read your code after you&#8217;ve just written it. You&#8217;ve to read your code the next day when you resume work. You&#8217;ve to read your code the moment a bug is found. You have to read your code when someone asks you how some aspect of it works a couple of months later. You&#8217;ve have to read your code when making a teeny-weeny feature addition. I just cannot emphasize enough how much time is spent is just reading. There have been times when I would spend a whole day just reading code and finally making &#8220;a single line of code change&#8221; at the end of the day!</p>
<p>So there it is. Python makes it possible to write readable code and that does wonders to programmer productivity. Maintaining your code becomes easier. Understanding your collegues code becomes easier and most of all understanding code written by someone across the world becomes easier - so you can start reusing components more quickly and with more confidence than ever before.</p>
<p><strong><big>I simply love Python.</big></strong></p>
<p>You must be wondering where I am taking this discussion&#8230; because the title says something about a Python being abused. Who is abusing the Python anyway?</p>
<p>I&#8217;ve noticed how newbies code in Python and found it particulary intriguing. What is interesting here is that every person comes from a certain programming background and are used to doing things in a certain way. When they are thrown into a situation where they have to learn a new language and write some code in it, they invariably apply the vast experience they have in their language of choice <img src='http://blog.prashanthellina.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I&#8217;ve had the oppurtunity of observing people from Java, C, Perl, VB (yes, Perl) backgrounds writing code in Python. The Java guys stress test multiple inhertitance in Python and bring the much cherished &#8220;everything in a class&#8221; practice to the Python. The C guys who are more often than not obsessed with performace and optimization put their brains to work and implement a strcpy using a &#8220;for loop&#8221; and insist on doing a &#8220;shift&#8221; instead of &#8220;division/multiplication&#8221;. The Perl guys just don&#8217;t seem to like the alphanumerals. They craft Python code with ingenious application making it look very concise. The more characters in one line the better the code. The more non-alphanumerals the better coder you are. That&#8217;s the way of the &#8220;Perl&#8217;ies&#8221;. The VB guys languish for a while complaining constantly about the lack of a proper IDE and after trying out various Python editors, decide to call it quits and go home to comfortable VB. I know, I know &#8230;.. I was a VB guy too and I did search for IDE&#8217;s too &#8230; But then I found VIM and everything was good <img src='http://blog.prashanthellina.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I&#8217;ve had the good (snigger) fortune of maintaining some of these brilliant artifacts and had my share of nightmares and laugh-outs. I thought I had seen it all, until I saw something today. I told myself &#8212; &#8220;Never underestimate a brilliant C programmer who has found exec and eval in Python&#8221; &#8230; Yes, you can quote me on this <img src='http://blog.prashanthellina.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="python">    guido = <span style="color: #483d8b;">&quot;is speechless&quot;</span>
    larry = <span style="color: #483d8b;">&quot;went nuts&quot;</span>
    sergey = <span style="color: #483d8b;">&quot;has seen it all now&quot;</span>
    ...
    ...
    <span style="color: #ff7700;font-weight:bold;">for</span> idx <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">'guido'</span>,<span style="color: #483d8b;">'larry'</span>,<span style="color: #483d8b;">'sergey'</span><span style="color: black;">&#93;</span>:
        idxv=<span style="color: #008000;">eval</span><span style="color: black;">&#40;</span>idx<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">not</span> idxv <span style="color: #ff7700;font-weight:bold;">and</span> dd.<span style="color: black;">has_key</span><span style="color: black;">&#40;</span>idx<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
            idxv=<span style="color: #483d8b;">'%s=&quot;%s&quot;'</span> %<span style="color: black;">&#40;</span>idx,dd<span style="color: black;">&#91;</span>idx<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">exec</span><span style="color: black;">&#40;</span>idxv<span style="color: black;">&#41;</span>
&nbsp;
    ...
    ...</pre></div></div>

<p><br/><br />
<strong>No&#8230;.. I don&#8217;t think I&#8217;ve seen it all&#8230;. <img src='http://blog.prashanthellina.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </strong></p>
<h3></h3><ul class="related_post"><li><a href="http://blog.prashanthellina.com/2008/05/22/nose-tdd-python/" title="Nose - TDD - Python">Nose - TDD - Python</a></li><li><a href="http://blog.prashanthellina.com/2008/01/07/interfacing-python-with-c-using-ctypes/" title="Interfacing Python with C using ctypes">Interfacing Python with C using ctypes</a></li><li><a href="http://blog.prashanthellina.com/2007/12/21/topic-extraction-using-wikipedia-data/" title="Topic extraction using Wikipedia data">Topic extraction using Wikipedia data</a></li><li><a href="http://blog.prashanthellina.com/2007/10/17/ways-to-process-and-use-wikipedia-dumps/" title="Ways to process and use Wikipedia dumps">Ways to process and use Wikipedia dumps</a></li><li><a href="http://blog.prashanthellina.com/2007/08/26/word-arithmetic-puzzle-generator/" title="Word arithmetic puzzle generator">Word arithmetic puzzle generator</a></li><li><a href="http://blog.prashanthellina.com/2007/08/24/drawing-on-your-desktop/" title="Drawing on your Desktop">Drawing on your Desktop</a></li><li><a href="http://blog.prashanthellina.com/2007/08/22/matrix-desktop/" title="Matrix Desktop">Matrix Desktop</a></li><li><a href="http://blog.prashanthellina.com/2008/09/23/blockworlds-periodic-table/" title="Blockworld's Periodic Table">Blockworld's Periodic Table</a></li><li><a href="http://blog.prashanthellina.com/2008/07/27/determining-the-difficulty-of-arithmetic-operations/" title="Determining the difficulty of Arithmetic Operations">Determining the difficulty of Arithmetic Operations</a></li><li><a href="http://blog.prashanthellina.com/2008/05/04/n-gram-data-from-project-gutenberg/" title="N-gram data from Project Gutenberg">N-gram data from Project Gutenberg</a></li><li><a href="http://blog.prashanthellina.com/2008/04/22/alexa-rank-a-script-to-get-the-rank-for-any-site/" title="Alexa rank: A script to get the rank for any site">Alexa rank: A script to get the rank for any site</a></li><li><a href="http://blog.prashanthellina.com/2008/02/06/all-ncert-text-books/" title="All NCERT text books!">All NCERT text books!</a></li><li><a href="http://blog.prashanthellina.com/2007/11/29/language-people-interesting-picture/" title="Language People - Interesting picture">Language People - Interesting picture</a></li><li><a href="http://blog.prashanthellina.com/2007/11/14/generating-call-graphs-for-understanding-and-refactoring-python-code/" title="Generating call graphs for understanding and refactoring python code">Generating call graphs for understanding and refactoring python code</a></li><li><a href="http://blog.prashanthellina.com/2007/09/29/vtap-windows-mobile-source-code/" title="vTap Windows Mobile source code">vTap Windows Mobile source code</a></li></ul>
<p><a href="http://feeds.feedburner.com/~a/prashanthellina?a=iypyhz"><img src="http://feeds.feedburner.com/~a/prashanthellina?i=iypyhz" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/prashanthellina?a=bi8p6J"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=bi8p6J" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=7hHNYj"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=7hHNYj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=t4CfGj"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=t4CfGj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=BbTSmj"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=BbTSmj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=PRJGWJ"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=PRJGWJ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/prashanthellina/~4/332313587" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.prashanthellina.com/2008/07/11/even-a-python-can-be-abused/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.prashanthellina.com/2008/07/11/even-a-python-can-be-abused/</feedburner:origLink></item>
		<item>
		<title>Most interesting chat conversation I ever had</title>
		<link>http://feeds.feedburner.com/~r/prashanthellina/~3/330683748/</link>
		<comments>http://blog.prashanthellina.com/2008/07/09/most-interesting-chat-conversation-i-ever-had/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 11:05:27 +0000</pubDate>
		<dc:creator>prashanthellina</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[chat]]></category>

		<category><![CDATA[conversation]]></category>

		<category><![CDATA[crazy]]></category>

		<category><![CDATA[fun]]></category>

		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=68</guid>
		<description><![CDATA[vikraman: hey!
me: hey, solra.
vikraman: just to say hi
me: hi
vikraman: hi
me:  okay. bye.
vikraman: bye

If you want to have more interesting conversations with my friend Vikraman, head over to his blog.
]]></description>
			<content:encoded><![CDATA[<blockquote><p>
<strong>vikraman</strong>: hey!<br />
<strong>me</strong>: hey, solra.<br />
<strong>vikraman</strong>: just to say hi<br />
<strong>me</strong>: hi<br />
<strong>vikraman</strong>: hi<br />
<strong>me</strong>: <img src='http://blog.prashanthellina.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> okay. bye.<br />
<strong>vikraman</strong>: bye
</p></blockquote>
<p>If you want to have more interesting conversations with my friend <a href="http://vikraman.blogspot.com" onclick="javascript:urchinTracker ('/outbound/article/vikraman.blogspot.com');">Vikraman</a>, head over to his <a href="http://vikraman.blogspot.com" onclick="javascript:urchinTracker ('/outbound/article/vikraman.blogspot.com');">blog</a>.</p>

<p><a href="http://feeds.feedburner.com/~a/prashanthellina?a=t98OJC"><img src="http://feeds.feedburner.com/~a/prashanthellina?i=t98OJC" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/prashanthellina?a=MZlhyJ"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=MZlhyJ" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=tVWzvj"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=tVWzvj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=Y7y3Nj"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=Y7y3Nj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=03CdKj"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=03CdKj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=oeuyQJ"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=oeuyQJ" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/prashanthellina/~4/330683748" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.prashanthellina.com/2008/07/09/most-interesting-chat-conversation-i-ever-had/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.prashanthellina.com/2008/07/09/most-interesting-chat-conversation-i-ever-had/</feedburner:origLink></item>
		<item>
		<title>Look who’s downloaded Firefox 3!</title>
		<link>http://feeds.feedburner.com/~r/prashanthellina/~3/314624329/</link>
		<comments>http://blog.prashanthellina.com/2008/06/18/look-whos-downloaded-firefox-3/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 13:41:08 +0000</pubDate>
		<dc:creator>prashanthellina</dc:creator>
		
		<category><![CDATA[web]]></category>

		<category><![CDATA[download day]]></category>

		<category><![CDATA[firefox]]></category>

		<category><![CDATA[firefox3]]></category>

		<category><![CDATA[guiness book]]></category>

		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=66</guid>
		<description><![CDATA[Related PostFirefox 3 has been getting rave reviews ever since it got in to beta. The blogosphere was abuzz with reports on how much more efficient and snappier FF3 is compared to its earlier incarnations and more so with respect to the competition (Opera, IE7, Safari).
Features like &#8220;Places&#8221; (Bookmarks on steroids), Cairo for rendering and OS [...]]]></description>
			<content:encoded><![CDATA[Related Post<p>Firefox 3 has been getting rave reviews ever since it got in to beta. The blogosphere was abuzz with reports on how much more efficient and snappier FF3 is compared to its earlier incarnations and more so with respect to the competition (Opera, IE7, Safari).</p>
<p>Features like &#8220;Places&#8221; (Bookmarks on steroids), Cairo for rendering and OS specific widgets have made the best browser better. At the time of writing of this blog post 6 million plus downloads from around the world have already happened.</p>
<p>Everywhere I look at work, I see the &#8220;Download Day&#8221; certificate from Mozilla corp &#8230; I got one myself too <img src='http://blog.prashanthellina.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>But the question is &#8230;.</p>
<blockquote><p>
<font size="+3"><br />
<strong><br />
Do you know who else got the certificate ???<br />
</strong><br />
</font>
</p></blockquote>
<p>Who else but Billy and Ballmy <img src='http://blog.prashanthellina.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><img src="http://www.prashanthellina.com/images/billgates_ff3_cert.jpg" alt="Bill gates firefox 3 certificate"/></p>
<p><img src="http://www.prashanthellina.com/images/steveballmer_ff3_cert.jpg" alt="Steve Ballmer firefox 3 certificate"/></p>
<h3></h3><ul class="related_post"><li><a href="http://blog.prashanthellina.com/2007/12/05/firefox-tattoo-from-fossin-2007/" title="Firefox tattoo from FOSS.IN 2007">Firefox tattoo from FOSS.IN 2007</a></li></ul>
<p><a href="http://feeds.feedburner.com/~a/prashanthellina?a=3QPeYV"><img src="http://feeds.feedburner.com/~a/prashanthellina?i=3QPeYV" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/prashanthellina?a=Dx6uOI"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=Dx6uOI" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=Ot3E0i"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=Ot3E0i" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=Ya0Sji"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=Ya0Sji" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=Fy1oXi"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=Fy1oXi" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=ubi4uI"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=ubi4uI" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/prashanthellina/~4/314624329" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.prashanthellina.com/2008/06/18/look-whos-downloaded-firefox-3/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.prashanthellina.com/2008/06/18/look-whos-downloaded-firefox-3/</feedburner:origLink></item>
		<item>
		<title>Nose - TDD - Python</title>
		<link>http://feeds.feedburner.com/~r/prashanthellina/~3/295881895/</link>
		<comments>http://blog.prashanthellina.com/2008/05/22/nose-tdd-python/#comments</comments>
		<pubDate>Thu, 22 May 2008 15:02:08 +0000</pubDate>
		<dc:creator>prashanthellina</dc:creator>
		
		<category><![CDATA[linux]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[python]]></category>

		<category><![CDATA[nose]]></category>

		<category><![CDATA[tdd]]></category>

		<category><![CDATA[testing]]></category>

		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=65</guid>
		<description><![CDATA[Related PostWhat, why
I&#8217;ve been reading up on TDD and it has struck me as particularly useful methodology to achieve &#8220;clean code that works&#8221;. TDD encourages writing unit tests to cover all the code (because by definition, you write a test before a line of code is written). Because all your code is covered you are freed [...]]]></description>
			<content:encoded><![CDATA[Related Post<h3>What, why</h3>
<p>I&#8217;ve been reading up on <a href="http://en.wikipedia.org/wiki/Test-driven_development" onclick="javascript:urchinTracker ('/outbound/article/en.wikipedia.org');">TDD</a> and it has struck me as particularly useful methodology to achieve &#8220;clean code that works&#8221;. TDD encourages writing unit tests to cover all the code (because by definition, you write a test before a line of code is written). Because all your code is covered you are freed from the fear of breakage due to change and can instantly be more confident and productive. Also, the test cases act as a specification in code - very useful.</p>
<p>Python has standard modules, <a href="http://docs.python.org/lib/module-unittest.html" onclick="javascript:urchinTracker ('/outbound/article/docs.python.org');">unittest</a> and <a href="http://docs.python.org/lib/module-doctest.html" onclick="javascript:urchinTracker ('/outbound/article/docs.python.org');">doctest</a> to help you write test cases. I simply love doctest. It alleviates much of the pain of writing a test case (setup and all) besides acting as &#8220;executable documentation&#8221;. The unittest module has a Java legacy and is not to my taste. Also, I wanted to find a solution that would help in automated test enumeration (discovery) in my source directories without having to write any &#8220;infrastructure&#8221; code. One more thing I was looking for was a way to run both unit tests and doc tests together.</p>
<p>After a bit of searching, I found &#8220;<a href="http://somethingaboutorange.com/mrl/projects/nose/" onclick="javascript:urchinTracker ('/outbound/article/somethingaboutorange.com');">Nose</a>&#8220;. Nose is a clone of &#8220;<a href="http://codespeak.net/py/dist/test.html" onclick="javascript:urchinTracker ('/outbound/article/codespeak.net');">py.test</a>&#8221; which I liked better than the original (subjectively). To get a feel of &#8220;Nose&#8221;, I set up some python test files.</p>
<p>The following is the directory structure and the contents of the files. I&#8217;ve put in both unit tests and doc tests in the files to see how &#8220;Nose&#8221; handles them. Also, the tests are spread across directories. Note that I had to put an &#8220;__init__.py&#8221; to allow &#8220;Nose&#8221; to import tests in a subdirectory.</p>
<h3>The setup</h3>
<p><strong>The directory structure</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash">prashanth@prashanth-desktop:~/tmp$ tree
.
|-- bingo.py
|-- somedir
|   |-- __init__.py
|   `-- test_another.py
`-- test_prashanth.py
&nbsp;
<span style="color: #000000;">1</span> directory, <span style="color: #000000;">4</span> files</pre></div></div>

<p><br/></p>
<p><strong><a href="http://www.littlewoodsbingo.com" onclick="javascript:urchinTracker ('/outbound/article/www.littlewoodsbingo.com');">bingo</a>.py</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> boing<span style="color: black;">&#40;</span>a, b<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    &gt;&gt;&gt; boing(10, 20)
    30
    '</span><span style="color: #483d8b;">''</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> a+b
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> boing1<span style="color: black;">&#40;</span>a, b<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">''</span><span style="color: #483d8b;">'
    &gt;&gt;&gt; boing1(10, 20)
    40
    '</span><span style="color: #483d8b;">''</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> a+b</pre></div></div>

<p><br/></p>
<p><strong>test_prashanth.py</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> test_a<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">assert</span> <span style="color: #ff4500;">1</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> test_b<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;hello&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">assert</span> <span style="color: #ff4500;">0</span></pre></div></div>

<p><br/></p>
<p><strong>somedir/test_another.py</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python"><span style="color: #ff7700;font-weight:bold;">def</span> test_bingo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'hgello'</span><span style="color: black;">&#41;</span></pre></div></div>

<p><br/></p>
<h3>Installing &#8220;Nose&#8221;</h3>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #c20cb9; font-weight: bold;">sudo</span> easy_install nose</pre></div></div>

<p><br/></p>
<p>If you don&#8217;t have easy_install, head over <a href="http://somethingaboutorange.com/mrl/projects/nose/" onclick="javascript:urchinTracker ('/outbound/article/somethingaboutorange.com');">here</a> to get information on installation.</p>
<h3>Running the tests</h3>
<p>Now that &#8220;Nose&#8221; is installed, let us run the tests,</p>

<div class="wp_syntax"><div class="code"><pre class="bash">nosetests --with-doctest</pre></div></div>

<p><br/></p>
<p>The output is</p>

<div class="wp_syntax"><div class="code"><pre class="bash">..E.F
======================================================================
ERROR: somedir.test_another.test_bingo
----------------------------------------------------------------------
Traceback <span style="color: #7a0874; font-weight: bold;">&#40;</span>most recent call <span style="color: #c20cb9; font-weight: bold;">last</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>:
  File <span style="color: #ff0000;">&quot;/usr/lib/python2.5/site-packages/nose-0.10.2-py2.5.egg/nose/case.py&quot;</span>, line <span style="color: #000000;">182</span>, <span style="color: #000000; font-weight: bold;">in</span> runTest
    self.<span style="color: #7a0874; font-weight: bold;">test</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>*self.arg<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  File <span style="color: #ff0000;">&quot;/home/prashanth/tmp/somedir/test_another.py&quot;</span>, line <span style="color: #000000;">2</span>, <span style="color: #000000; font-weight: bold;">in</span> test_bingo
    raise Exception<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'hgello'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
Exception: hgello
&nbsp;
======================================================================
FAIL: test_prashanth.test_b
----------------------------------------------------------------------
Traceback <span style="color: #7a0874; font-weight: bold;">&#40;</span>most recent call <span style="color: #c20cb9; font-weight: bold;">last</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>:
  File <span style="color: #ff0000;">&quot;/usr/lib/python2.5/site-packages/nose-0.10.2-py2.5.egg/nose/case.py&quot;</span>, line <span style="color: #000000;">182</span>, <span style="color: #000000; font-weight: bold;">in</span> runTest
    self.<span style="color: #7a0874; font-weight: bold;">test</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>*self.arg<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  File <span style="color: #ff0000;">&quot;/home/prashanth/tmp/test_prashanth.py&quot;</span>, line <span style="color: #000000;">7</span>, <span style="color: #000000; font-weight: bold;">in</span> test_b
    assert <span style="color: #000000;">0</span>
AssertionError:
-------------------- &gt;&gt; begin captured stdout &lt;&lt; ---------------------
hello
&nbsp;
--------------------- &gt;&gt; end captured stdout &lt;&lt; ----------------------
&nbsp;
----------------------------------------------------------------------
Ran <span style="color: #000000;">5</span> tests <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000;">0</span>.057s
&nbsp;
FAILED <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">errors=</span><span style="color: #000000;">1</span>, <span style="color: #007800;">failures=</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p><br/></p>
<p>The first line in the output is the &#8220;test progress&#8221; indication (..E.F) . When a test succeeds, a &#8216;.&#8217; is written. When a test fails, an &#8216;F&#8217; is written. When a test throws an Exception, an &#8216;E&#8217; is written. Very useful to get a sense of progress as a huge test suite being executed.</p>
<p>&#8220;Nose&#8221; captures the stdout and stderr when a test case fails to help you debug the issue. To <a href="http://ivory.idyll.org/articles/nose-intro.html" onclick="javascript:urchinTracker ('/outbound/article/ivory.idyll.org');">learn more about using &#8220;Nose&#8221; go here</a>.</p>
<h3></h3><ul class="related_post"><li><a href="http://blog.prashanthellina.com/2007/08/24/drawing-on-your-desktop/" title="Drawing on your Desktop">Drawing on your Desktop</a></li><li><a href="http://blog.prashanthellina.com/2007/08/22/matrix-desktop/" title="Matrix Desktop">Matrix Desktop</a></li><li><a href="http://blog.prashanthellina.com/2008/07/11/even-a-python-can-be-abused/" title="Even a python can be abused">Even a python can be abused</a></li><li><a href="http://blog.prashanthellina.com/2008/02/03/create-pdf-thumbnails-using-imagemagick-on-linux/" title="Create PDF thumbnails using ImageMagick on Linux">Create PDF thumbnails using ImageMagick on Linux</a></li><li><a href="http://blog.prashanthellina.com/2008/01/07/interfacing-python-with-c-using-ctypes/" title="Interfacing Python with C using ctypes">Interfacing Python with C using ctypes</a></li><li><a href="http://blog.prashanthellina.com/2007/12/21/topic-extraction-using-wikipedia-data/" title="Topic extraction using Wikipedia data">Topic extraction using Wikipedia data</a></li><li><a href="http://blog.prashanthellina.com/2007/10/17/ways-to-process-and-use-wikipedia-dumps/" title="Ways to process and use Wikipedia dumps">Ways to process and use Wikipedia dumps</a></li><li><a href="http://blog.prashanthellina.com/2007/08/26/word-arithmetic-puzzle-generator/" title="Word arithmetic puzzle generator">Word arithmetic puzzle generator</a></li><li><a href="http://blog.prashanthellina.com/2008/09/23/blockworlds-periodic-table/" title="Blockworld's Periodic Table">Blockworld's Periodic Table</a></li><li><a href="http://blog.prashanthellina.com/2008/07/27/determining-the-difficulty-of-arithmetic-operations/" title="Determining the difficulty of Arithmetic Operations">Determining the difficulty of Arithmetic Operations</a></li><li><a href="http://blog.prashanthellina.com/2008/05/04/n-gram-data-from-project-gutenberg/" title="N-gram data from Project Gutenberg">N-gram data from Project Gutenberg</a></li><li><a href="http://blog.prashanthellina.com/2008/04/22/alexa-rank-a-script-to-get-the-rank-for-any-site/" title="Alexa rank: A script to get the rank for any site">Alexa rank: A script to get the rank for any site</a></li><li><a href="http://blog.prashanthellina.com/2008/03/30/visualizing-mpeg4-motion-compensation-vectors-using-mplayer/" title="Visualizing mpeg4 motion compensation vectors using mplayer">Visualizing mpeg4 motion compensation vectors using mplayer</a></li><li><a href="http://blog.prashanthellina.com/2008/03/29/creating-video-thumbnails-using-ffmpeg/" title="Creating video thumbnails using ffmpeg">Creating video thumbnails using ffmpeg</a></li><li><a href="http://blog.prashanthellina.com/2008/03/23/watching-television-on-linux-setting-up-a-tv-tuner-card/" title="Watching Television on Linux: setting up a TV Tuner card">Watching Television on Linux: setting up a TV Tuner card</a></li><li><a href="http://blog.prashanthellina.com/2008/02/06/all-ncert-text-books/" title="All NCERT text books!">All NCERT text books!</a></li><li><a href="http://blog.prashanthellina.com/2008/01/13/kde4-on-kubuntu-impressions-and-screenshots/" title="KDE4 on Kubuntu - Impressions and Screenshots">KDE4 on Kubuntu - Impressions and Screenshots</a></li><li><a href="http://blog.prashanthellina.com/2007/11/29/language-people-interesting-picture/" title="Language People - Interesting picture">Language People - Interesting picture</a></li><li><a href="http://blog.prashanthellina.com/2007/11/27/building-a-low-cost-bad-ass-server-machine/" title="Building a low-cost bad-ass "server" machine">Building a low-cost bad-ass "server" machine</a></li><li><a href="http://blog.prashanthellina.com/2007/11/14/generating-call-graphs-for-understanding-and-refactoring-python-code/" title="Generating call graphs for understanding and refactoring python code">Generating call graphs for understanding and refactoring python code</a></li><li><a href="http://blog.prashanthellina.com/2007/10/21/making-ubuntu-710-gutsy-look-slicker/" title="Making Ubuntu 7.10 (Gutsy) look slicker">Making Ubuntu 7.10 (Gutsy) look slicker</a></li><li><a href="http://blog.prashanthellina.com/2007/10/06/ubuntu-gutsy-gibbon-and-linux-on-the-desktop/" title="Ubuntu Gutsy Gibbon and Linux on the Desktop">Ubuntu Gutsy Gibbon and Linux on the Desktop</a></li><li><a href="http://blog.prashanthellina.com/2007/09/29/vtap-windows-mobile-source-code/" title="vTap Windows Mobile source code">vTap Windows Mobile source code</a></li></ul>
<p><a href="http://feeds.feedburner.com/~a/prashanthellina?a=S5lFFP"><img src="http://feeds.feedburner.com/~a/prashanthellina?i=S5lFFP" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/prashanthellina?a=hKm4UH"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=hKm4UH" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=xNp1Ph"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=xNp1Ph" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=f2sMrh"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=f2sMrh" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=b1VSuh"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=b1VSuh" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=cl5eFH"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=cl5eFH" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/prashanthellina/~4/295881895" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.prashanthellina.com/2008/05/22/nose-tdd-python/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.prashanthellina.com/2008/05/22/nose-tdd-python/</feedburner:origLink></item>
		<item>
		<title>Project Gutenberg Ngram data: English only</title>
		<link>http://feeds.feedburner.com/~r/prashanthellina/~3/289547917/</link>
		<comments>http://blog.prashanthellina.com/2008/05/13/project-gutenberg-ngram-data-english-only/#comments</comments>
		<pubDate>Tue, 13 May 2008 16:36:05 +0000</pubDate>
		<dc:creator>prashanthellina</dc:creator>
		
		<category><![CDATA[data mining]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[text processing]]></category>

		<category><![CDATA[ngram]]></category>

		<category><![CDATA[project gutenberg]]></category>

		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=64</guid>
		<description><![CDATA[Related PostIn my earlier post, I&#8217;d posted links to the Project Gutenberg Ngram data I had computed for e-books of all languages. If you are interested in only the English data, get these files instead.


These two files are splits of a compressed file which contains all of the Project Gutenberg English e-books downloaded about a week [...]]]></description>
			<content:encoded><![CDATA[Related Post<p>
In my <a href="/2008/05/04/n-gram-data-from-project-gutenberg/">earlier post</a>, I&#8217;d posted links to the <a href="http://www.gutenberg.org" onclick="javascript:urchinTracker ('/outbound/article/www.gutenberg.org');">Project Gutenberg</a> Ngram data I had computed for e-books of all languages. If you are interested in only the English data, get these files instead.
</p>
<p>
These two files are splits of a compressed file which contains all of the Project Gutenberg English e-books downloaded about a week before the date of this post.<br/><br />
<a href="http://www.prashanthellina.com/docs/gutenberg_data/gutenberg_en_files.tar.bz2.0" >gutenberg_en_files.tar.bz2.0</a> (<strong>2.0GB</strong>) <br/></p>
<p><a href="http://www.prashanthellina.com/docs/gutenberg_data/gutenberg_en_files.tar.bz2.1" >gutenberg_en_files.tar.bz2.1</a> (<strong>1.4GB</strong>) <br/></p>
<p>Unigrams along with frequency count from the text data above<br/><br />
<a href="http://www.prashanthellina.com/docs/gutenberg_data/gutenberg_en_unigrams.tar.gz" >gutenberg_en_unigrams.tar.gz</a> (<strong>7.4MB</strong>) <br/></p>
<p>Bi-grams and Tri-grams along with frequency count from the text data above<br/><br />
<a href="http://www.prashanthellina.com/docs/gutenberg_data/gutenberg_en_bi_tri_grams.tar.gz" >gutenberg_en_bi_tri_grams.tar.gz</a> (<strong>493MB</strong>) <br/>
</p>
<p>I had to split the files because my webserver has a limitation in serving out files larger than 2GB. After downloading the files, do this</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #c20cb9; font-weight: bold;">mv</span> gutenberg_en_files.<span style="color: #c20cb9; font-weight: bold;">tar</span>.bz2<span style="color: #000000;">.0</span> gutenberg_en_files.<span style="color: #c20cb9; font-weight: bold;">tar</span>.bz2
<span style="color: #c20cb9; font-weight: bold;">cat</span> gutenberg_en_files.<span style="color: #c20cb9; font-weight: bold;">tar</span>.bz2<span style="color: #000000;">.1</span> &gt;&gt; gutenberg_en_files.<span style="color: #c20cb9; font-weight: bold;">tar</span>.bz2
<span style="color: #c20cb9; font-weight: bold;">rm</span> gutenberg_en_files.<span style="color: #c20cb9; font-weight: bold;">tar</span>.bz2<span style="color: #000000;">.1</span></pre></div></div>

<p><br/></p>
<p>If you find the data useful, I&#8217;d be delighted to hear the context in which you made use of it.</p>
<h3></h3><ul class="related_post"><li><a href="http://blog.prashanthellina.com/2007/12/21/topic-extraction-using-wikipedia-data/" title="Topic extraction using Wikipedia data">Topic extraction using Wikipedia data</a></li><li><a href="http://blog.prashanthellina.com/2007/10/17/ways-to-process-and-use-wikipedia-dumps/" title="Ways to process and use Wikipedia dumps">Ways to process and use Wikipedia dumps</a></li><li><a href="http://blog.prashanthellina.com/2008/05/04/n-gram-data-from-project-gutenberg/" title="N-gram data from Project Gutenberg">N-gram data from Project Gutenberg</a></li></ul>
<p><a href="http://feeds.feedburner.com/~a/prashanthellina?a=lWqqPf"><img src="http://feeds.feedburner.com/~a/prashanthellina?i=lWqqPf" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/prashanthellina?a=2kex1H"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=2kex1H" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=oWo4ph"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=oWo4ph" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=iedgdh"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=iedgdh" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=CTSvdh"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=CTSvdh" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=GsOPtH"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=GsOPtH" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/prashanthellina/~4/289547917" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.prashanthellina.com/2008/05/13/project-gutenberg-ngram-data-english-only/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.prashanthellina.com/2008/05/13/project-gutenberg-ngram-data-english-only/</feedburner:origLink></item>
		<item>
		<title>N-gram data from Project Gutenberg</title>
		<link>http://feeds.feedburner.com/~r/prashanthellina/~3/283384260/</link>
		<comments>http://blog.prashanthellina.com/2008/05/04/n-gram-data-from-project-gutenberg/#comments</comments>
		<pubDate>Sun, 04 May 2008 16:40:14 +0000</pubDate>
		<dc:creator>prashanthellina</dc:creator>
		
		<category><![CDATA[data mining]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[python]]></category>

		<category><![CDATA[text processing]]></category>

		<category><![CDATA[gutenberg]]></category>

		<category><![CDATA[ngrams]]></category>

		<category><![CDATA[project gutenberg]]></category>

		<category><![CDATA[text parsing]]></category>

		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=63</guid>
		<description><![CDATA[Related Post  I&#8217;ve been working on Wordza.com for which I needed Ngram data from a sufficiently large corpus. Initially,  I thought of using Wikipedia data which I already have on my disk, but decided on using Project Gutenberg data as it is more representative of the general usage of English language.

Get Project Gutenberg Ngram [...]]]></description>
			<content:encoded><![CDATA[Related Post<p>
  I&#8217;ve been working on <a href="http://www.wordza.com" name="Wordza" onclick="javascript:urchinTracker ('/outbound/article/www.wordza.com');">Wordza.com</a> for which I needed Ngram data from a sufficiently large corpus. Initially,  I thought of using Wikipedia data which I already <a href="/2007/12/21/topic-extraction-using-wikipedia-data/">have on my disk</A>, but decided on using <A href="http://www.gutenberg.org" onclick="javascript:urchinTracker ('/outbound/article/www.gutenberg.org');">Project Gutenberg</a> data as it is more representative of the general usage of English language.
</p>
<h2>Get Project Gutenberg Ngram data</h2>
<p>
The Ngram data contains bi-grams and tri-grams for now. I plan to generate uni-grams soon. I&#8217;ve made the data available here so you can download and use it! This data contains all of the e-books hosted by Project Gutenberg (which means the data contains English, French, German and other languages). If you want an English only dataset, check back in a week or two. I am in the process of generating the same.
</p>
<p>
  The Ngram data containing bi-grams and tri-grams. Each line is prepended with the occurence count.<br/><br />
<a href="http://www.prashanthellina.com/docs/gutenberg_data/gutenberg_ngrams.tar.bz2" >gutenberg_ngrams.tar.bz2</a> (<strong>624 MB</strong>)<br/></p>
<p><br/></p>
<p>This is the compressed tarball of all the txt files in Project Gutenberg (as of a week before this blog post). Note that you don&#8217;t need this file unless you want to generate the Ngrams yourself using the scripts provided below.<br/><br />
<a href="http://www.prashanthellina.com/docs/gutenberg_data/gutenberg_files.tar.bz2.0" >gutenberg_files.tar.bz2.0</a>,<br />
<a href="http://www.prashanthellina.com/docs/gutenberg_data/gutenberg_files.tar.bz2.1" >gutenberg_files.tar.bz2.1</a>,<br />
<a href="http://www.prashanthellina.com/docs/gutenberg_data/gutenberg_files.tar.bz2.2" >gutenberg_files.tar.bz2.2</a> (<strong>5.3 GB</strong>)<br/></p>
<p>My webserver (Apache) has a problem serving out files bigger than 2GB, so I had to split the file up. After you download the splits, you have to join them like this.</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #c20cb9; font-weight: bold;">mv</span> gutenberg_files.<span style="color: #c20cb9; font-weight: bold;">tar</span>.bz2<span style="color: #000000;">.0</span> gutenberg_files.<span style="color: #c20cb9; font-weight: bold;">tar</span>.bz2
<span style="color: #c20cb9; font-weight: bold;">cat</span> gutenberg_files.<span style="color: #c20cb9; font-weight: bold;">tar</span>.bz2<span style="color: #000000;">.1</span> &gt;&gt; gutenberg_files.<span style="color: #c20cb9; font-weight: bold;">tar</span>.bz2
<span style="color: #c20cb9; font-weight: bold;">cat</span> gutenberg_files.<span style="color: #c20cb9; font-weight: bold;">tar</span>.bz2<span style="color: #000000;">.2</span> &gt;&gt; gutenberg_files.<span style="color: #c20cb9; font-weight: bold;">tar</span>.bz2</pre></div></div>

<p><br/></p>
<p>To decompress the files, you will need bunzip2 on *nix/Cygwin. On Windows, use 7zip.
</p>
<h2>Generate the data yourself</h2>
<p>
In case you want to generate the Ngrams yourself by processing the Project Gutenberg data files, follow these instructions. You will have to get the Project gutenberg data files. Use the following command to get all the English language files in txt format.</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #c20cb9; font-weight: bold;">mkdir</span> gutenberg
<span style="color: #7a0874; font-weight: bold;">cd</span> gutenberg
<span style="color: #c20cb9; font-weight: bold;">wget</span> -<span style="color: #c20cb9; font-weight: bold;">w</span> <span style="color: #000000;">2</span> -m <span style="color: #ff0000;">&quot;http://www.gutenberg.org/robot/harvest?filetypes[]=txt&amp;langs[]=en&quot;</span></pre></div></div>

<p><br/></p>
<p>The txt files are compressed and stored in files ending with .zip extension. These zip files are spread across multiple directories. The following command will move the zip files into the &#8220;gutenberg&#8221; directory you created in the above step.</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> `<span style="color: #c20cb9; font-weight: bold;">find</span> . -name <span style="color: #ff0000;">&quot;*.zip&quot;</span>`; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$i</span> . ; <span style="color: #000000; font-weight: bold;">done</span>;</pre></div></div>

<p><br/></p>
<p>Now that all the zip files are in the same directory, unzip the zip files.Some zip files may contain files other than .txt&#8217;s. The following command extracts only .txt&#8217;s in the zip files.</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #7a0874; font-weight: bold;">cd</span> ..
<span style="color: #c20cb9; font-weight: bold;">mkdir</span> gutenberg_txt
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> `<span style="color: #c20cb9; font-weight: bold;">find</span> gutenberg -name <span style="color: #ff0000;">&quot;*.zip&quot;</span>`; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #c20cb9; font-weight: bold;">unzip</span> <span style="color: #007800;">$i</span> \*.txt -d gutenberg_txt/ ; <span style="color: #000000; font-weight: bold;">done</span>;
<span style="color: #7a0874; font-weight: bold;">cd</span> gutenberg_txt
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> `<span style="color: #c20cb9; font-weight: bold;">find</span> . -name <span style="color: #ff0000;">&quot;*.txt&quot;</span>`; <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$i</span> . ; <span style="color: #000000; font-weight: bold;">done</span>;
<span style="color: #7a0874; font-weight: bold;">cd</span> ..</pre></div></div>

<p><br/></p>
<p>The gutenberg txt files have gutenberg headers and footers which should be removed lest they skew the frequency of Ngrams. The script &#8220;remove_gutenberg_text.py&#8221; does exactly this. The &#8220;generate_ngrams.py&#8221; script creates uni, bi and tri-grams of whatever text is piped into it. The following command pipes all the txt files through both the scripts to create the ngrams file.</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> `<span style="color: #c20cb9; font-weight: bold;">find</span> gutenberg_txt/ -name <span style="color: #ff0000;">&quot;*.txt&quot;</span>`; \
<span style="color: #000000; font-weight: bold;">do</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$i</span> | python remove_gutenberg_text.py | \
<span style="color: #c20cb9; font-weight: bold;">grep</span> -i -v <span style="color: #ff0000;">&quot;project gutenberg&quot;</span> |\
 python generate_ngrams.py &gt;&gt; gutenberg_ngrams; <span style="color: #000000; font-weight: bold;">done</span>;</pre></div></div>

<p><br/></p>
<p>Now you have to count the number of times an ngram occurs. The following sequence of commands process the ngrams file generated above and produce a file with the frequency counts of the ngrams. Note that the &#8220;512K&#8221; option to sort is because I had to run these scripts on my host which kills processes that take too much memory. If you have a machine with a lot of memory, sorting can be significantly faster if you use a higher value, say &#8220;1G&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #c20cb9; font-weight: bold;">sort</span> -S 512K -T tmp_sort/ gutenberg_ngrams &gt; gutenberg_ngrams.sorted
<span style="color: #c20cb9; font-weight: bold;">uniq</span> -c gutenberg_ngrams.sorted &gt; gutenberg_ngrams.counted
<span style="color: #c20cb9; font-weight: bold;">sort</span> -S 512K -T tmp_sort/ gutenberg_ngrams.counted &gt; gutenberg_ngrams.counted.sorted</pre></div></div>

<p><br/>
</p>
<h3>Gutenberg data processing scripts</h3>
<ul>
<li><a href="http://code.prashanthellina.com/code/remove_gutenberg_text.py" >remove_gutenberg_text.py</a> &#8212; removes Project Gutenberg header and footer from txt files</li>
<li><a href="http://code.prashanthellina.com/code/generate_ngrams.py" >generate_ngrams.py</a> &#8212; generate uni, bi and tri-grams for any text</li>
</ul>
<h2>Do get back</h2>
<p>If you use this data, I would really appreciate if you get back with details about how you used it in the context of your project</p>
<h3></h3><ul class="related_post"><li><a href="http://blog.prashanthellina.com/2008/09/23/blockworlds-periodic-table/" title="Blockworld's Periodic Table">Blockworld's Periodic Table</a></li><li><a href="http://blog.prashanthellina.com/2008/07/27/determining-the-difficulty-of-arithmetic-operations/" title="Determining the difficulty of Arithmetic Operations">Determining the difficulty of Arithmetic Operations</a></li><li><a href="http://blog.prashanthellina.com/2008/07/11/even-a-python-can-be-abused/" title="Even a python can be abused">Even a python can be abused</a></li><li><a href="http://blog.prashanthellina.com/2008/05/22/nose-tdd-python/" title="Nose - TDD - Python">Nose - TDD - Python</a></li><li><a href="http://blog.prashanthellina.com/2008/05/13/project-gutenberg-ngram-data-english-only/" title="Project Gutenberg Ngram data: English only">Project Gutenberg Ngram data: English only</a></li><li><a href="http://blog.prashanthellina.com/2008/04/22/alexa-rank-a-script-to-get-the-rank-for-any-site/" title="Alexa rank: A script to get the rank for any site">Alexa rank: A script to get the rank for any site</a></li><li><a href="http://blog.prashanthellina.com/2008/02/06/all-ncert-text-books/" title="All NCERT text books!">All NCERT text books!</a></li><li><a href="http://blog.prashanthellina.com/2008/01/07/interfacing-python-with-c-using-ctypes/" title="Interfacing Python with C using ctypes">Interfacing Python with C using ctypes</a></li><li><a href="http://blog.prashanthellina.com/2007/12/21/topic-extraction-using-wikipedia-data/" title="Topic extraction using Wikipedia data">Topic extraction using Wikipedia data</a></li><li><a href="http://blog.prashanthellina.com/2007/11/14/generating-call-graphs-for-understanding-and-refactoring-python-code/" title="Generating call graphs for understanding and refactoring python code">Generating call graphs for understanding and refactoring python code</a></li><li><a href="http://blog.prashanthellina.com/2007/10/17/ways-to-process-and-use-wikipedia-dumps/" title="Ways to process and use Wikipedia dumps">Ways to process and use Wikipedia dumps</a></li><li><a href="http://blog.prashanthellina.com/2007/08/26/word-arithmetic-puzzle-generator/" title="Word arithmetic puzzle generator">Word arithmetic puzzle generator</a></li><li><a href="http://blog.prashanthellina.com/2007/08/24/drawing-on-your-desktop/" title="Drawing on your Desktop">Drawing on your Desktop</a></li><li><a href="http://blog.prashanthellina.com/2007/08/22/matrix-desktop/" title="Matrix Desktop">Matrix Desktop</a></li></ul>
<p><a href="http://feeds.feedburner.com/~a/prashanthellina?a=MdPhub"><img src="http://feeds.feedburner.com/~a/prashanthellina?i=MdPhub" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/prashanthellina?a=Hk9FjH"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=Hk9FjH" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=YXnH4h"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=YXnH4h" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=F27HXh"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=F27HXh" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=YkyAlh"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=YkyAlh" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=mjjMlH"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=mjjMlH" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/prashanthellina/~4/283384260" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.prashanthellina.com/2008/05/04/n-gram-data-from-project-gutenberg/feed/</wfw:commentRss>
		<feedburner:origLink>http://blog.prashanthellina.com/2008/05/04/n-gram-data-from-project-gutenberg/</feedburner:origLink></item>
		<item>
		<title>Wordza - A Smart Word Quizzer</title>
		<link>http://feeds.feedburner.com/~r/prashanthellina/~3/278697221/</link>
		<comments>http://blog.prashanthellina.com/2008/04/27/wordza-a-smart-word-quizzer/#comments</comments>
		<pubDate>Sun, 27 Apr 2008 08:36:49 +0000</pubDate>
		<dc:creator>prashanthellina</dc:creator>
		
		<category><![CDATA[web]]></category>

		<category><![CDATA[quiz]]></category>

		<category><![CDATA[vocabulary]]></category>

		<category><![CDATA[word]]></category>

		<category><![CDATA[wordza]]></category>

		<guid isPermaLink="false">http://blog.prashanthellina.com/?p=62</guid>
		<description><![CDATA[Related PostI&#8217;d thought of making a word quizzer as a web application to improve my vocabulary when I took the GRE test a couple of years back. I&#8217;d written one in Visual Basic 6 when I wrote SAT :), but desktop applications are boring!
I got inspired to bring my long standing idea to fruition and the [...]]]></description>
			<content:encoded><![CDATA[Related Post<p>I&#8217;d thought of making a word quizzer as a web application to improve my vocabulary when I took the GRE test a couple of years back. I&#8217;d written one in Visual Basic 6 when I wrote SAT :), but desktop applications are boring!</p>
<p>I got inspired to bring my long standing idea to fruition and the outcome is <a href="http://www.wordza.com" onclick="javascript:urchinTracker ('/outbound/article/www.wordza.com');">Wordza</a>.</p>
<p><a href="http://www.wordza.com" onclick="javascript:urchinTracker ('/outbound/article/www.wordza.com');"><img src="http://www.prashanthellina.com/images/wordza_logo.gif" alt="wordza"/></a></p>
<blockquote><p>
Wordza is a smart word quizzing service designed to help you rapidly learn new words. It adapts to your level and continually challenges you to get better. Preparing for GRE/SAT/CAT? Use Wordza 15 minutes a day.</p>
<p>Questions have difficulty ratings ranging between 1 and 100. The ratings are derived from user activity. A question is easy if a lot of people get it right and difficult when very few do!</p>
<p>You start at difficulty level 50. If you get three questions right in a row, you drive up the difficulty level a notch. On the other hand, answering a question wrong will reduce the difficulty level by one.
</p></blockquote>
<p><img src="http://www.prashanthellina.com/images/wordza_screenshot.gif" alt="wordza screenshot"/></p>
<p>If you are interested in the implementation &#8230; I used Python, MySQL behind Apache to get this up. When I started making it, I wanted to keep it simple and make it work fast. I hope I&#8217;ve met both these requirements. Do give it a try and let me know what you think.</p>
<h3></h3><ul class="related_post"><li><a href="http://blog.prashanthellina.com/2008/03/27/songza-music-search-engine-and-jukebox/" title="Songza - music search engine and jukebox">Songza - music search engine and jukebox</a></li><li><a href="http://blog.prashanthellina.com/2007/12/21/topic-extraction-using-wikipedia-data/" title="Topic extraction using Wikipedia data">Topic extraction using Wikipedia data</a></li><li><a href="http://blog.prashanthellina.com/2007/12/15/googles-knol-a-new-wikipedia/" title="Google's Knol - A new Wikipedia?">Google's Knol - A new Wikipedia?</a></li><li><a href="http://blog.prashanthellina.com/2007/10/13/dreamhost-my-wonderful-web-host/" title="DreamHost: My wonderful web host">DreamHost: My wonderful web host</a></li></ul>
<p><a href="http://feeds.feedburner.com/~a/prashanthellina?a=KpBwOj"><img src="http://feeds.feedburner.com/~a/prashanthellina?i=KpBwOj" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/prashanthellina?a=czow8G"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=czow8G" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=XqxJtg"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=XqxJtg" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=3pr1qg"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=3pr1qg" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=Sy1D0g"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=Sy1D0g" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/prashanthellina?a=qoMxhG"><img src="http://feeds.feedburner.com/~f/prashanthellina?i=qoMxhG" border="0"></img></a>
</div><img src="http://feeds.feedbu