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

<channel>
	<title>Derek Illchuk</title>
	<atom:link href="http://www.derekillchuk.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.derekillchuk.com</link>
	<description>Software and Personal Finance</description>
	<lastBuildDate>Wed, 09 Jun 2010 01:24:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Silly Logic Games: Part 1</title>
		<link>http://www.derekillchuk.com/2010/06/silly-logic-games-part-1/</link>
		<comments>http://www.derekillchuk.com/2010/06/silly-logic-games-part-1/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 00:57:15 +0000</pubDate>
		<dc:creator>derek.illchuk</dc:creator>
				<category><![CDATA[Thinking]]></category>

		<guid isPermaLink="false">http://www.derekillchuk.com/?p=54</guid>
		<description><![CDATA[Derek: I flip two coins, hidden from your view, and call out, &#8220;one is heads&#8221;.  Can you tell me the odds that both coins match?
Dr. Logic: Yes!  1 in 3!  You see, the possibilities are as follows: HH, HT, TH, TT.  But, you said heads, so the last is excluded.  Hence, the possibilities are HH, [...]]]></description>
			<content:encoded><![CDATA[<p><em>Derek:</em> I flip two coins, hidden from your view, and call out, &#8220;one is heads&#8221;.  Can you tell me the odds that both coins match?</p>
<p><em>Dr. Logic:</em> Yes!  1 in 3!  You see, the possibilities are as follows: HH, HT, TH, <em>TT</em>.  But, you said <em>heads</em>, so the last is excluded.  Hence, the possibilities are HH, HT, TH, one matching and two not.  Voila!</p>
<p><em>Derek:</em> Uh, the odds are 1 in 2. &lt;whisper whisper whisper to Dr. Logic&gt;</p>
<p><em>Dr. Logic:</em> Oh my!  You&#8217;re right!</p>
<div>&nbsp;</div>
<p><strong>So, what&#8217;s going on?</strong></p>
<p>When I said, &#8220;one is heads&#8221;, Dr. Logic did not consider <em>how I chose what to say</em>.  And how&#8217;s that?  I simply looked at the left-most coin and called out its state, heads or tails, as in &#8220;one is heads&#8221; or &#8220;one is tails&#8221;.  Here&#8217;s how that plays out:</p>
<p>&#8220;One is heads&#8221; means HH, HT and yields a match 1 in 2</p>
<p>&#8220;One is tails&#8221; means TH, TT yields a match 1 in 2</p>
<p>So, when I asked, &#8220;Can you tell me the odds that both coins match?&#8221;, Dr. Logic should have said, &#8220;Not yet, I still have a question.&#8221;</p>
<div>&nbsp;</div>
<p><strong>Code test</strong></p>
<p>I like to see probability in action, so I&#8217;ve coded this to run a million times.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$heads</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tails</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$counts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
  <span style="color: #0000ff;">'heads'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'tails'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'one_is_heads'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'match'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'no_match'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">'one_is_tails'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'match'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'no_match'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">0</span>
  <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1000000</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$coin1</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span><span style="color:#800080;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>? <span style="color: #000088;">$heads</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$tails</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$coin1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$heads</span> <span style="color: #009900;">&#41;</span> <span style="color: #000088;">$counts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'heads'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$coin1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$tails</span> <span style="color: #009900;">&#41;</span> <span style="color: #000088;">$counts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tails'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
&nbsp;
  <span style="color: #000088;">$coin2</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">%</span><span style="color:#800080;">2</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>? <span style="color: #000088;">$heads</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$tails</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$coin2</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$heads</span> <span style="color: #009900;">&#41;</span> <span style="color: #000088;">$counts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'heads'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$coin2</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$tails</span> <span style="color: #009900;">&#41;</span> <span style="color: #000088;">$counts</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tails'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
&nbsp;
  <span style="color: #000088;">$count_ix</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$coin1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$heads</span> <span style="color: #009900;">&#41;</span>? <span style="color: #0000ff;">'one_is_heads'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'one_is_tails'</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$match_ix</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$coin1</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$coin2</span> <span style="color: #009900;">&#41;</span>? <span style="color: #0000ff;">'match'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'no_match'</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$counts</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$count_ix</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$match_ix</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$counts</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<div>&nbsp;</div>
<p><strong>Result:</strong><br />
Everything is 1 in 2.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Array
(
    [heads] =&gt; 1000658
    [tails] =&gt; 999342
    [one_is_heads] =&gt; Array
        (
            [match] =&gt; 250567
            [no_match] =&gt; 249449
        )
&nbsp;
    [one_is_tails] =&gt; Array
        (
            [match] =&gt; 249909
            [no_match] =&gt; 250075
        )
&nbsp;
)</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.derekillchuk.com/2010/06/silly-logic-games-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wealth</title>
		<link>http://www.derekillchuk.com/2010/06/wealth/</link>
		<comments>http://www.derekillchuk.com/2010/06/wealth/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 23:45:09 +0000</pubDate>
		<dc:creator>derek.illchuk</dc:creator>
				<category><![CDATA[Finance]]></category>

		<guid isPermaLink="false">http://www.derekillchuk.com/?p=50</guid>
		<description><![CDATA[You say, what good is wealth if you don&#8217;t spend it?  I say, what good is wealth if you do?
]]></description>
			<content:encoded><![CDATA[<p>You say, what good is wealth if you don&#8217;t spend it?  I say, what good is wealth if you <em>do</em>?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derekillchuk.com/2010/06/wealth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Elbert Hubbard</title>
		<link>http://www.derekillchuk.com/2010/06/elbert-hubbard/</link>
		<comments>http://www.derekillchuk.com/2010/06/elbert-hubbard/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 20:43:54 +0000</pubDate>
		<dc:creator>derek.illchuk</dc:creator>
				<category><![CDATA[Notable]]></category>

		<guid isPermaLink="false">http://www.derekillchuk.com/?p=46</guid>
		<description><![CDATA[&#8220;To avoid criticism do nothing, say nothing, be nothing.&#8221;
]]></description>
			<content:encoded><![CDATA[<p>&#8220;To avoid criticism do nothing, say nothing, be nothing.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derekillchuk.com/2010/06/elbert-hubbard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Where to defrost your food</title>
		<link>http://www.derekillchuk.com/2010/06/where-to-defrost-your-food/</link>
		<comments>http://www.derekillchuk.com/2010/06/where-to-defrost-your-food/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 20:40:08 +0000</pubDate>
		<dc:creator>derek.illchuk</dc:creator>
				<category><![CDATA[Thinking]]></category>

		<guid isPermaLink="false">http://www.derekillchuk.com/?p=38</guid>
		<description><![CDATA[The situation: you have some food to defrost, and you&#8217;d like to place it on a room-temperature surface, one of the following:
a. the table
b. the counter
c. the cutting board
d. a trivet
Which is best?
&#160;
The answer: touch each surface with your hand for ten seconds.  The one that feels the coldest is the winner.  Yes, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>The situation</strong>: you have some food to defrost, and you&#8217;d like to place it on a room-temperature surface, one of the following:</p>
<p>a. the table</p>
<p>b. the counter</p>
<p>c. the cutting board</p>
<p>d. a trivet</p>
<p>Which is best?</p>
<div>&nbsp;</div>
<p><strong>The answer</strong>: touch each surface with your hand for ten seconds.  The one that feels the <em>coldest </em>is the winner.  Yes, the <em>coldest</em>.  Can you explain it?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derekillchuk.com/2010/06/where-to-defrost-your-food/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Check your pulse, hands-free</title>
		<link>http://www.derekillchuk.com/2010/06/check-your-pulse-hands-free/</link>
		<comments>http://www.derekillchuk.com/2010/06/check-your-pulse-hands-free/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 20:39:00 +0000</pubDate>
		<dc:creator>derek.illchuk</dc:creator>
				<category><![CDATA[Thinking]]></category>

		<guid isPermaLink="false">http://www.derekillchuk.com/?p=36</guid>
		<description><![CDATA[Here&#8217;s how to check your pulse&#8230; hands free!
1. Sit down on a firm chair.
2. Cross your legs.
3. Watch the tip of your big toe, it will bump with every pulse, just ever so slightly.
]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s how to check your pulse&#8230; hands free!</p>
<p>1. Sit down on a firm chair.</p>
<p>2. Cross your legs.</p>
<p>3. Watch the tip of your big toe, it will <em>bump </em>with every pulse, just ever so slightly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derekillchuk.com/2010/06/check-your-pulse-hands-free/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Square Brackets Permutation</title>
		<link>http://www.derekillchuk.com/2010/06/square-brackets-permutation/</link>
		<comments>http://www.derekillchuk.com/2010/06/square-brackets-permutation/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 20:34:05 +0000</pubDate>
		<dc:creator>derek.illchuk</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.derekillchuk.com/?p=32</guid>
		<description><![CDATA[Let&#8217;s look at another SPOJ problem today, called Square Brackets.  The problem is how many proper bracket expressions can be made having a certain length.  For added measure, you are given the placement of a few opening brackets (which is trivial to handle, as you&#8217;ll see).
For our example, we have an expression with [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s look at another SPOJ problem today, called <a href="http://www.spoj.pl/problems/SQRBR/" target="_blank"><span style="color: blue; text-decoration: underline;">Square Brackets</span></a>.  The problem is how many proper bracket expressions can be made having a certain length.  For added measure, you are given the placement of a few opening brackets (which is trivial to handle, as you&#8217;ll see).</p>
<p>For our example, we have an expression with length of six (so, three bracket pairs) and the third place is given as an opening bracket:</p>
<p style="margin-left: 27pt;">.  .  [  .  .  .</p>
<p>So, how many expressions can be made from that above?</p>
<p>My first thought is, uh oh, traverse all permutations, branch at each place in the expression, generate all options and bubble them up to the top.  And yes, if we were required to print all proper expressions, this would be required.  Fortunately, though, we&#8217;re asked only for the proper expression <em>count</em>, and this lets us take a nice shortcut.</p>
<p>What&#8217;s the trick?  Well, before we start, we&#8217;re at bracket depth 0, and when we end, we better be at bracket depth 0 (otherwise, the expression can&#8217;t be considered <em>proper</em>).  So, we simply keep track of how many branches are at a particular depth, and &#8220;branch&#8221; at each place by moving our counts around like tokens.</p>
<p>It&#8217;s easier to show you with a picture for our example:</p>
<p><img src="http://www.derekillchuk.com/wp-content/uploads/2009/01/brackets.png" alt="" /><span style="font-family: Times New Roman; font-size: 12pt;"></p>
<p></span></p>
<p><strong>Moves at each expression place</p>
<p></strong>0: Before we start, we have a count of 1 at depth 0.</p>
<p>1: Open/close, can only open though (proper expression can&#8217;t have negative depth), count 1 at depth 1.</p>
<p>2: Open/close, branch, depth 1 goes to both depth 0 (i.e. close) and 2 (i.e. open).</p>
<p>3. Open only, depth 0 goes to depth 1, depth 2 goes to depth 3</p>
<p>4. Open/close, depth 1 branches to 0 and 2, depth 3 branches to 2 but 4 isn&#8217;t valid (since the expression can never close at that depth)</p>
<p>5. Open/close, depth 0 to depth 1, depth 2 to depth 1 and 3.</p>
<p>6. Open/close, depth 1 to depth 0, etc.</p>
<p>After the expression is processed, our answer is in the final place of the 0-depth column.  Our answer for this example is three.</p>
<p><strong>General Algorithm</strong></p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">  expression = array[1..num_pairs*2], each item is array[-1,1] // open/close
  each open expression item is array [1] // open only
&nbsp;
  depth_counts = array[0..num_pairs]
  depth_counts[0] = 1 // to start
&nbsp;
   for each expression item as depth_changes // loop: O(num_pairs)
&nbsp;
    depth_counts_past = depth_counts
    depth_counts = array[0..num_pairs]
    for each depth_counts_past index as count // loop: O(num_pairs)
&nbsp;
      for each depth_changes as depth_change // loop: O(2)
        next_count = count + depth_change
        if next_count &gt;= 0 and next_count &lt;= num_pairs
          depth_counts[next_count] += depth_counts_past[count]
        end if
      end for each
    end for each
  end for each
&nbsp;
  result = depth_counts[0]</pre></div></div>

<p>And there we are.  What is inherently a permutation problem has been collapsed into complexity O(N²).  Calculating the answer for 500 bracket pairs with ruby takes under one second on my system, and the result is a number having 300 places.  Not bad!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derekillchuk.com/2010/06/square-brackets-permutation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Towers of Hanoi Puzzle: An Iterative Solution</title>
		<link>http://www.derekillchuk.com/2010/06/the-towers-of-hanoi-puzzle-an-iterative-solution/</link>
		<comments>http://www.derekillchuk.com/2010/06/the-towers-of-hanoi-puzzle-an-iterative-solution/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 20:19:38 +0000</pubDate>
		<dc:creator>derek.illchuk</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.derekillchuk.com/?p=21</guid>
		<description><![CDATA[Reading through the New Turing OmniBus: 66 Excursions in Computer Science, I turned upon the classic Towers of Hanoi Puzzle.  The recursive solution, as many know, is mind-blowing in it&#8217;s simplicity; the first time I saw it, I remember saying, &#8220;OK&#8230; and then what?&#8221;

function hanoi( n, from, dest, aux )
{
  if ( n [...]]]></description>
			<content:encoded><![CDATA[<p>Reading through <a href="http://www.amazon.com/New-Turing-Omnibus-Sixty-Six-Excursions/dp/0805071660" target="_blank">the New Turing OmniBus: 66 Excursions in Computer Science</a>, I turned upon the classic <a href="http://en.wikipedia.org/wiki/Tower_of_Hanoi">Towers of Hanoi Puzzle</a>.  The <em>recursive</em> solution, as many know, is mind-blowing in it&#8217;s simplicity; the first time I saw it, I remember saying, &#8220;OK&#8230; and then what?&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">function hanoi( n, from, dest, aux )
{
  if ( n == 1 )
    printline 'Move the plate from ' + from + ' to ' + dest
  else
    hanoi( n-1, from, aux, dest )
    hanoi( 1, from, dest, aux )
    hanoi( n-1, aux , dest, from )
  endif
end</pre></div></div>

<p><br style="line-height: 20px;" /><br />
<a style="float: right;" href="http://www.derekillchuk.com/wp-content/uploads/2009/01/hanoi.png"><img class="alignnone size-full wp-image-201" title="hanoi" src="http://www.derekillchuk.com/wp-content/uploads/2009/01/hanoi.png" alt="hanoi" width="310" height="297" /></a></p>
<p>The book goes on to present an iterative solution, as follows:</p>
<p>A. Always move the smallest disc first in a &#8220;clockwise&#8221; manner.</p>
<p>B. Then, move the smaller remaining disc onto the the other (in fact, that&#8217;s the only option).</p>
<p>C. Repeat A &amp; B until finished.</p>
<p><br style="clear: both;" /><br />
<br style="line-height: 20px;" /></p>
<p>Here&#8217;s a straightforward implementation (<a href="http://www.derekillchuk.com/wp-content/uploads/2009/01/hanoi.cpp">download</a>).</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;vector&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">unsigned</span> <span style="color: #0000ff;">int</span> uint32<span style="color: #008080;">;</span>
<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">int</span> int32<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">typedef</span> uint32 Stack<span style="color: #008080;">;</span>
<span style="color: #0000ff;">typedef</span> uint32 Disc<span style="color: #008080;">;</span>
&nbsp;
Stack next_stack<span style="color: #008000;">&#40;</span> Stack curr_stack <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000ff;">return</span> <span style="color: #008000;">&#40;</span> curr_stack <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
int32 main<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">void</span> <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Calculating tower of hanoi moves&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter the number of discs: &quot;</span><span style="color: #008080;">;</span>
  uint32 num_discs <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> num_discs<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #ff0000; font-style: italic;">/**
   * Set up the stacks, with discs loaded onto the first one.
   */</span>
  vector<span style="color: #000080;">&lt;</span>vector<span style="color: #000080;">&gt;</span> stacks<span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> Stack stack_id <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> stack_id <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">;</span> stack_id<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    stacks.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span>
      vector<span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">1</span>, num_discs<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span> <span style="color: #666666;">// sentinel to avoid empty-stack logic</span>
    <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  Stack curr_stack <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> Disc disc <span style="color: #000080;">=</span> num_discs<span style="color: #008080;">;</span> disc <span style="color: #000080;">&gt;</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> disc<span style="color: #000040;">--</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    stacks<span style="color: #008000;">&#91;</span>curr_stack<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span> disc <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  uint32 num_moves <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">true</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
    <span style="color: #ff0000; font-style: italic;">/**
     * Smallest disc always moved one over.
     */</span>
    Disc disc1 <span style="color: #000080;">=</span> stacks<span style="color: #008000;">&#91;</span>curr_stack<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">back</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    stacks<span style="color: #008000;">&#91;</span>curr_stack<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">pop_back</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    curr_stack <span style="color: #000080;">=</span> next_stack<span style="color: #008000;">&#40;</span> curr_stack <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    stacks<span style="color: #008000;">&#91;</span>curr_stack<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span> disc1 <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    num_moves<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #ff0000; font-style: italic;">/**
     * Remaining move is forced.
     */</span>
    Stack from_stack <span style="color: #000080;">=</span> next_stack<span style="color: #008000;">&#40;</span> curr_stack <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    Stack to_stack <span style="color: #000080;">=</span> next_stack<span style="color: #008000;">&#40;</span> from_stack <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> stacks<span style="color: #008000;">&#91;</span>from_stack<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">back</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&gt;</span> stacks<span style="color: #008000;">&#91;</span>to_stack<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">back</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      Stack temp_stack <span style="color: #000080;">=</span> from_stack<span style="color: #008080;">;</span>
      from_stack <span style="color: #000080;">=</span> to_stack<span style="color: #008080;">;</span>
      to_stack <span style="color: #000080;">=</span> temp_stack<span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    Disc disc2 <span style="color: #000080;">=</span> stacks<span style="color: #008000;">&#91;</span>from_stack<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">back</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    stacks<span style="color: #008000;">&#91;</span>from_stack<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">pop_back</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> stacks<span style="color: #008000;">&#91;</span>from_stack<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">empty</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// we're done</span>
      <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    stacks<span style="color: #008000;">&#91;</span>to_stack<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span> disc2 <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    num_moves<span style="color: #000040;">++</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Total moves:&quot;</span> <span style="color: #000080;">&lt;&lt;</span> num_moves <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br style="line-height: 20px;" /><br />
Is this particular iterative solution better than the recursive one?  Aside from being a nice little exercise, no, it&#8217;s not that impressive.  It takes longer to run than the recursive solution and requires more code.  But, I think it&#8217;s easy to <em>get</em> how it works: <em>&#8220;Move smallest clockwise then move what you can, repeat until we&#8217;re done.&#8221;</em>.</p>
<p>There are iterative solutions out there that are much quicker, but they start to look like <a href="http://www.geo.uni-bonn.de/members/hattendorf/html/misc/hanoi/iterative_solutions/welcome.html" target="_blank">this</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> hanoi<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> fr<span style="color: #339933;">,</span> to<span style="color: #339933;">;</span>
  <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> i<span style="color: #339933;">,</span> j<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>x<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> <span style="color: #009900;">&#40;</span><span style="color: #0000dd;">1</span> <span style="color: #339933;">&lt;&lt;</span> N<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    i<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span>x<span style="color: #339933;">|</span>x<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> to<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">+</span>i<span style="color: #339933;">/</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span><span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
    i<span style="color: #339933;">=</span>x<span style="color: #339933;">&amp;</span>x<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> fr<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">+</span>i<span style="color: #339933;">/</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span><span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span>x<span style="color: #339933;">,</span> j<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">;</span> i<span style="color: #339933;">&gt;&gt;=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">&amp;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.derekillchuk.com/2010/06/the-towers-of-hanoi-puzzle-an-iterative-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding the Longest Common Subsequence</title>
		<link>http://www.derekillchuk.com/2010/06/finding-the-longest-common-subsequence/</link>
		<comments>http://www.derekillchuk.com/2010/06/finding-the-longest-common-subsequence/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 20:09:51 +0000</pubDate>
		<dc:creator>derek.illchuk</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.derekillchuk.com/?p=14</guid>
		<description><![CDATA[As I looked into my next SPOJ problem, Cross-country, I realized this was clearly the Longest Common Subsequence problem I encountered during my university studies.  The solution is well-documented, and I simply need to implement this function, given in Wikipedia as:

function  LCSLength(X[1..m], Y[1..n])
    C = array(0..m, 0..n)
    [...]]]></description>
			<content:encoded><![CDATA[<p>As I looked into my next SPOJ problem, <a href="https://www.spoj.pl/problems/CRSCNTRY/" target="_blank">Cross-country</a>, I realized this was clearly the Longest Common Subsequence problem I encountered during my university studies.  The solution is well-documented, and I simply need to implement this function, given in <a href="http://en.wikipedia.org/wiki/Longest_common_subsequence_problem#Computing_the_length_of_the_LCS" target="_blank">Wikipedia </a>as:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">function  LCSLength(X[1..m], Y[1..n])
    C = array(0..m, 0..n)
    for i := 0..m
       C[i,0] = 0
    for j := 0..n
       C[0,j] = 0
    for i := 1..m
        for j := 1..n
            if X[i-1] = Y[j-1]
                C[i,j] := C[i-1,j-1] + 1
            else:
                C[i,j] := max(C[i,j-1], C[i-1,j])
    return C[m,n]</pre></div></div>

<p><br style="line-height: 20px;" /></p>
<p>This can be turned into C++ rather methodically, with something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">typedef</span> vector<span style="color: #000080;">&lt;</span>uint32<span style="color: #000080;">&gt;</span> Sequence<span style="color: #008080;">;</span>
<span style="color: #0000ff;">typedef</span> vector<span style="color: #000080;">&lt;</span>Sequence<span style="color: #000080;">&gt;</span> Sequences<span style="color: #008080;">;</span>
uint32 LCSLength<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Sequence<span style="color: #000040;">&amp;</span> X, <span style="color: #0000ff;">const</span> Sequence<span style="color: #000040;">&amp;</span> Y <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #ff0000; font-style: italic;">/**
   * Load top-left edges with zeroes.
   *
   * e.g. X length 2, Y length 5
   * 000000
   * 0SSSSS
   * 0SSSSS
   */</span>
  Sequences C<span style="color: #008000;">&#40;</span> X.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">+</span><span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> uint32 Xix <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> Xix <span style="color: #000080;">&lt;</span> C.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> Xix<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    C<span style="color: #008000;">&#91;</span>Xix<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> Sequence<span style="color: #008000;">&#40;</span> Y.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">+</span><span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #ff0000; font-style: italic;">/**
   * Generate the table, giving our result.
   */</span>
  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> uint32 Xix <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> Xix <span style="color: #000080;">&lt;</span> C.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> Xix<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> uint32 Yix <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> Yix <span style="color: #000080;">&lt;</span> C<span style="color: #008000;">&#91;</span>Xix<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> Yix<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> X<span style="color: #008000;">&#91;</span>Xix<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> Y<span style="color: #008000;">&#91;</span>Yix<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        C<span style="color: #008000;">&#91;</span>Xix<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>Yix<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> C<span style="color: #008000;">&#91;</span>Xix<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>Yix<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
      <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
        C<span style="color: #008000;">&#91;</span>Xix<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>Yix<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> max<span style="color: #008000;">&#40;</span> C<span style="color: #008000;">&#91;</span>Xix<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>Yix<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span>, C<span style="color: #008000;">&#91;</span>Xix<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>Yix<span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">return</span> C<span style="color: #008000;">&#91;</span>X.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>Y.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br style="line-height: 20px;" /></p>
<p>An easy optimization pops out: each iteration looks at only the current and previous row of C, yet we&#8217;re generating a whole m x n table.  Reducing C to only two rows, and flip-flopping between them yields the following:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">uint32 LCSLength<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> Sequence<span style="color: #000040;">&amp;</span> X, <span style="color: #0000ff;">const</span> Sequence<span style="color: #000040;">&amp;</span> Y <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #ff0000; font-style: italic;">/**
   *  Speed &amp; memory optimization: only need the last two rows.
   */</span>
  Sequences C<span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">2</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  C<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> Sequence<span style="color: #008000;">&#40;</span> Y.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">+</span><span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  C<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> Sequence<span style="color: #008000;">&#40;</span> Y.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">+</span><span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  uint32 CprevIx <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
  uint32 CcurrIx <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
&nbsp;
  <span style="color: #ff0000; font-style: italic;">/**
   * Generate the table, giving our result.
   */</span>
  <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> uint32 Xix <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> Xix <span style="color: #000080;">&lt;=</span> X.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> Xix<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span> uint32 Yix <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> Yix <span style="color: #000080;">&lt;=</span> Y.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> Yix<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span> X<span style="color: #008000;">&#91;</span>Xix<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">==</span> Y<span style="color: #008000;">&#91;</span>Yix<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        C<span style="color: #008000;">&#91;</span>CcurrIx<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>Yix<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> C<span style="color: #008000;">&#91;</span>CprevIx<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>Yix<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #000040;">+</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
      <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
        C<span style="color: #008000;">&#91;</span>CcurrIx<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>Yix<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> max<span style="color: #008000;">&#40;</span> C<span style="color: #008000;">&#91;</span>CcurrIx<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>Yix<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span>, C<span style="color: #008000;">&#91;</span>CprevIx<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>Yix<span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    uint32 CtempIx <span style="color: #000080;">=</span> CprevIx<span style="color: #008080;">;</span>
    CprevIx <span style="color: #000080;">=</span> CcurrIx<span style="color: #008080;">;</span>
    CcurrIx <span style="color: #000080;">=</span> CtempIx<span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0000ff;">return</span> C<span style="color: #008000;">&#91;</span>CprevIx<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>Y.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br style="line-height: 20px;" /></p>
<p>This strategy reduces the algorithm run-time about 40% when testing with inputs generated by my <a href="http://www.derekillchuk.com/wp-content/uploads/2009/01/generate.rb">ruby input generator</a>.  Yes, it gives the algorithm a good case of tunnel vision, focusing on finding only the <em>length</em> of the longest common subsequence while giving up the ability to actually determine what that subsequence is.  But, it certainly does what it&#8217;s called to do as well as it can.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derekillchuk.com/2010/06/finding-the-longest-common-subsequence/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Venerable FizzBuzz Problem</title>
		<link>http://www.derekillchuk.com/2010/06/the-venerable-fizzbuzz-problem/</link>
		<comments>http://www.derekillchuk.com/2010/06/the-venerable-fizzbuzz-problem/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 19:59:04 +0000</pubDate>
		<dc:creator>derek.illchuk</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.derekillchuk.com/?p=10</guid>
		<description><![CDATA[Over at Imran on Tech, they presented a problem which they say stumps many comp sci. graduates (although, I have yet to be convinced).
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers [...]]]></description>
			<content:encoded><![CDATA[<p>Over at <a href="http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/" target="_blank">Imran on Tech</a>, they presented a problem which they say stumps many comp sci. graduates (although, I have yet to be convinced).</p>
<blockquote><p>Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.</p></blockquote>
<p><br/></p>
<p>Here&#8217;s a table-driven solution in ruby, turning if-else logic into pure data.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># fizzbuzz[is mod 3][is mod 5]</span>
fizzbuzz = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
fizzbuzz<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#93;</span>  = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#0000FF; font-weight:bold;">true</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'FizzBuzz'</span>, <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Fizz'</span><span style="color:#006600; font-weight:bold;">&#125;</span>
fizzbuzz<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#0000FF; font-weight:bold;">true</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Buzz'</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">for</span> i <span style="color:#9966CC; font-weight:bold;">in</span> 1..100
  fizzbuzz<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#93;</span> = i
  <span style="color:#CC0066; font-weight:bold;">puts</span> fizzbuzz<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">%</span>3==<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">%</span>5==<span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.derekillchuk.com/2010/06/the-venerable-fizzbuzz-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Aggressive Cows!</title>
		<link>http://www.derekillchuk.com/2010/06/aggressive-cows/</link>
		<comments>http://www.derekillchuk.com/2010/06/aggressive-cows/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 19:57:25 +0000</pubDate>
		<dc:creator>derek.illchuk</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">http://www.derekillchuk.com/?p=4</guid>
		<description><![CDATA[I found an interesting little problem called &#8216;Aggressive Cows&#8216;, and I really like the solution.
Imagine a row of stalls, stretching out far and wide.  These stalls are spaced a little strangely, they&#8217;re certainly not an equal distance apart.  Now, we have a certain number of cows to place, each going into their own [...]]]></description>
			<content:encoded><![CDATA[<p>I found an interesting little problem called &#8216;<a href="https://www.spoj.pl/problems/AGGRCOW/" target="_blank">Aggressive Cows</a>&#8216;, and I really like the solution.</p>
<p>Imagine a row of stalls, stretching out far and wide.  These stalls are spaced a little strangely, they&#8217;re certainly not an equal distance apart.  Now, we have a certain number of cows to place, each going into their own stall.  What&#8217;s the most spaced out these cows can be?  Or, in more succinct terms, what&#8217;s the maximum minimum distance?</p>
<p><a href="http://www.derekillchuk.com/wp-content/uploads/2010/06/stalls-problem2.png"><img class="alignnone size-full wp-image-5" title="stalls-problem2" src="http://www.derekillchuk.com/wp-content/uploads/2010/06/stalls-problem2.png" alt="" width="610" height="161" /></a></p>
<p><a href="http://www.derekillchuk.com/wp-content/uploads/2010/06/stalls-solution.png"><img class="alignnone size-full wp-image-6" title="stalls-solution" src="http://www.derekillchuk.com/wp-content/uploads/2010/06/stalls-solution.png" alt="" width="499" height="147" /></a></p>
<p><a href="http://www.derekillchuk.com/wp-content/uploads/2010/06/stalls-solution-5-cows1.png"><img class="alignnone size-full wp-image-7" title="stalls-solution-5-cows1" src="http://www.derekillchuk.com/wp-content/uploads/2010/06/stalls-solution-5-cows1.png" alt="" width="495" height="126" /></a></p>
<p><br/></p>
<p>What&#8217;s the solution?  It turns out, it&#8217;s best just to guess!</p>
<ol style="margin-bottom: 20px;">
<li>From what you know, determine the minimum and maximum possible guess &#8212; cast a wide net here.</li>
<li>Your actual guess is halfway between your min and max guess.</li>
<li>See if your actual guess works.  This is a straightforward O(N) operation: walk through the stalls, placing cows as you can.</li>
<li>If it works (all cows were placed), your minimum guess becomes this actual guess.</li>
<li>If it doesn&#8217;t work, your maximum guess becomes one less than the actual guess.</li>
<li>Keep going until you converge onto the highest guess that has been proven to work.  The search is binary and makes O(log N) guesses.</li>
<li>Total running time: O(N log N)</li>
</ol>
<p>This solutions depends on two qualities of the problem:</p>
<ol style="margin-bottom: 20px;">
<li>The solution space is sorted and limited.</li>
<li>Each guess is relatively inexpensive to check.</li>
</ol>
<p>With those in place, your tricky problem becomes no match for the tried-and-true binary search.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.derekillchuk.com/2010/06/aggressive-cows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

