<?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>davinci’s notebook &#187; C++</title>
	<atom:link href="http://stargrads.net/blogs/davinci/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://stargrads.net/blogs/davinci</link>
	<description>everything is an experiment</description>
	<lastBuildDate>Tue, 07 Sep 2010 02:51:13 +0000</lastBuildDate>
	<language>fa</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Programming exercise: interweaved strings</title>
		<link>http://stargrads.net/blogs/davinci/2009/11/programming-exercise-interweaved-strings/</link>
		<comments>http://stargrads.net/blogs/davinci/2009/11/programming-exercise-interweaved-strings/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 01:45:33 +0000</pubDate>
		<dc:creator>davinci</dc:creator>
				<category><![CDATA[programming and technical issues]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[dynamic programming]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming exercises]]></category>

		<guid isPermaLink="false">http://stargrads.net/blogs/davinci/?p=2574</guid>
		<description><![CDATA[
Another programming exercise.  The problem is to determine whether one string can be formed by interweaving two others.
]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t quite remember where I saw this problem, but I&#8217;m sure it&#8217;s appeared in a number of places.  Given two strings \(s\) and \(t\), determine whether a string \(u\) is formed by interweaving \(s\) and \(t\).  That is, determine whether \(u\) can be formed by taking the first few characters of (say) \(s\), followed by the first few characters of \(t\), then the next few characters of \(s\), and so on.  For example, the string &#8220;abccdcxey&#8221; can be formed by interweaving &#8220;abcde&#8221; with &#8220;ccxy&#8221;<span id="more-2574"></span>.<!--adsensestart--></p>
<p>I took way too long thinking about how to set up the problem than I should have.  In the end, I decided to quickly write up a solution that is easy to code, even if it may not be optimal.  The solution uses an \((m+1) \times (n+1)\) table, with indices starting from \(0\), where the \((i,j)\) entry is a boolean value indicating whether it&#8217;s possible to form the \((i+j)\)-character prefix of \(u\) from the first \(i\) characters of \(s\) and the first \(j\) characters of \(t\).<!--adsensestop--></p>
<p>Let \(m\) and \(n\) be the lengths of \(s\) and \(t\), respectively.  Assume that \(u\) is of length \(m+n\).  (This can be easily checked and handled at the beginning.) The function that determines whether \(u\) is formed by interweaving \(s\) and \(t\) is as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">bool interweave<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>s<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> m<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>t<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>u<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    bool <span style="color: #339933;">**</span>table <span style="color: #339933;">=</span> new_table<span style="color: #009900;">&#40;</span>m<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> n<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
    table<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> row <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> row <span style="color: #339933;">&lt;</span> m<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> row<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> col <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> col <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> col<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> table<span style="color: #009900;">&#91;</span>row<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>col<span style="color: #009900;">&#93;</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> row <span style="color: #339933;">!=</span> m <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    table<span style="color: #009900;">&#91;</span>row<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>col<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#91;</span>row<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> u<span style="color: #009900;">&#91;</span>row<span style="color: #339933;">+</span>col<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> col <span style="color: #339933;">!=</span> n <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    table<span style="color: #009900;">&#91;</span>row<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>col<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#91;</span>col<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> u<span style="color: #009900;">&#91;</span>row<span style="color: #339933;">+</span>col<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    bool result <span style="color: #339933;">=</span> table<span style="color: #009900;">&#91;</span>m<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    delete table<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>When programming on the whiteboard, it&#8217;s important not to get bogged down in details.  In the above, I&#8217;ve assumed the existence of a function <code>new_table(rows,cols)</code> which returns a two-dimensional array of <code>bool</code>s, of the requested number of rows and columns, with all entries initialised to <code>false</code>.  This is simple enough to implement that I hope the interviewer won&#8217;t actually ask me to write it out during the interview, but in case I have to do so, here it is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">bool <span style="color: #339933;">**</span>new_table<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> rows<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> cols<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    bool <span style="color: #339933;">**</span>table <span style="color: #339933;">=</span> new bool<span style="color: #339933;">*</span><span style="color: #009900;">&#91;</span>rows<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> rows<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        table<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> new bool<span style="color: #009900;">&#91;</span>cols<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #993333;">int</span> j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> cols<span style="color: #339933;">;</span> j<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            table<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> table<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The algorithm runs in time \(O(m \times n)\) and also takes that much space.  I wasn&#8217;t really satisfied with this solution because it doesn&#8217;t make use of the fact that the same prefix of \(u\) can be formed by interweaving prefixes of \(s\) and \(t\) in different ways.  For example, &#8220;abcc&#8221; can be formed by interweaving &#8220;abc&#8221; and &#8220;c&#8221; in two different ways.  Now, the above algorithm doesn&#8217;t perform redundant calculations, but only because we&#8217;re sweeping the table row-by-row in an orderly fashion.  </p>
<p>One can, of course, contrive values for which most of the table actually needs to be visited, but for most values of \(s\) and \(t\), if one or more paths from the upper left corner to the lower right corner of the table exists, the only entries of the table that need to be visited are likely to be very close to these paths.  It makes sense therefore to keep track of which entries in the table are actually necessary to visit.  </p>
<p>The following code is closer to the algorithm I have in mind:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="c" style="font-family:monospace;">bool interweave<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>s<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> m<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>t<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>u<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    queue q <span style="color: #339933;">=</span> new queue<span style="color: #009900;">&#40;</span>m<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>n<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    q.<span style="color: #202020;">enqueue</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #339933;">,</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    bool result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span>q.<span style="color: #202020;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #993333;">int</span> row<span style="color: #339933;">,</span> col<span style="color: #339933;">;</span>
        q.<span style="color: #202020;">dequeue</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>row<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>col<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span> row <span style="color: #339933;">==</span> m <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span> col <span style="color: #339933;">==</span> n <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span> row <span style="color: #339933;">!=</span> m <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span> s<span style="color: #009900;">&#91;</span>row<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> u<span style="color: #009900;">&#91;</span>row<span style="color: #339933;">+</span>col<span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>q.<span style="color: #202020;">marked</span><span style="color: #009900;">&#40;</span>row<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>col<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            q.<span style="color: #202020;">enqueue</span><span style="color: #009900;">&#40;</span>row<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>col<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span> col <span style="color: #339933;">!=</span> n <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span>t<span style="color: #009900;">&#91;</span>col<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> u<span style="color: #009900;">&#91;</span>row<span style="color: #339933;">+</span>col<span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>q.<span style="color: #202020;">marked</span><span style="color: #009900;">&#40;</span>row<span style="color: #339933;">,</span>col<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            q.<span style="color: #202020;">enqueue</span><span style="color: #009900;">&#40;</span>row<span style="color: #339933;">,</span>col<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: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    delete queue<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In the above, I&#8217;ve assumed the existence of a <code>queue</code> class that allows me to enqueue and dequeue pairs of integers consisting of a row and a column.  A row-column pair is enqueued only if it corresponds to a prefix of \(u\) that can be formed by interweaving a prefix of \(s\) and a prefix of \(t\).  Furthermore, the <code>queue</code> class also keeps track of whether a pair has <em>ever</em> been enqueued.  The <code>marked</code> function returns true if that&#8217;s the case, and false otherwise.  It can do this either using a two-dimensional array, as before, or it can use a fancier structure that trades off time for savings in space.  </p>
<p>If a two-dimensional array is used, the algorithm will still require \(O(m \times n)\) time just to initialise it.  However, the main loop in the <code>interweave</code> function will be \(O(m+n)\) for inputs for which there is little overlap or repetition in the input strings.</p>
<p>&#8211; davinci 11848</p>




	<a rel="nofollow"  href="http://stargrads.net/blogs/davinci/feed/" title="RSS"><img src="http://stargrads.net/common/images/handycons/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Programming%20exercise%3A%20interweaved%20strings&amp;body=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-interweaved-strings%2F" title="email"><img src="http://stargrads.net/common/images/handycons/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-interweaved-strings%2F&amp;t=Programming%20exercise%3A%20interweaved%20strings" title="Facebook"><img src="http://stargrads.net/common/images/handycons/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Programming%20exercise%3A%20interweaved%20strings%20-%20http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-interweaved-strings%2F" title="Twitter"><img src="http://stargrads.net/common/images/handycons/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=Programming%20exercise%3A%20interweaved%20strings&amp;link=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-interweaved-strings%2F" title="FriendFeed"><img src="http://stargrads.net/common/images/handycons/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-interweaved-strings%2F&amp;title=Programming%20exercise%3A%20interweaved%20strings&amp;notes=Another%20programming%20exercise.%20%20The%20problem%20is%20to%20determine%20whether%20one%20string%20can%20be%20formed%20by%20interweaving%20two%20others." title="del.icio.us"><img src="http://stargrads.net/common/images/handycons/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-interweaved-strings%2F&amp;title=Programming%20exercise%3A%20interweaved%20strings&amp;bodytext=Another%20programming%20exercise.%20%20The%20problem%20is%20to%20determine%20whether%20one%20string%20can%20be%20formed%20by%20interweaving%20two%20others." title="Digg"><img src="http://stargrads.net/common/images/handycons/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-interweaved-strings%2F&amp;title=Programming%20exercise%3A%20interweaved%20strings&amp;annotation=Another%20programming%20exercise.%20%20The%20problem%20is%20to%20determine%20whether%20one%20string%20can%20be%20formed%20by%20interweaving%20two%20others." title="Google Bookmarks"><img src="http://stargrads.net/common/images/handycons/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://bookmarks.yahoo.com/toolbar/savebm?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-interweaved-strings%2F&amp;t=Programming%20exercise%3A%20interweaved%20strings&opener=bm&amp;ei=UTF-8&amp;d=Another%20programming%20exercise.%20%20The%20problem%20is%20to%20determine%20whether%20one%20string%20can%20be%20formed%20by%20interweaving%20two%20others." title="Yahoo! Bookmarks"><img src="http://stargrads.net/common/images/handycons/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-interweaved-strings%2F&amp;title=Programming%20exercise%3A%20interweaved%20strings" title="StumbleUpon"><img src="http://stargrads.net/common/images/handycons/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-interweaved-strings%2F" title="Technorati"><img src="http://stargrads.net/common/images/handycons/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-interweaved-strings%2F&amp;title=Programming%20exercise%3A%20interweaved%20strings" title="Reddit"><img src="http://stargrads.net/common/images/handycons/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>


<br/><br/><img src="http://stargrads.net/blogs/davinci/?ak_action=api_record_view&id=2574&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://stargrads.net/blogs/davinci/2009/10/programming-exercise-permutations-of-a-string/' rel='bookmark' title='Permanent Link: Programming exercise: permutations of a string'>Programming exercise: permutations of a string</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/10/programming-exercise-combinations-of-a-string/' rel='bookmark' title='Permanent Link: Programming exercise: combinations of a string'>Programming exercise: combinations of a string</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/11/programming-exercise-red-white-blue-sorting/' rel='bookmark' title='Permanent Link: Programming exercise: red-white-blue sorting'>Programming exercise: red-white-blue sorting</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://stargrads.net/blogs/davinci/2009/11/programming-exercise-interweaved-strings/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Programming exercise: red-white-blue sorting</title>
		<link>http://stargrads.net/blogs/davinci/2009/11/programming-exercise-red-white-blue-sorting/</link>
		<comments>http://stargrads.net/blogs/davinci/2009/11/programming-exercise-red-white-blue-sorting/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 20:52:38 +0000</pubDate>
		<dc:creator>davinci</dc:creator>
				<category><![CDATA[programming and technical issues]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming exercises]]></category>
		<category><![CDATA[quicksort]]></category>
		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://stargrads.net/blogs/davinci/?p=2549</guid>
		<description><![CDATA[
I&#8217;ve been practising coding on the whiteboard for job interviews. This is very different than coding in front of a computer which has a keyboard, a monitor, and a nice editing program that allows you to correct your mistakes and type repetitive text very quickly. I&#8217;m trying to keep my programs simple and short, and [...]
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been practising coding on the whiteboard for job interviews.  This is very different than coding in front of a computer which has a keyboard, a monitor, and a nice editing program that allows you to correct your mistakes and type repetitive text very quickly.  I&#8217;m trying to keep my programs simple and short, and writing in a C-like pseudocode.</p>
<p>This exercise comes from Skiena&#8217;s <a href="http://www.amazon.ca/gp/product/1848000693?ie=UTF8&amp;tag=davincisnoteb-20&amp;linkCode=as2&amp;camp=15121&amp;creative=330641&amp;creativeASIN=1848000693"><i>The Algorithm Design Manual</i></a><img src="http://www.assoc-amazon.ca/e/ir?t=davincisnoteb-20&amp;l=as2&amp;o=15&amp;a=1848000693" width="1" height="1" border="0" alt="" style="border:none !important;margin:0px !important" /><sup><a class='footnote' id='note-2549-1' href='#footnote-2549-skiena08'>[1]</a></sup>\(\)<span id="more-2549"></span>.<!--adsensestart--></p>
<p>You are given an array \(A\) of \(n\) elements, each of which is one of <em>red</em>, <em>white</em>, or <em>blue</em>.  The only permitted operations on \(A\) are to examine an item at position \(i\) using <code>Examine(i)</code>, and to swap two items at positions \(i\) and \(j\) using <code>Swap(i,j)</code>.  The problem is to sort the elements as efficiently as possible, such that the elements are in the order <em>red</em>, followed by <em>white</em>, then finally <em>blue</em>.</p>
<p>The trick to coding on a whiteboard is to first draw some simple visual examples, and use them to talk your interviewer (or yourself) through how the algorithm is supposed to work, before writing down any actual code.  It&#8217;s very important that you have the algorithm you want to code before you start coding, because any editing other than very minor changes is basically impossible on a whiteboard.</p>
<p>The first thing to notice is that we can do better than the standard \(O(n\log n)\) sorting algorithms, since the number of different values for the elements is a small constant.  My first thought was to make one pass of the array to count the number of red, white, and blue elements, one more pass to put all the red elements in front, and a final pass to put the white elements before the blue ones.  While this algorithm is linear time, which is asymptotically the best you can do, with a little thought and a little bookkeeping, it&#8217;s possible to do everything in just one pass of the array.<!--adsensestop--></p>
<p>The idea is as follows.  Keep two &#8220;pointers&#8221;, say \(r\) and \(b\), which begin at the front and back respectively, and which keep track of the places where the next red and blue elements should go.  Now sweep the array from front to back.  </p>
<p>If the current position has a blue element, swap it to position \(b\).  Now, we decrement \(b\), but we don&#8217;t advance the position of the sweep yet, because we might have just swapped in another blue element.  This rule ensures that there are no blue elements behind us (i.e., towards the front of the array).  </p>
<p>Dealing with a red element is a <em>bit</em> trickier, since both the \(r\) pointer and the sweep&#8217;s position are advancing.  If we&#8217;re at the same position as the \(r\) pointer, there&#8217;s a contiguous block of red elements behind us, and we can simply increment \(r\) and move on to the next position.  If we&#8217;re ahead of the \(r\) pointer, we can swap the red element to that position, and increment \(r\).  Note that we can only ever swap in a white element, since we&#8217;ve guaranteed that there are no blue elements behind us, and \(r\) is always beyond the continguous block of known red elements at the front.</p>
<p>Finally, if the current position has a white element, we can just skip over it.  If it&#8217;s in the wrong position, it&#8217;ll be dealt with later when we encounter the red element it needs to be swapped with.</p>
<p>The loop terminates when our sweep hits the contiguous block of blue elements at the back of \(A\), i.e., when \(pos &gt; b\).  For simplicity, we assume that \(n \geq 1\) (which we can always check for before we begin).</p>
<p>This is the algorithm I came up with on the whiteboard:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> r<span style="color: #339933;">,</span> b<span style="color: #339933;">,</span> pos<span style="color: #339933;">;</span>
&nbsp;
r <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> b <span style="color: #339933;">=</span> n<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>Examine<span style="color: #009900;">&#40;</span>r<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #ff0000;">'R'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> r<span style="color: #339933;">++;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>Examine<span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #ff0000;">'B'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> b<span style="color: #339933;">--;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
pos <span style="color: #339933;">=</span> r<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span> pos <span style="color: #339933;">&lt;=</span> b <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> val <span style="color: #339933;">=</span> Examine<span style="color: #009900;">&#40;</span>pos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> val <span style="color: #339933;">==</span> <span style="color: #ff0000;">'B'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Swap<span style="color: #009900;">&#40;</span>b<span style="color: #339933;">,</span> pos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        b<span style="color: #339933;">--;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> val <span style="color: #339933;">==</span> <span style="color: #ff0000;">'R'</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> r <span style="color: #339933;">&lt;</span> pos <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Swap<span style="color: #009900;">&#40;</span>r<span style="color: #339933;">,</span> pos<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
            pos<span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
        r<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> val <span style="color: #339933;">==</span> <span style="color: #ff0000;">'W'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        pos<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;ve assumed that <code>Examine(i)</code> returns one of <code>'R'</code>, <code>'W'</code>, or <code>'B'</code>.  The \(pos\) variable keeps track of the position as I sweep the array from front to back.  The two initial loops skip over any red elements at the front and any blue elements at the back of the array (these are not strictly necessary, but make things a bit faster in some cases).  Each element is examined exactly once, for a total run time of \(n\), making this the most efficient algorithm possible.  </p>
<p>&#8211; davinci 11847</p>




	<a rel="nofollow"  href="http://stargrads.net/blogs/davinci/feed/" title="RSS"><img src="http://stargrads.net/common/images/handycons/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Programming%20exercise%3A%20red-white-blue%20sorting&amp;body=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-red-white-blue-sorting%2F" title="email"><img src="http://stargrads.net/common/images/handycons/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-red-white-blue-sorting%2F&amp;t=Programming%20exercise%3A%20red-white-blue%20sorting" title="Facebook"><img src="http://stargrads.net/common/images/handycons/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Programming%20exercise%3A%20red-white-blue%20sorting%20-%20http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-red-white-blue-sorting%2F" title="Twitter"><img src="http://stargrads.net/common/images/handycons/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=Programming%20exercise%3A%20red-white-blue%20sorting&amp;link=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-red-white-blue-sorting%2F" title="FriendFeed"><img src="http://stargrads.net/common/images/handycons/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-red-white-blue-sorting%2F&amp;title=Programming%20exercise%3A%20red-white-blue%20sorting&amp;notes=I%27ve%20been%20practising%20coding%20on%20the%20whiteboard%20for%20job%20interviews.%20%20This%20is%20very%20different%20than%20coding%20in%20front%20of%20a%20computer%20which%20has%20a%20keyboard%2C%20a%20monitor%2C%20and%20a%20nice%20editing%20program%20that%20allows%20you%20to%20correct%20your%20mistakes%20and%20type%20repetitive%20text" title="del.icio.us"><img src="http://stargrads.net/common/images/handycons/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-red-white-blue-sorting%2F&amp;title=Programming%20exercise%3A%20red-white-blue%20sorting&amp;bodytext=I%27ve%20been%20practising%20coding%20on%20the%20whiteboard%20for%20job%20interviews.%20%20This%20is%20very%20different%20than%20coding%20in%20front%20of%20a%20computer%20which%20has%20a%20keyboard%2C%20a%20monitor%2C%20and%20a%20nice%20editing%20program%20that%20allows%20you%20to%20correct%20your%20mistakes%20and%20type%20repetitive%20text" title="Digg"><img src="http://stargrads.net/common/images/handycons/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-red-white-blue-sorting%2F&amp;title=Programming%20exercise%3A%20red-white-blue%20sorting&amp;annotation=I%27ve%20been%20practising%20coding%20on%20the%20whiteboard%20for%20job%20interviews.%20%20This%20is%20very%20different%20than%20coding%20in%20front%20of%20a%20computer%20which%20has%20a%20keyboard%2C%20a%20monitor%2C%20and%20a%20nice%20editing%20program%20that%20allows%20you%20to%20correct%20your%20mistakes%20and%20type%20repetitive%20text" title="Google Bookmarks"><img src="http://stargrads.net/common/images/handycons/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://bookmarks.yahoo.com/toolbar/savebm?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-red-white-blue-sorting%2F&amp;t=Programming%20exercise%3A%20red-white-blue%20sorting&opener=bm&amp;ei=UTF-8&amp;d=I%27ve%20been%20practising%20coding%20on%20the%20whiteboard%20for%20job%20interviews.%20%20This%20is%20very%20different%20than%20coding%20in%20front%20of%20a%20computer%20which%20has%20a%20keyboard%2C%20a%20monitor%2C%20and%20a%20nice%20editing%20program%20that%20allows%20you%20to%20correct%20your%20mistakes%20and%20type%20repetitive%20text" title="Yahoo! Bookmarks"><img src="http://stargrads.net/common/images/handycons/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-red-white-blue-sorting%2F&amp;title=Programming%20exercise%3A%20red-white-blue%20sorting" title="StumbleUpon"><img src="http://stargrads.net/common/images/handycons/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-red-white-blue-sorting%2F" title="Technorati"><img src="http://stargrads.net/common/images/handycons/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F11%2Fprogramming-exercise-red-white-blue-sorting%2F&amp;title=Programming%20exercise%3A%20red-white-blue%20sorting" title="Reddit"><img src="http://stargrads.net/common/images/handycons/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>


<br/><br/><img src="http://stargrads.net/blogs/davinci/?ak_action=api_record_view&id=2549&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://stargrads.net/blogs/davinci/2009/11/programming-exercise-interweaved-strings/' rel='bookmark' title='Permanent Link: Programming exercise: interweaved strings'>Programming exercise: interweaved strings</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/09/programming-exercise-maximum-value-in-integer-array-part-1/' rel='bookmark' title='Permanent Link: Programming exercise: maximum value in integer array, part 1'>Programming exercise: maximum value in integer array, part 1</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/09/programming-exercise-hello-world/' rel='bookmark' title='Permanent Link: Programming exercise: Hello, world!'>Programming exercise: Hello, world!</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://stargrads.net/blogs/davinci/2009/11/programming-exercise-red-white-blue-sorting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming exercise: permutations of a string</title>
		<link>http://stargrads.net/blogs/davinci/2009/10/programming-exercise-permutations-of-a-string/</link>
		<comments>http://stargrads.net/blogs/davinci/2009/10/programming-exercise-permutations-of-a-string/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 17:57:07 +0000</pubDate>
		<dc:creator>davinci</dc:creator>
				<category><![CDATA[programming and technical issues]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[characters]]></category>
		<category><![CDATA[chars]]></category>
		<category><![CDATA[comparison of programming languages]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[permutations]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming exercises]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[strings]]></category>

		<guid isPermaLink="false">http://stargrads.net/blogs/davinci/?p=2318</guid>
		<description><![CDATA[
I do another programming exercise.  The problem this time is to output all the permutations of a string.  I solve this in Scheme, Python, and C++.
]]></description>
			<content:encoded><![CDATA[<p>The problem is to implement a function that outputs all possible <a href="http://en.wikipedia.org/wiki/Permutations">permutations</a> of the characters in a string.  Unlike <a href="http://en.wikipedia.org/wiki/Combinations">combinations</a>, two permutations are considered distinct if they contain the same characters, but in a different order.  Also, for the purposes of this exercise, each occurrence of a repeated character is considered to be a distinct character.  That is, if the input is &#8220;aaa&#8221;, the output should be <em>six</em> repetitions of &#8220;aaa&#8221;.  The permutations may be output in any order.</p>
<p>This exercise, like <a href="http://stargrads.net/blogs/davinci/2009/10/programming-exercise-combinations-of-a-string">the previous one</a> on combinations of a string, is from the book <a href="http://www.amazon.ca/gp/product/047012167X?ie=UTF8&amp;tag=davincisnoteb-20&amp;linkCode=as2&amp;camp=15121&amp;creative=330641&amp;creativeASIN=047012167X"><i>Programming Interviews Exposed</i></a><img src="http://www.assoc-amazon.ca/e/ir?t=davincisnoteb-20&amp;l=as2&amp;o=15&amp;a=047012167X" width="1" height="1" border="0" alt="" style="border:none !important;margin:0px !important" /> by John Mongan and Noah Suojanen<sup><a class='footnote' id='note-2318-1' href='#footnote-2318-ms00pie'>[1]</a></sup>\(\)<span id="more-2318"></span>.<!--adsensestart--></p>
<p>For a string of length \(n\), there are \(n! = n \times (n-1) \times (n-2) \times \cdots \times 1\) permutations.  It&#8217;s fairly easy to see how these arise.  A permutation may begin with any of the \(n\) characters.  Having fixed this first character, the second one may be chosen from the remaining \((n &#8211; 1)\) characters.  And so on.  </p>
<p>The recursive structure of the problem is then obvious.  To compute the permutations of a string of \(n\) characters, first cycle through the characters in the string.  For each character, remove it from the string, compute the permutations of the remaining \((n &#8211; 1)\) characters, and then prepend the previously removed character to each of the resulting permutations.  The base case is the empty string, whose only permutation is itself.</p>
<p>The problem stipulates that each occurrence of a repeated character is to be treated as a distinct character.  Consider the input string &#8220;aba&#8221;.  When dealing with the first &#8220;a&#8221;, the program should remove it, compute the permutations of &#8220;ba&#8221; (which are &#8220;ba&#8221; and &#8220;ab&#8221;), and then prepend &#8220;a&#8221; to those permutations (resulting in &#8220;aba&#8221; and &#8220;aab&#8221;).  However, when dealing with the second occurrence of &#8220;a&#8221;, the program should remove <em>that</em> &#8220;a&#8221;, and compute the permutations of the remaining string, &#8220;ab&#8221;.  This seems to cause some difficulty, as it will mean that our program has to distinguish between the removal of the first &#8220;a&#8221; and the removal of the second &#8220;a&#8221;.  Note, however, that the permutations of &#8220;ab&#8221; are the same as the permutations of &#8220;ba&#8221; (though they might be output in a different order).  A little thought shows that any time we need to remove a character, we can always just remove its first occurrence, since the permutations of the resulting characters will be the same regardless of which occurrence we remove.<!--adsensestop--></p>
<p>To begin with, we need a function to remove the first occurrence of an item from a list.  This is basically an exercise from &#8220;Scheme 101&#8243;:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">define</span> <span style="color: #66cc66;">&#40;</span>remove<span style="color: #66cc66;">-</span>once the<span style="color: #66cc66;">-</span>item the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">null?</span> the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">equal?</span> the<span style="color: #66cc66;">-</span>item <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">car</span> the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">cdr</span> the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">else</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">cons</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">car</span> the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>remove<span style="color: #66cc66;">-</span>once the<span style="color: #66cc66;">-</span>item <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">cdr</span> the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>The heart of our solution will be a function called <code>permute</code>.  Given an empty set, it returns a set containing only the empty set.  Otherwise, it calls itself recursively.  The outline looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">define</span> <span style="color: #66cc66;">&#40;</span>permute the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">null?</span> the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>...<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>What goes into the <code>(...)</code> above?  We can build this up from the inside out.  We know that there is a recursive call to <code>permute</code>, but with the selected character removed (ignoring for the moment how we select this character):</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>permute <span style="color: #66cc66;">&#40;</span>remove<span style="color: #66cc66;">-</span>once the<span style="color: #66cc66;">-</span>selected the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The selected character should be prepended to each of the resulting permutations:</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">lambda</span> <span style="color: #66cc66;">&#40;</span>the<span style="color: #66cc66;">-</span>rest<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">cons</span> the<span style="color: #66cc66;">-</span>selected the<span style="color: #66cc66;">-</span>rest<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>permute <span style="color: #66cc66;">&#40;</span>remove<span style="color: #66cc66;">-</span>once the<span style="color: #66cc66;">-</span>selected the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Each of the characters should become the selected character in turn:</p>

<div class="wp_syntax"><div class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">lambda</span> <span style="color: #66cc66;">&#40;</span>the<span style="color: #66cc66;">-</span>selected<span style="color: #66cc66;">&#41;</span> 
    <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">lambda</span> <span style="color: #66cc66;">&#40;</span>the<span style="color: #66cc66;">-</span>rest<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">cons</span> the<span style="color: #66cc66;">-</span>selected the<span style="color: #66cc66;">-</span>rest<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>permute <span style="color: #66cc66;">&#40;</span>remove<span style="color: #66cc66;">-</span>once the<span style="color: #66cc66;">-</span>selected the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 
                 the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Running the above code on the input <code>'(a b c d e)</code> will result in something like <code>'( (a-perms) (b-perms) (c-perms) (d-perms) (e-perms) )</code>, where <code>x-perms</code> are permutations beginning with the letter <code>x</code>.  This list of lists of permutations should be flattened by <code>apply</code>ing <code>append</code>.  The rest of the program in Scheme is then:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">define</span> <span style="color: #66cc66;">&#40;</span>permute the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">null?</span> the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>apply <span style="color: #0000ff;">append</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">lambda</span> <span style="color: #66cc66;">&#40;</span>the<span style="color: #66cc66;">-</span>selected<span style="color: #66cc66;">&#41;</span> 
                         <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">lambda</span> <span style="color: #66cc66;">&#40;</span>the<span style="color: #66cc66;">-</span>rest<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">cons</span> the<span style="color: #66cc66;">-</span>selected the<span style="color: #66cc66;">-</span>rest<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                              <span style="color: #66cc66;">&#40;</span>permute <span style="color: #66cc66;">&#40;</span>remove<span style="color: #66cc66;">-</span>once the<span style="color: #66cc66;">-</span>selected the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 
                 the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">list</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">define</span> <span style="color: #66cc66;">&#40;</span>permutations the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">string</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">s</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>apply <span style="color: #0000ff;">string</span> <span style="color: #0000ff;">s</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span>permute <span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">-&gt;</span>list the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">string</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">define</span> test<span style="color: #66cc66;">-</span>input <span style="color: #ff0000;">&quot;abcde&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">display</span> <span style="color: #66cc66;">&#40;</span>permutations test<span style="color: #66cc66;">-</span>input<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Because we need to output \(n!\) permutations (note that the factorial grows faster than the exponential function), it&#8217;s probably a good idea in most cases not to keep the permutations in memory, but rather to print them out as they are generated.  </p>
<p>The following Python program uses logic similar to the above Scheme program, but prints out each permutation right away.  The only difference in the logic is that, because the Python program uses strings and indices into strings, it distinguishes between each occurrence of a repeated character:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> print_permutations<span style="color: black;">&#40;</span>input_string<span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> print_permutations_helper<span style="color: black;">&#40;</span>set_of_chars, output_string<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> set_of_chars == <span style="color: #483d8b;">&quot;&quot;</span>:
            <span style="color: #ff7700;font-weight:bold;">print</span> output_string,
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">for</span> index, char <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>set_of_chars<span style="color: black;">&#41;</span>:
                print_permutations_helper<span style="color: black;">&#40;</span>
                    set_of_chars<span style="color: black;">&#91;</span>:index<span style="color: black;">&#93;</span>+set_of_chars<span style="color: black;">&#91;</span>index+<span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>, 
                    output_string + char<span style="color: black;">&#41;</span>
&nbsp;
    print_permutations_helper<span style="color: black;">&#40;</span>input_string, <span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
test_input = <span style="color: #483d8b;">&quot;abcde&quot;</span>
print_permutations<span style="color: black;">&#40;</span>test_input<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>One can manipulate sets, lists, or strings in C++ using <abbr title="Standard Template Library">STL</abbr> classes, but it&#8217;s probably much faster to just use a boolean array to keep track of which letters have been &#8220;removed&#8221; from use.  The following C++ program produces the same output as the above Python program:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #339900;">#include&lt;string.h&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> print_permutations_helper<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>input, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>output, <span style="color: #0000ff;">bool</span> <span style="color: #000040;">*</span>used,
    <span style="color: #0000ff;">int</span> len, <span style="color: #0000ff;">int</span> depth<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// At the current depth, cycle through all the letters.</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> len<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
&nbsp;
        <span style="color: #666666;">// If the letter hasn't been used yet, use it.</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> <span style="color: #000040;">!</span>used<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            output<span style="color: #008000;">&#91;</span>depth<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> input<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
            <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> depth <span style="color: #000080;">==</span> len <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</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> output <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008080;">;</span>
            <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
                used<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
                print_permutations_helper<span style="color: #008000;">&#40;</span>input, output, used, len, depth<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
                used<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</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: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> print_permutations<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>input<span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// Allocate a new character buffer to hold the output, and also</span>
    <span style="color: #666666;">// an array of flags for keeping track of which letters have</span>
    <span style="color: #666666;">// already been used.  (Assume that this always succeeds, or</span>
    <span style="color: #666666;">// that error handling is taken care of elsewhere.)</span>
    <span style="color: #0000ff;">int</span> len <span style="color: #000080;">=</span> <span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>input<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>output <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">char</span><span style="color: #008000;">&#91;</span>len<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    output<span style="color: #008000;">&#91;</span>len<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">'\ 0'</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">bool</span> <span style="color: #000040;">*</span>used <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">bool</span><span style="color: #008000;">&#91;</span>len<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Initialise all the letters as unused.</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> len<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        used<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #666666;">// Recursively print the permutations. </span>
    print_permutations_helper<span style="color: #008000;">&#40;</span>input, output, used, len, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Free the memory.    </span>
    <span style="color: #0000dd;">delete</span> output<span style="color: #008080;">;</span>
    <span style="color: #0000dd;">delete</span> used<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">char</span> test_input<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;abcde&quot;</span><span style="color: #008080;">;</span>
    print_permutations<span style="color: #008000;">&#40;</span>test_input<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>As with the <a href="http://stargrads.net/blogs/davinci/2009/10/programming-exercise-combinations-of-a-string/">previous exercise on combinations of a string</a>, this exercise also demonstrates how the typical approach to solving a problem will differ depending on whether Scheme or C++ (or some other language) is used.  One can postulate a version of the <a href="http://en.wikipedia.org/wiki/Linguistic_relativity">Sapir-Whorf hypothesis</a> for programming languages, that the language used to tackle a problem has an effect on the way in which it is solved.</p>
<p>&#8211; davinci 11804</p>




	<a rel="nofollow"  href="http://stargrads.net/blogs/davinci/feed/" title="RSS"><img src="http://stargrads.net/common/images/handycons/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Programming%20exercise%3A%20permutations%20of%20a%20string&amp;body=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-permutations-of-a-string%2F" title="email"><img src="http://stargrads.net/common/images/handycons/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-permutations-of-a-string%2F&amp;t=Programming%20exercise%3A%20permutations%20of%20a%20string" title="Facebook"><img src="http://stargrads.net/common/images/handycons/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Programming%20exercise%3A%20permutations%20of%20a%20string%20-%20http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-permutations-of-a-string%2F" title="Twitter"><img src="http://stargrads.net/common/images/handycons/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=Programming%20exercise%3A%20permutations%20of%20a%20string&amp;link=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-permutations-of-a-string%2F" title="FriendFeed"><img src="http://stargrads.net/common/images/handycons/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-permutations-of-a-string%2F&amp;title=Programming%20exercise%3A%20permutations%20of%20a%20string&amp;notes=I%20do%20another%20programming%20exercise.%20%20The%20problem%20this%20time%20is%20to%20output%20all%20the%20permutations%20of%20a%20string.%20%20I%20solve%20this%20in%20Scheme%2C%20Python%2C%20and%20C%2B%2B." title="del.icio.us"><img src="http://stargrads.net/common/images/handycons/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-permutations-of-a-string%2F&amp;title=Programming%20exercise%3A%20permutations%20of%20a%20string&amp;bodytext=I%20do%20another%20programming%20exercise.%20%20The%20problem%20this%20time%20is%20to%20output%20all%20the%20permutations%20of%20a%20string.%20%20I%20solve%20this%20in%20Scheme%2C%20Python%2C%20and%20C%2B%2B." title="Digg"><img src="http://stargrads.net/common/images/handycons/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-permutations-of-a-string%2F&amp;title=Programming%20exercise%3A%20permutations%20of%20a%20string&amp;annotation=I%20do%20another%20programming%20exercise.%20%20The%20problem%20this%20time%20is%20to%20output%20all%20the%20permutations%20of%20a%20string.%20%20I%20solve%20this%20in%20Scheme%2C%20Python%2C%20and%20C%2B%2B." title="Google Bookmarks"><img src="http://stargrads.net/common/images/handycons/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://bookmarks.yahoo.com/toolbar/savebm?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-permutations-of-a-string%2F&amp;t=Programming%20exercise%3A%20permutations%20of%20a%20string&opener=bm&amp;ei=UTF-8&amp;d=I%20do%20another%20programming%20exercise.%20%20The%20problem%20this%20time%20is%20to%20output%20all%20the%20permutations%20of%20a%20string.%20%20I%20solve%20this%20in%20Scheme%2C%20Python%2C%20and%20C%2B%2B." title="Yahoo! Bookmarks"><img src="http://stargrads.net/common/images/handycons/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-permutations-of-a-string%2F&amp;title=Programming%20exercise%3A%20permutations%20of%20a%20string" title="StumbleUpon"><img src="http://stargrads.net/common/images/handycons/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-permutations-of-a-string%2F" title="Technorati"><img src="http://stargrads.net/common/images/handycons/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-permutations-of-a-string%2F&amp;title=Programming%20exercise%3A%20permutations%20of%20a%20string" title="Reddit"><img src="http://stargrads.net/common/images/handycons/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>


<br/><br/><img src="http://stargrads.net/blogs/davinci/?ak_action=api_record_view&id=2318&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://stargrads.net/blogs/davinci/2009/10/programming-exercise-combinations-of-a-string/' rel='bookmark' title='Permanent Link: Programming exercise: combinations of a string'>Programming exercise: combinations of a string</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/09/programming-exercise-hello-world/' rel='bookmark' title='Permanent Link: Programming exercise: Hello, world!'>Programming exercise: Hello, world!</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/09/programming-exercise-maximum-value-in-integer-array-part-1/' rel='bookmark' title='Permanent Link: Programming exercise: maximum value in integer array, part 1'>Programming exercise: maximum value in integer array, part 1</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://stargrads.net/blogs/davinci/2009/10/programming-exercise-permutations-of-a-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming exercise: combinations of a string</title>
		<link>http://stargrads.net/blogs/davinci/2009/10/programming-exercise-combinations-of-a-string/</link>
		<comments>http://stargrads.net/blogs/davinci/2009/10/programming-exercise-combinations-of-a-string/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 18:13:27 +0000</pubDate>
		<dc:creator>davinci</dc:creator>
				<category><![CDATA[programming and technical issues]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[characters]]></category>
		<category><![CDATA[chars]]></category>
		<category><![CDATA[combinations]]></category>
		<category><![CDATA[comparison of programming languages]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[permutations]]></category>
		<category><![CDATA[power set]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming exercises]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[strings]]></category>

		<guid isPermaLink="false">http://stargrads.net/blogs/davinci/?p=2290</guid>
		<description><![CDATA[
The problem is to implement a function that outputs all possible combinations of the characters in a string (with length ranging from one to the length of the string). Unlike permutations, two combinations are considered to be the same if they contain the same characters, but in a different order. Another way to define the [...]
]]></description>
			<content:encoded><![CDATA[<p>The problem is to implement a function that outputs all possible <a href="http://en.wikipedia.org/wiki/Combinations">combinations</a> of the characters in a string (with length ranging from one to the length of the string).  Unlike <a href="http://en.wikipedia.org/wiki/Permutations">permutations</a>, two combinations are considered to be the same if they contain the same characters, but in a different order.  Another way to define the problem is to find the <a href="http://en.wikipedia.org/wiki/Power_set">power set</a> of the characters of the string (excluding the empty set).</p>
<p>Like the previous exercise, this one is also from the book <a href="http://www.amazon.ca/gp/product/047012167X?ie=UTF8&amp;tag=davincisnoteb-20&amp;linkCode=as2&amp;camp=15121&amp;creative=330641&amp;creativeASIN=047012167X"><i>Programming Interviews Exposed</i></a><img src="http://www.assoc-amazon.ca/e/ir?t=davincisnoteb-20&amp;l=as2&amp;o=15&amp;a=047012167X" width="1" height="1" border="0" alt="" style="border:none !important;margin:0px !important" /> by John Mongan and Noah Suojanen<sup><a class='footnote' id='note-2290-1' href='#footnote-2290-ms00pie'>[1]</a></sup>\(\)<span id="more-2290"></span>.<!--adsensestart--></p>
<p>To begin with, let&#8217;s consider an example.  What are the combinations of the string &#8220;abcde&#8221;?  Since the ordering of the letters within the combinations don&#8217;t matter, let&#8217;s keep the letters in the same order as in the original string.  Also, for now, let&#8217;s include the empty string as one of the combinations, for simplicity.  Then it&#8217;s clear that there are \(2^{5} = 32\) combinations, since each of the \(5\) letters can be either included or excluded.  (For an input string of length \(n\), there will be \(2^{n}\) combinations, including the empty string.)<!--adsensestop--></p>
<p>The letter &#8220;a&#8221; will be included in half of the combinations, and excluded in the other half.  (Of course, this is also true of each of the other letters.)  In fact, those combinations that exclude &#8220;a&#8221; are just the combinations of the string &#8220;bcde&#8221;, and those that include &#8220;a&#8221; are exactly these same combinations, but prefixed with &#8220;a&#8221;.  This gives an obvious recursive solution to the problem.</p>
<p>Expressed in Scheme, the program looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">define</span> <span style="color: #66cc66;">&#40;</span>power<span style="color: #66cc66;">-</span>set the<span style="color: #66cc66;">-</span>set<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">null?</span> the<span style="color: #66cc66;">-</span>set<span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">let</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span>power<span style="color: #66cc66;">-</span>set<span style="color: #66cc66;">-</span>of<span style="color: #66cc66;">-</span>subset <span style="color: #66cc66;">&#40;</span>power<span style="color: #66cc66;">-</span>set <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">cdr</span> the<span style="color: #66cc66;">-</span>set<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 
               <span style="color: #66cc66;">&#40;</span>prepend<span style="color: #66cc66;">-</span>excluded <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">lambda</span> <span style="color: #66cc66;">&#40;</span>subset<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">cons</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">car</span> the<span style="color: #66cc66;">-</span>set<span style="color: #66cc66;">&#41;</span> subset<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">append</span>
                power<span style="color: #66cc66;">-</span>set<span style="color: #66cc66;">-</span>of<span style="color: #66cc66;">-</span>subset
                <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">map</span> prepend<span style="color: #66cc66;">-</span>excluded power<span style="color: #66cc66;">-</span>set<span style="color: #66cc66;">-</span>of<span style="color: #66cc66;">-</span>subset<span style="color: #66cc66;">&#41;</span> 
             <span style="color: #66cc66;">&#41;</span> 
        <span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">define</span> <span style="color: #66cc66;">&#40;</span>combinations the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">string</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">s</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>apply <span style="color: #0000ff;">string</span> <span style="color: #0000ff;">s</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 
         <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">cdr</span> <span style="color: #66cc66;">&#40;</span>power<span style="color: #66cc66;">-</span>set <span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">-&gt;</span>list the<span style="color: #66cc66;">-</span><span style="color: #0000ff;">string</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">define</span> test<span style="color: #66cc66;">-</span>input <span style="color: #ff0000;">&quot;abcde&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">display</span> <span style="color: #66cc66;">&#40;</span>combinations test<span style="color: #66cc66;">-</span>input<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>The <code>combinations</code> function just converts the input string to a list of letters, applies <code>power-set</code> to the list, and converts the result back into a list of strings.  (The empty set is excluded by applying <code>cdr</code>, since it is the first item in the list returned by <code>power-set</code> by design.)</p>
<p>The <code>power-set</code> function is pretty straightforward.  It computes the power set of the subset that excludes the first letter (with a recursive call), then returns a set consisting of the elements of this power set along with these elements prepended with the excluded letter.  The base case is slightly tricky: the power set of the empty set is not the empty set, but a set containing only the empty set.  </p>
<p>(Aside: I wish that the higher-order functions <a href="http://en.wikipedia.org/wiki/Map_(higher-order_function)">map</a> and <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)">fold/reduce</a> had better names &#8212; especially &#8220;map&#8221;, a word that is used for just too many things.)</p>
<p>The same program can be written in Python as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> combinations<span style="color: black;">&#40;</span>input_string<span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> combinations_helper<span style="color: black;">&#40;</span>set_of_chars<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> set_of_chars == <span style="color: #483d8b;">&quot;&quot;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            combinations_of_subset = combinations_helper<span style="color: black;">&#40;</span>set_of_chars<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            combinations_of_subset.<span style="color: black;">extend</span><span style="color: black;">&#40;</span>
                <span style="color: #008000;">map</span><span style="color: black;">&#40;</span> <span style="color: #ff7700;font-weight:bold;">lambda</span> t: set_of_chars<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + t, combinations_of_subset<span style="color: black;">&#41;</span>
            <span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> combinations_of_subset
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> combinations_helper<span style="color: black;">&#40;</span>input_string<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>
&nbsp;
test_input = <span style="color: #483d8b;">&quot;abcde&quot;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> combinations<span style="color: black;">&#40;</span>test_input<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>The Python program uses string manipulation operations (string slicing, concatenation) instead of treating the input as a list as the Scheme program does.  The idea, however, is exactly the same.</p>
<p>There are probably more efficient ways to write these programs in these languages.  In particular, the conversions between strings and lists (in Scheme) and the string manipulation operations (in Python) may be expensive.  </p>
<p>What I like about the <em>idea</em> behind the above programs is that the result of the computation for the combinations excluding a character are re-used when computing the combinations including it.  This saves on a lot of recursive calls.</p>
<p>The &#8220;obvious&#8221; solution to the same problem in C++ is the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #339900;">#include&lt;string.h&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> print_combinations_helper<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>input, <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>output, 
    <span style="color: #0000ff;">int</span> len, <span style="color: #0000ff;">int</span> depth, <span style="color: #0000ff;">int</span> start<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// The variable depth holds the recursion depth, or, equivalently, </span>
    <span style="color: #666666;">// the index into the output string of the character that's being</span>
    <span style="color: #666666;">// generated.  The variable start is the index of the first of</span>
    <span style="color: #666666;">// the still available letters.</span>
&nbsp;
    <span style="color: #666666;">// At the current depth, cycle through the still available letters.</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> start<span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> len<span style="color: #008080;">;</span> i<span style="color: #000040;">++</span> <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        output<span style="color: #008000;">&#91;</span>depth<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> input<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
        print_combinations_helper<span style="color: #008000;">&#40;</span>input, output, len, depth<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span>, i<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>
    output<span style="color: #008000;">&#91;</span>depth<span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">'\ 0'</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> output <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> print_combinations<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>input<span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #666666;">// Allocate a new character buffer to hold the output.  (Assume </span>
    <span style="color: #666666;">// that this always succeeds, or that error handling is being</span>
    <span style="color: #666666;">// taken care of elsewhere.)</span>
    <span style="color: #0000ff;">int</span> len <span style="color: #000080;">=</span> <span style="color: #0000dd;">strlen</span><span style="color: #008000;">&#40;</span>input<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">char</span> <span style="color: #000040;">*</span>output <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> <span style="color: #0000ff;">char</span><span style="color: #008000;">&#91;</span>len<span style="color: #000040;">+</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Recursively print the combinations, starting with the first</span>
    <span style="color: #666666;">// character.</span>
    print_combinations_helper<span style="color: #008000;">&#40;</span>input, output, len, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #666666;">// Free the memory for the output buffer.</span>
    <span style="color: #0000dd;">delete</span> output<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">char</span> test_input<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #FF0000;">&quot;abcde&quot;</span><span style="color: #008080;">;</span>
    print_combinations<span style="color: #008000;">&#40;</span>test_input<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The above program actually also prints out the empty string as one of the combinations, but this can be easily fixed by an <code>if</code> clause, or by relocating the <code>cout</code> statement (lines 18&#8211;19) inside the loop (at line 16, and substituting <code>depth+1</code> for <code>depth</code>).  The reason that I didn&#8217;t write the program that way is because then the combinations would have been output in a very different order compared to the above Scheme/Python programs.  (The way it&#8217;s written now, the output is actually in exactly the reverse order, but that&#8217;s good enough for comparison purposes.)</p>
<p>While the recursive function is called only \(n+1\) times in the Scheme/Python programs (once for each character and once more for the empty set), the C++ program makes a total of \(2^{n}\) calls to the recursive helper function.  On the other hand, while the Scheme/Python programs have to keep \(2^{n}\) items in memory (each of which is \(O(n)\) in size), the C++ program prints each combination as soon as it has been computed and thus uses only \(O(n)\) memory.  </p>
<p>Personally, I prefer the solution used in the Scheme program because I find it conceptually much more elegant.  It would also be preferably if recursive function calls are expensive for some reason.  However, the solution used in the C++ program is better if memory is limited, or if the combinations do not need to be retained in memory after they have been printed.  </p>
<p>Of course, one can write the first program in C++ or the second one in Scheme, but the point is that the two solutions are, in some sense, the more &#8220;natural&#8221; ones for their respective languages.</p>
<p>&#8211; davinci 11803</p>




	<a rel="nofollow"  href="http://stargrads.net/blogs/davinci/feed/" title="RSS"><img src="http://stargrads.net/common/images/handycons/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Programming%20exercise%3A%20combinations%20of%20a%20string&amp;body=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-combinations-of-a-string%2F" title="email"><img src="http://stargrads.net/common/images/handycons/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-combinations-of-a-string%2F&amp;t=Programming%20exercise%3A%20combinations%20of%20a%20string" title="Facebook"><img src="http://stargrads.net/common/images/handycons/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Programming%20exercise%3A%20combinations%20of%20a%20string%20-%20http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-combinations-of-a-string%2F" title="Twitter"><img src="http://stargrads.net/common/images/handycons/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=Programming%20exercise%3A%20combinations%20of%20a%20string&amp;link=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-combinations-of-a-string%2F" title="FriendFeed"><img src="http://stargrads.net/common/images/handycons/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-combinations-of-a-string%2F&amp;title=Programming%20exercise%3A%20combinations%20of%20a%20string&amp;notes=The%20problem%20is%20to%20implement%20a%20function%20that%20outputs%20all%20possible%20%5Bhttp%3A%2F%2Fen.wikipedia.org%2Fwiki%2FCombinations%20combinations%5D%20of%20the%20characters%20in%20a%20string%20%28with%20length%20ranging%20from%20one%20to%20the%20length%20of%20the%20string%29.%20%20Unlike%20%5Bhttp%3A%2F%2Fen.wikipedia.org%2Fwiki%2F" title="del.icio.us"><img src="http://stargrads.net/common/images/handycons/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-combinations-of-a-string%2F&amp;title=Programming%20exercise%3A%20combinations%20of%20a%20string&amp;bodytext=The%20problem%20is%20to%20implement%20a%20function%20that%20outputs%20all%20possible%20%5Bhttp%3A%2F%2Fen.wikipedia.org%2Fwiki%2FCombinations%20combinations%5D%20of%20the%20characters%20in%20a%20string%20%28with%20length%20ranging%20from%20one%20to%20the%20length%20of%20the%20string%29.%20%20Unlike%20%5Bhttp%3A%2F%2Fen.wikipedia.org%2Fwiki%2F" title="Digg"><img src="http://stargrads.net/common/images/handycons/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-combinations-of-a-string%2F&amp;title=Programming%20exercise%3A%20combinations%20of%20a%20string&amp;annotation=The%20problem%20is%20to%20implement%20a%20function%20that%20outputs%20all%20possible%20%5Bhttp%3A%2F%2Fen.wikipedia.org%2Fwiki%2FCombinations%20combinations%5D%20of%20the%20characters%20in%20a%20string%20%28with%20length%20ranging%20from%20one%20to%20the%20length%20of%20the%20string%29.%20%20Unlike%20%5Bhttp%3A%2F%2Fen.wikipedia.org%2Fwiki%2F" title="Google Bookmarks"><img src="http://stargrads.net/common/images/handycons/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://bookmarks.yahoo.com/toolbar/savebm?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-combinations-of-a-string%2F&amp;t=Programming%20exercise%3A%20combinations%20of%20a%20string&opener=bm&amp;ei=UTF-8&amp;d=The%20problem%20is%20to%20implement%20a%20function%20that%20outputs%20all%20possible%20%5Bhttp%3A%2F%2Fen.wikipedia.org%2Fwiki%2FCombinations%20combinations%5D%20of%20the%20characters%20in%20a%20string%20%28with%20length%20ranging%20from%20one%20to%20the%20length%20of%20the%20string%29.%20%20Unlike%20%5Bhttp%3A%2F%2Fen.wikipedia.org%2Fwiki%2F" title="Yahoo! Bookmarks"><img src="http://stargrads.net/common/images/handycons/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-combinations-of-a-string%2F&amp;title=Programming%20exercise%3A%20combinations%20of%20a%20string" title="StumbleUpon"><img src="http://stargrads.net/common/images/handycons/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-combinations-of-a-string%2F" title="Technorati"><img src="http://stargrads.net/common/images/handycons/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F10%2Fprogramming-exercise-combinations-of-a-string%2F&amp;title=Programming%20exercise%3A%20combinations%20of%20a%20string" title="Reddit"><img src="http://stargrads.net/common/images/handycons/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>


<br/><br/><img src="http://stargrads.net/blogs/davinci/?ak_action=api_record_view&id=2290&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://stargrads.net/blogs/davinci/2009/10/programming-exercise-permutations-of-a-string/' rel='bookmark' title='Permanent Link: Programming exercise: permutations of a string'>Programming exercise: permutations of a string</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/09/programming-exercise-hello-world/' rel='bookmark' title='Permanent Link: Programming exercise: Hello, world!'>Programming exercise: Hello, world!</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/09/programming-exercise-maximum-value-in-integer-array-part-1/' rel='bookmark' title='Permanent Link: Programming exercise: maximum value in integer array, part 1'>Programming exercise: maximum value in integer array, part 1</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://stargrads.net/blogs/davinci/2009/10/programming-exercise-combinations-of-a-string/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Programming exercise: maximum value in integer array, part 1</title>
		<link>http://stargrads.net/blogs/davinci/2009/09/programming-exercise-maximum-value-in-integer-array-part-1/</link>
		<comments>http://stargrads.net/blogs/davinci/2009/09/programming-exercise-maximum-value-in-integer-array-part-1/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 06:32:20 +0000</pubDate>
		<dc:creator>davinci</dc:creator>
				<category><![CDATA[programming and technical issues]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Common Lisp. Scheme]]></category>
		<category><![CDATA[comparison of programming languages]]></category>
		<category><![CDATA[fold]]></category>
		<category><![CDATA[int]]></category>
		<category><![CDATA[integers]]></category>
		<category><![CDATA[iteration]]></category>
		<category><![CDATA[iterators]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[max]]></category>
		<category><![CDATA[maximum]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming exercises]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[recursion]]></category>
		<category><![CDATA[reduce]]></category>
		<category><![CDATA[STL]]></category>
		<category><![CDATA[tail recursion]]></category>

		<guid isPermaLink="false">http://stargrads.net/blogs/davinci/?p=2145</guid>
		<description><![CDATA[
In this post, I write a program to find the maximum value in an integer array in several programming languages.
]]></description>
			<content:encoded><![CDATA[<p>This exercise is just a <em>little</em> bit more substantial than the <a href="http://stargrads.net/blogs/davinci/2009/09/programming-exercise-hello-world/">last one</a> &#8212; but not by very much.  Given an array of \(n\) non-negative integers, find the maximum value in the array, or return \(-1\) if the array is empty.  Obviously, the use of any built-in maximum-finding function is forbidden.  While the problem is almost trivial, it does illustrate how each language works with array or vector data types, as well as how it handles iteration.  </p>
<p>This exercise, like some of the other ones that I will also be going through, is from the book <a href="http://www.amazon.ca/gp/product/047012167X?ie=UTF8&amp;tag=davincisnoteb-20&amp;linkCode=as2&amp;camp=15121&amp;creative=330641&amp;creativeASIN=047012167X"><i>Programming Interviews Exposed</i></a><img src="http://www.assoc-amazon.ca/e/ir?t=davincisnoteb-20&amp;l=as2&amp;o=15&amp;a=047012167X" width="1" height="1" border="0" alt="" style="border:none !important;margin:0px !important" /> by John Mongan and Noah Suojanen<sup><a class='footnote' id='note-2145-1' href='#footnote-2145-ms00pie'>[1]</a></sup><span id="more-2145"></span>.<!--adsensestart--></p>
<p>For consistency, let&#8217;s assume that the array for which we&#8217;re trying to find the maximum value is \(\{6, 3, 8, 7, 1, 2, 0, 9, 4, 5\}\).  Let&#8217;s also assume for simplicity that \(n\) is given, that we don&#8217;t have to check for errors (the input is a list of non-negative integers as promised), and that we can write \(-1\) for the error return code.  (In production code, we would of course define a named constant such as <code>ARRAY_EMPTY_ERROR</code>, or use some other form of error-handling such as exceptions.)<!--adsensestop--></p>
<p>Let&#8217;s begin with C.  The code is pretty straightforward, except that I had to remind myself to declare the loop counter \(i\) outside of the loop.  (Declaring it in the loop initialisation is not allowed in standards prior to C99.)  It&#8217;s also a lot harder to code on paper than it is on a keyboard (my handwriting is terrible, due to atrophy).  Here&#8217;s the program in C:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include&lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> arraymax<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> array<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> currentMax<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> n<span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
    currentMax <span style="color: #339933;">=</span> array<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> n<span style="color: #339933;">;</span> i<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> array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> currentMax <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            currentMax <span style="color: #339933;">=</span> array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> currentMax<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> testinput<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000dd;">6</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">7</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">9</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">5</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;max = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> arraymax<span style="color: #009900;">&#40;</span>testinput<span style="color: #339933;">,</span> <span style="color: #0000dd;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I could, of course, have written the exact same program in C++, but to make things interesting, I&#8217;m going to use the <abbr title="Standard Template Library">STL</abbr> <code>vector</code> class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td 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>
&nbsp;
<span style="color: #0000ff;">int</span> vectormax<span style="color: #008000;">&#40;</span>vector<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> vec<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> currentMax<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> vec.<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: #0000ff;">return</span> <span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    currentMax <span style="color: #000080;">=</span> vec<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> vec.<span style="color: #007788;">size</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> i<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> vec<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&gt;</span> currentMax <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            currentMax <span style="color: #000080;">=</span> vec<span style="color: #008000;">&#91;</span>i<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> currentMax<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">int</span> values<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">6</span>, <span style="color: #0000dd;">3</span>, <span style="color: #0000dd;">8</span>, <span style="color: #0000dd;">7</span>, <span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">2</span>, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">9</span>, <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">5</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
    vector<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> testinput<span style="color: #008000;">&#40;</span>values, values<span style="color: #000040;">+</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;max = &quot;</span> <span style="color: #000080;">&lt;&lt;</span> vectormax<span style="color: #008000;">&#40;</span>testinput<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The only tricky part was I had to look up how to initialise a vector without looping through the values and using <code>push_back</code>.  The code above calls the iteration constructor, using the trick that an <code>int</code> array in C++ is just a pointer to its first element, and the fact that pointers are iterators.</p>
<p>Apparently, in the proposed <a href="http://en.wikipedia.org/wiki/C%2B%2B0x">c++0x</a> standard, it will be possible to initialise a vector using a static array like <a href="http://en.wikipedia.org/wiki/C%2B%2B0x#Initializer_lists">this</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>26
27
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #666666;">// Use the initialiser list constructor.</span>
    vector<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span> testinput <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">6</span>, <span style="color: #0000dd;">3</span>, <span style="color: #0000dd;">8</span>, <span style="color: #0000dd;">7</span>, <span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">2</span>, <span style="color: #0000dd;">0</span>, <span style="color: #0000dd;">9</span>, <span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">5</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>In the program above, I&#8217;ve used indices as if the vector were just a regular array.  The preferred way to access vector elements in C++ is to use iterators.  This means replacing lines 14&#8211;15 with the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>14
15
16
17
18
19
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">    vector<span style="color: #000080;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> vi <span style="color: #000080;">=</span> vec.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> currentMax <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>vi<span style="color: #000040;">++</span><span style="color: #008080;">;</span> vi <span style="color: #000040;">!</span><span style="color: #000080;">=</span> vec.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> vi<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> <span style="color: #000040;">*</span>vi <span style="color: #000080;">&gt;</span> currentMax <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            currentMax <span style="color: #000080;">=</span> <span style="color: #000040;">*</span>vi<span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I had to think a little bit about the loop initialisation condition, <code>currentMax = *vi++</code>.  The iterator is <a href="http://stackoverflow.com/questions/859770/-on-a-dereferenced-pointer-in-c">dereferenced before it is incremented</a>, which is the desired behaviour.</p>
<p>There are a number of differences between the C# and C/C++ programs.  First, there&#8217;s the <code>foreach</code> statement, which makes for nicer-looking loops.  Then there&#8217;s the fact that the array type specifier <code>[]</code> must appear before the variable name, rather than after.  Here&#8217;s the code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ArrayMax <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">int</span> arraymax<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> array<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span> array.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span> <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #FF0000;">int</span> currentMax <span style="color: #008000;">=</span> array<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span> <span style="color: #FF0000;">int</span> value <span style="color: #0600FF;">in</span> array <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span> value <span style="color: #008000;">&gt;</span> currentMax <span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                currentMax <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">return</span> currentMax<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> testinput <span style="color: #008000;">=</span> <span style="color: #000000;">&#123;</span> <span style="color: #FF0000;">6</span>, <span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">8</span>, <span style="color: #FF0000;">7</span>, <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">9</span>, <span style="color: #FF0000;">4</span>, <span style="color: #FF0000;">5</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
        Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;max = {0}<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span>, arraymax<span style="color: #000000;">&#40;</span>testinput<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>I had to remember to make the <code>arraymax</code> function static so that it can be called without instantiating the <code>ArrayMax</code> class.</p>
<p>The Java code is essentially identical to the C# code, <em>mutatis mutandis</em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ArrayMax <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> arraymax<span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> array <span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span> 
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> array.<span style="color: #006633;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">int</span> currentMax <span style="color: #339933;">=</span> array<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> array.<span style="color: #006633;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> currentMax <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                currentMax <span style="color: #339933;">=</span> array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> currentMax<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span> <span style="color: #003399;">String</span> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">int</span> testinput<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">7</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">9</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;max = &quot;</span> <span style="color: #339933;">+</span> arraymax<span style="color: #009900;">&#40;</span>testinput<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Beginning in J2SE v1.5/5.0<sup><a class='footnote' id='note-2145-2' href='#footnote-2145-java_version_history'>[2]</a></sup>, Java introduced a &#8220;for each&#8221; loop, and so lines 9&#8211;13 above should be replaced with this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>9
10
11
12
13
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> value <span style="color: #339933;">:</span> array <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> value <span style="color: #339933;">&gt;</span> currentMax <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                currentMax <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now that we&#8217;ve gotten the standard general-purpose imperative and object-oriented (i.e., <a href="http://en.wikipedia.org/wiki/C-like_syntax">&#8220;C-like&#8221;</a>) languages out of the way, it&#8217;s time for some fun(ctional programming).</p>
<p>Of course, Lisp and Scheme already have a <code>max</code> function defined to get the maximum value in a list of integers, but as mentioned before, its use is forbidden to us.  Nevertheless, the solution to the given problem in these languages is still very simple.  How can we express the function that returns the maximum value of a list of integers?  One way to do it is to express it as the <a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)">folding (or reducing)</a> of the list with a function that compares <em>two</em> integer values and returns the larger one.  Expressed in Lisp, this function is:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">defun</span> larger <span style="color: #66cc66;">&#40;</span>x y<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> x y<span style="color: #66cc66;">&#41;</span> x y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>The rest of the Lisp program easily follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">defun</span> list-<span style="color: #0000ff;">max</span> <span style="color: #66cc66;">&#40;</span>intlist<span style="color: #66cc66;">&#41;</span> 
    <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">null</span> intlist<span style="color: #66cc66;">&#41;</span> -<span style="color: #cc66cc;">1</span>
          <span style="color: #66cc66;">&#40;</span>reduce #'larger intlist<span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">setq</span> intlist '<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;max = ~D&quot;</span> <span style="color: #66cc66;">&#40;</span>list-<span style="color: #0000ff;">max</span> intlist<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>(Incidentally, it&#8217;s because of things like <code>#'</code>, which is called the sharp-quote, that some people don&#8217;t like Lisp.)</p>
<p>The Scheme program is essentially identical:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">define</span> <span style="color: #66cc66;">&#40;</span>listmax intlist<span style="color: #66cc66;">&#41;</span> 
    <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">null?</span> intlist<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">let</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">&#40;</span>larger <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">lambda</span> <span style="color: #66cc66;">&#40;</span>x y<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> x y<span style="color: #66cc66;">&#41;</span> x y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span>fold larger <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">car</span> intlist<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">cdr</span> intlist<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">define</span> intlist '<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">9</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">display</span> <span style="color: #ff0000;">&quot;max = &quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">display</span> <span style="color: #66cc66;">&#40;</span>listmax intlist<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>The Python code is also very similar:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> listmax<span style="color: black;">&#40;</span>intlist<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> intlist == <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> -<span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>: 
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span> <span style="color: #ff7700;font-weight:bold;">lambda</span> x, y : x <span style="color: #ff7700;font-weight:bold;">if</span> x <span style="color: #66cc66;">&gt;</span> y <span style="color: #ff7700;font-weight:bold;">else</span> y, intlist<span style="color: black;">&#41;</span>
&nbsp;
testinput = <span style="color: black;">&#91;</span> <span style="color: #ff4500;">6</span>, <span style="color: #ff4500;">3</span>, <span style="color: #ff4500;">8</span>, <span style="color: #ff4500;">7</span>, <span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">2</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">9</span>, <span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">5</span> <span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;max =&quot;</span>, listmax<span style="color: black;">&#40;</span>testinput<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>I really like the Python code.  It combines the simplicity of the fold idea used in Lisp/Scheme with the clarity of an ALGOL-like syntax.</p>
<p>I should mention that another way of expressing the function that returns the maximum value in a list of integers is as a recursive function that returns the only element in a list of size one, and returns the larger of the first element and the result of the function applied to the rest of the list for a list of size two or more.  However, unless the program is written using <a href="http://en.wikipedia.org/wiki/Tail_recursion">tail recursion</a>, and one is using a compiler that optimises tail-recursive calls, the stack will grow with the size of the list, so this solution is not as good as the one using fold/reduce.</p>
<p>I was originally also going to write the program in the remaining languages listed in the <a href="http://stargrads.net/blogs/davinci/2009/09/programming-exercise-hello-world/">previous post</a>, but I&#8217;ve spent far too much time already on this exercise, and I&#8217;d like to move on to some more difficult ones.  And besides, the program in the other functional languages will be very similar except for syntax, and there&#8217;s almost no point in coding a &#8220;maximum integer value in an array&#8221; function in the mathematical languages, since an optimised version of such a function is already built into them.  I may return to this exercise later with additional languages, so I&#8217;ll append &#8220;part 1&#8243; to the title of this post.</p>
<p>&#8211; davinci 11796</p>




	<a rel="nofollow"  href="http://stargrads.net/blogs/davinci/feed/" title="RSS"><img src="http://stargrads.net/common/images/handycons/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Programming%20exercise%3A%20maximum%20value%20in%20integer%20array%2C%20part%201&amp;body=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-maximum-value-in-integer-array-part-1%2F" title="email"><img src="http://stargrads.net/common/images/handycons/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-maximum-value-in-integer-array-part-1%2F&amp;t=Programming%20exercise%3A%20maximum%20value%20in%20integer%20array%2C%20part%201" title="Facebook"><img src="http://stargrads.net/common/images/handycons/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Programming%20exercise%3A%20maximum%20value%20in%20integer%20array%2C%20part%201%20-%20http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-maximum-value-in-integer-array-part-1%2F" title="Twitter"><img src="http://stargrads.net/common/images/handycons/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=Programming%20exercise%3A%20maximum%20value%20in%20integer%20array%2C%20part%201&amp;link=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-maximum-value-in-integer-array-part-1%2F" title="FriendFeed"><img src="http://stargrads.net/common/images/handycons/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-maximum-value-in-integer-array-part-1%2F&amp;title=Programming%20exercise%3A%20maximum%20value%20in%20integer%20array%2C%20part%201&amp;notes=In%20this%20post%2C%20I%20write%20a%20program%20to%20find%20the%20maximum%20value%20in%20an%20integer%20array%20in%20several%20programming%20languages." title="del.icio.us"><img src="http://stargrads.net/common/images/handycons/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-maximum-value-in-integer-array-part-1%2F&amp;title=Programming%20exercise%3A%20maximum%20value%20in%20integer%20array%2C%20part%201&amp;bodytext=In%20this%20post%2C%20I%20write%20a%20program%20to%20find%20the%20maximum%20value%20in%20an%20integer%20array%20in%20several%20programming%20languages." title="Digg"><img src="http://stargrads.net/common/images/handycons/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-maximum-value-in-integer-array-part-1%2F&amp;title=Programming%20exercise%3A%20maximum%20value%20in%20integer%20array%2C%20part%201&amp;annotation=In%20this%20post%2C%20I%20write%20a%20program%20to%20find%20the%20maximum%20value%20in%20an%20integer%20array%20in%20several%20programming%20languages." title="Google Bookmarks"><img src="http://stargrads.net/common/images/handycons/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://bookmarks.yahoo.com/toolbar/savebm?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-maximum-value-in-integer-array-part-1%2F&amp;t=Programming%20exercise%3A%20maximum%20value%20in%20integer%20array%2C%20part%201&opener=bm&amp;ei=UTF-8&amp;d=In%20this%20post%2C%20I%20write%20a%20program%20to%20find%20the%20maximum%20value%20in%20an%20integer%20array%20in%20several%20programming%20languages." title="Yahoo! Bookmarks"><img src="http://stargrads.net/common/images/handycons/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-maximum-value-in-integer-array-part-1%2F&amp;title=Programming%20exercise%3A%20maximum%20value%20in%20integer%20array%2C%20part%201" title="StumbleUpon"><img src="http://stargrads.net/common/images/handycons/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-maximum-value-in-integer-array-part-1%2F" title="Technorati"><img src="http://stargrads.net/common/images/handycons/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-maximum-value-in-integer-array-part-1%2F&amp;title=Programming%20exercise%3A%20maximum%20value%20in%20integer%20array%2C%20part%201" title="Reddit"><img src="http://stargrads.net/common/images/handycons/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>


<br/><br/><img src="http://stargrads.net/blogs/davinci/?ak_action=api_record_view&id=2145&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://stargrads.net/blogs/davinci/2009/09/programming-exercise-hello-world/' rel='bookmark' title='Permanent Link: Programming exercise: Hello, world!'>Programming exercise: Hello, world!</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/10/programming-exercise-permutations-of-a-string/' rel='bookmark' title='Permanent Link: Programming exercise: permutations of a string'>Programming exercise: permutations of a string</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/10/programming-exercise-combinations-of-a-string/' rel='bookmark' title='Permanent Link: Programming exercise: combinations of a string'>Programming exercise: combinations of a string</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://stargrads.net/blogs/davinci/2009/09/programming-exercise-maximum-value-in-integer-array-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming exercise: Hello, world!</title>
		<link>http://stargrads.net/blogs/davinci/2009/09/programming-exercise-hello-world/</link>
		<comments>http://stargrads.net/blogs/davinci/2009/09/programming-exercise-hello-world/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 16:22:50 +0000</pubDate>
		<dc:creator>davinci</dc:creator>
				<category><![CDATA[programming and technical issues]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[comparison of programming languages]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Maple]]></category>
		<category><![CDATA[Mathematica]]></category>
		<category><![CDATA[Matlab]]></category>
		<category><![CDATA[ML]]></category>
		<category><![CDATA[Octave]]></category>
		<category><![CDATA[Perl]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[programming exercises]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://stargrads.net/blogs/davinci/?p=2177</guid>
		<description><![CDATA[
The obligatory "Hello, world!" program in a variety of languages.
]]></description>
			<content:encoded><![CDATA[<p><!--adsensestart-->As a preliminary exercise to jog my memory, here is the obligatory &#8220;Hello, world!&#8221; program in a variety of languages.  On the one hand, the programs do nothing except output a string, and so don&#8217;t illustrate very much about the respective languages.  On the other hand, the simplicity of the task does illustrate the difference between languages that allow you to begin coding right away versus those that require a considerable amount of setting up (importing libraries, declaring classes, etc.) before you even have a functioning program<!--adsensestop--><span id="more-2177"></span>.</p>
<p>To be consistent, the programs will all output &#8220;Hello, world!&#8221;, followed by the newline character.</p>
<p>First up, of course, is C:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include&lt;stdio.h&gt;</span>
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Hello, world!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>C++ is similar, except that <code>iostream</code> is used instead of <code>stdio</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include&lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</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;Hello, world!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Java requires that a class be defined:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> HelloWorld <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello, world!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>C# looks like a mixture of C and Java:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> HelloWorld <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
        Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Hello, world!<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>In an interview situation, I doubt that I would use C# or Java, unless explicitly asked.  It wastes too much time to set up the classes.  Also, when writing code on a whiteboard or on paper, the shallower your indentation level, the better, because you don&#8217;t want to use up space unnecessarily, and you&#8217;ll have to move things around when you&#8217;re asked to add to or modify the code.</p>
<p>In Perl, this program is just one line:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;Hello, world!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The program is almost the same in Python:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Hello, world!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span></pre></td></tr></table></div>

<p>The only difference is that the statement in Python does not need to end in a semicolon (although it is legal, it is superfluous).  In Python 3 and above, print is a function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">print</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello, world!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>In Ruby:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Hello, world!<span style="color:#000099;">\n</span>&quot;</span></pre></td></tr></table></div>

<p>In Common Lisp:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;Hello, world!~%&quot;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>I&#8217;ll probably not be able to remember the format operators on the spot (e.g., &#8220;~%&#8221; instead of the much more common &#8220;\n&#8221; for &#8220;newline&#8221;).</p>
<p>In Scheme:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="scheme" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">display</span> <span style="color: #ff0000;">&quot;Hello, world!&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">newline</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>In Prolog:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="prolog" style="font-family:monospace;"><span style="color: #990000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Hello, world!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">nl</span><span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>In Haskell:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="haskell" style="font-family:monospace;"><span style="font-weight: bold;">putStrLn</span><span style="color: green;">&#40;</span><span style="background-color: #3cb371;">&quot;Hello, world!&quot;</span><span style="color: green;">&#41;</span></pre></td></tr></table></div>

<p>Note that <code>putStrLn</code> already appends a newline character.</p>
<p>In Standard ML:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="sml" style="font-family:monospace;">print<span style="color: #6c6;">&#40;</span><span style="color: #3cb371;">&quot;Hello, world!\n&quot;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">;</span></pre></td></tr></table></div>

<p>In the Linux shell (e.g., bash):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'Hello, world!'</span></pre></td></tr></table></div>

<p>The <code>echo</code> command outputs a trailing newline.</p>
<p>And now, for some mathematical languages.  In Matlab:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="matlab" style="font-family:monospace;"><span style="color: #0000FF;">fprintf</span><span style="color: #080;">&#40;</span>&quot;Hello, world!\n&quot;<span style="color: #080;">&#41;</span>;</pre></td></tr></table></div>

<p>In Octave (the above Matlab program will also work):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="octave" style="font-family:monospace;">printf<span style="color: #080;">&#40;</span>&quot;Hello, world!\n&quot;<span style="color: #080;">&#41;</span>;</pre></td></tr></table></div>

<p>In Maple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="maple" style="font-family:monospace;">print(`Hello, world!\n`);</pre></td></tr></table></div>

<p>In Mathematica:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="mathematica" style="font-family:monospace;">Print[&quot;Hello, world!\n&quot;]</pre></td></tr></table></div>

<p>I suppose that, in the mathematical languages, rather than print &#8220;Hello, world!&#8221; to the display, a true &#8220;Hello, world!&#8221; <em>program</em> would assign that value to a string, and then evaluate the string (to itself).</p>
<p>In an interview situation, I&#8217;ll probably use C/C++, because that&#8217;s the general-purpose programming language that I have the most experience with.  However, I think that Python is actually a better language for quickly mocking up working programs on the spot, so I&#8217;m going to practise using Python some more.</p>
<p>&#8211; davinci 11793</p>




	<a rel="nofollow"  href="http://stargrads.net/blogs/davinci/feed/" title="RSS"><img src="http://stargrads.net/common/images/handycons/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Programming%20exercise%3A%20Hello%2C%20world%21&amp;body=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-hello-world%2F" title="email"><img src="http://stargrads.net/common/images/handycons/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-hello-world%2F&amp;t=Programming%20exercise%3A%20Hello%2C%20world%21" title="Facebook"><img src="http://stargrads.net/common/images/handycons/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Programming%20exercise%3A%20Hello%2C%20world%21%20-%20http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-hello-world%2F" title="Twitter"><img src="http://stargrads.net/common/images/handycons/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=Programming%20exercise%3A%20Hello%2C%20world%21&amp;link=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-hello-world%2F" title="FriendFeed"><img src="http://stargrads.net/common/images/handycons/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-hello-world%2F&amp;title=Programming%20exercise%3A%20Hello%2C%20world%21&amp;notes=The%20obligatory%20%22Hello%2C%20world%21%22%20program%20in%20a%20variety%20of%20languages." title="del.icio.us"><img src="http://stargrads.net/common/images/handycons/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-hello-world%2F&amp;title=Programming%20exercise%3A%20Hello%2C%20world%21&amp;bodytext=The%20obligatory%20%22Hello%2C%20world%21%22%20program%20in%20a%20variety%20of%20languages." title="Digg"><img src="http://stargrads.net/common/images/handycons/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-hello-world%2F&amp;title=Programming%20exercise%3A%20Hello%2C%20world%21&amp;annotation=The%20obligatory%20%22Hello%2C%20world%21%22%20program%20in%20a%20variety%20of%20languages." title="Google Bookmarks"><img src="http://stargrads.net/common/images/handycons/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://bookmarks.yahoo.com/toolbar/savebm?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-hello-world%2F&amp;t=Programming%20exercise%3A%20Hello%2C%20world%21&opener=bm&amp;ei=UTF-8&amp;d=The%20obligatory%20%22Hello%2C%20world%21%22%20program%20in%20a%20variety%20of%20languages." title="Yahoo! Bookmarks"><img src="http://stargrads.net/common/images/handycons/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-hello-world%2F&amp;title=Programming%20exercise%3A%20Hello%2C%20world%21" title="StumbleUpon"><img src="http://stargrads.net/common/images/handycons/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-hello-world%2F" title="Technorati"><img src="http://stargrads.net/common/images/handycons/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fprogramming-exercise-hello-world%2F&amp;title=Programming%20exercise%3A%20Hello%2C%20world%21" title="Reddit"><img src="http://stargrads.net/common/images/handycons/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>


<br/><br/><img src="http://stargrads.net/blogs/davinci/?ak_action=api_record_view&id=2177&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://stargrads.net/blogs/davinci/2009/09/programming-exercise-maximum-value-in-integer-array-part-1/' rel='bookmark' title='Permanent Link: Programming exercise: maximum value in integer array, part 1'>Programming exercise: maximum value in integer array, part 1</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/10/programming-exercise-combinations-of-a-string/' rel='bookmark' title='Permanent Link: Programming exercise: combinations of a string'>Programming exercise: combinations of a string</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/10/programming-exercise-permutations-of-a-string/' rel='bookmark' title='Permanent Link: Programming exercise: permutations of a string'>Programming exercise: permutations of a string</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://stargrads.net/blogs/davinci/2009/09/programming-exercise-hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nostalgia: Old Computer Programming Software and Books</title>
		<link>http://stargrads.net/blogs/davinci/2009/09/nostalgia-old-computer-programming-software-and-books/</link>
		<comments>http://stargrads.net/blogs/davinci/2009/09/nostalgia-old-computer-programming-software-and-books/#comments</comments>
		<pubDate>Fri, 04 Sep 2009 19:25:01 +0000</pubDate>
		<dc:creator>davinci</dc:creator>
				<category><![CDATA[a life in books]]></category>
		<category><![CDATA[personal]]></category>
		<category><![CDATA[Adobe Acrobat]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[Borland]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[C++Builder]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[InterBase]]></category>
		<category><![CDATA[nostalgia]]></category>
		<category><![CDATA[Object Windows Library]]></category>
		<category><![CDATA[OWL]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Prolog]]></category>
		<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://stargrads.net/blogs/davinci/?p=1320</guid>
		<description><![CDATA[
A few photos of some old computer software and books.
]]></description>
			<content:encoded><![CDATA[<p>I had actually posted <a href="http://www.facebook.com/album.php?aid=4739&amp;id=100000085740395&amp;l=32af1b4246">this</a> to my <a href="http://www.facebook.com/dlyongemallo">Facebook</a> about two weeks ago.  At the time, I wrote:<br />
<blockquote>I recently threw out several boxes of old computer programming software and books, because they were obsolete, and I didn&#8217;t know anyone who wanted them. For nostalgia&#8217;s sake, I took photos of the stuff I threw out. It&#8217;s such a pity that commercial software and the associated books become outdated so quickly.</p></blockquote>
<p>I&#8217;m reposting the photos here<span id="more-1320"></span> since they do belong, in a way, in this series of posts on <a href="http://stargrads.net/blogs/davinci/2009/09/a-life-in-books/">the books that I&#8217;ve owned throughout my life</a>.</p>
<p>From left to right, the following pictures show: </p>
<ol>
<li>the books that came with Borland C++ v5;
</li>
<li>the books that came with Borland C++Builder v1 and v3;
</li>
<li>some books on C, Prolog, and InterBase;
</li>
<li>two books on C++Builder;
</li>
<li>more books on Borland C++, and a few other topics.
</li>
</ol>
<p><a href="http://img142.imageshack.us/img142/2415/p7240002.jpg" target="_blank"><img src="http://img142.imageshack.us/img142/2415/p7240002.th.jpg" border="0" /></a> <a href="http://img3.imageshack.us/img3/8058/p7240008.jpg" target="_blank"><img src="http://img3.imageshack.us/img3/8058/p7240008.th.jpg" border="0" /></a> <a href="http://img254.imageshack.us/img254/7915/p7240011.jpg" target="_blank"><img src="http://img254.imageshack.us/img254/7915/p7240011.th.jpg" border="0" /></a> <a href="http://img254.imageshack.us/img254/7402/p7240030.jpg" target="_blank"><img src="http://img254.imageshack.us/img254/7402/p7240030.th.jpg" border="0" /></a> <a href="http://img254.imageshack.us/img254/4608/p7250081.jpg" target="_blank"><img src="http://img254.imageshack.us/img254/4608/p7250081.th.jpg" border="0" /></a></p>
<p>I don&#8217;t have very much to say about the books that came with the Borland products, other than that I never used them very much, since pretty much the same content was also available through the Windows help system.  I have to wonder if <em>anyone</em> ever referred to their paper copies of these volumes, or if these were included merely to justify the prices.  On the other hand, the <a href="http://en.wikipedia.org/wiki/Object_Windows_Library">OWL</a> reference posters were both useful and aesthetically pleasing.  </p>
<p>As for the other books, they served their purpose.  I learned what I needed to from them at the time, but now they&#8217;re obsolete and are basically completely useless (except maybe to hobbyists who build or refurbish old computers for fun).</p>
<p>The following three pictures aren&#8217;t of books, but are related to the above: </p>
<ol>
<li>the box that Borland C++ v4 came in &#8212; unfortunately, I did not take a picture of the books inside, but it was full of them;
</li>
<li>I did, however, take a picture of the 22 floppy disks;
</li>
<li>Adobe Acrobat v4.
</li>
</ol>
<p><a href="http://img3.imageshack.us/img3/9439/p7250048.jpg" target="_blank"><img src="http://img3.imageshack.us/img3/9439/p7250048.th.jpg" border="0" /></a> <a href="http://img142.imageshack.us/img142/4391/p7240044.jpg" target="_blank"><img src="http://img142.imageshack.us/img142/4391/p7240044.th.jpg" border="0" /></a> <a href="http://img142.imageshack.us/img142/8113/p7240004.jpg" target="_blank"><img src="http://img142.imageshack.us/img142/8113/p7240004.th.jpg" border="0" /></a></p>
<p>&#8211; davinci 11770</p>




	<a rel="nofollow"  href="http://stargrads.net/blogs/davinci/feed/" title="RSS"><img src="http://stargrads.net/common/images/handycons/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Nostalgia%3A%20Old%20Computer%20Programming%20Software%20and%20Books&amp;body=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fnostalgia-old-computer-programming-software-and-books%2F" title="email"><img src="http://stargrads.net/common/images/handycons/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fnostalgia-old-computer-programming-software-and-books%2F&amp;t=Nostalgia%3A%20Old%20Computer%20Programming%20Software%20and%20Books" title="Facebook"><img src="http://stargrads.net/common/images/handycons/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Nostalgia%3A%20Old%20Computer%20Programming%20Software%20and%20Books%20-%20http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fnostalgia-old-computer-programming-software-and-books%2F" title="Twitter"><img src="http://stargrads.net/common/images/handycons/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=Nostalgia%3A%20Old%20Computer%20Programming%20Software%20and%20Books&amp;link=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fnostalgia-old-computer-programming-software-and-books%2F" title="FriendFeed"><img src="http://stargrads.net/common/images/handycons/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fnostalgia-old-computer-programming-software-and-books%2F&amp;title=Nostalgia%3A%20Old%20Computer%20Programming%20Software%20and%20Books&amp;notes=A%20few%20photos%20of%20some%20old%20computer%20software%20and%20books." title="del.icio.us"><img src="http://stargrads.net/common/images/handycons/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fnostalgia-old-computer-programming-software-and-books%2F&amp;title=Nostalgia%3A%20Old%20Computer%20Programming%20Software%20and%20Books&amp;bodytext=A%20few%20photos%20of%20some%20old%20computer%20software%20and%20books." title="Digg"><img src="http://stargrads.net/common/images/handycons/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fnostalgia-old-computer-programming-software-and-books%2F&amp;title=Nostalgia%3A%20Old%20Computer%20Programming%20Software%20and%20Books&amp;annotation=A%20few%20photos%20of%20some%20old%20computer%20software%20and%20books." title="Google Bookmarks"><img src="http://stargrads.net/common/images/handycons/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://bookmarks.yahoo.com/toolbar/savebm?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fnostalgia-old-computer-programming-software-and-books%2F&amp;t=Nostalgia%3A%20Old%20Computer%20Programming%20Software%20and%20Books&opener=bm&amp;ei=UTF-8&amp;d=A%20few%20photos%20of%20some%20old%20computer%20software%20and%20books." title="Yahoo! Bookmarks"><img src="http://stargrads.net/common/images/handycons/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fnostalgia-old-computer-programming-software-and-books%2F&amp;title=Nostalgia%3A%20Old%20Computer%20Programming%20Software%20and%20Books" title="StumbleUpon"><img src="http://stargrads.net/common/images/handycons/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fnostalgia-old-computer-programming-software-and-books%2F" title="Technorati"><img src="http://stargrads.net/common/images/handycons/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fnostalgia-old-computer-programming-software-and-books%2F&amp;title=Nostalgia%3A%20Old%20Computer%20Programming%20Software%20and%20Books" title="Reddit"><img src="http://stargrads.net/common/images/handycons/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>


<br/><br/><img src="http://stargrads.net/blogs/davinci/?ak_action=api_record_view&id=1320&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://stargrads.net/blogs/davinci/2009/09/programming-exercises-and-comparison-of-programming-languages/' rel='bookmark' title='Permanent Link: Programming exercises and comparison of programming languages'>Programming exercises and comparison of programming languages</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/09/a-life-in-books/' rel='bookmark' title='Permanent Link: A life in books'>A life in books</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2009/09/object-oriented-turing/' rel='bookmark' title='Permanent Link: Object Oriented Turing'>Object Oriented Turing</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://stargrads.net/blogs/davinci/2009/09/nostalgia-old-computer-programming-software-and-books/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mock-up of a Yudit-like mobile application</title>
		<link>http://stargrads.net/blogs/davinci/2009/09/mock-up-of-a-yudit-like-mobile-application/</link>
		<comments>http://stargrads.net/blogs/davinci/2009/09/mock-up-of-a-yudit-like-mobile-application/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 02:50:41 +0000</pubDate>
		<dc:creator>davinci</dc:creator>
				<category><![CDATA[personal]]></category>
		<category><![CDATA[programming and technical issues]]></category>
		<category><![CDATA[BlackBerry]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Cangjie]]></category>
		<category><![CDATA[cellphone]]></category>
		<category><![CDATA[Chinese]]></category>
		<category><![CDATA[Devanagari]]></category>
		<category><![CDATA[Farsi]]></category>
		<category><![CDATA[g11n]]></category>
		<category><![CDATA[globalisation]]></category>
		<category><![CDATA[globalization]]></category>
		<category><![CDATA[Hindi]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[input methods]]></category>
		<category><![CDATA[internationalisation]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[J2ME]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[L10n]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[localisation]]></category>
		<category><![CDATA[localization]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[National Language Support]]></category>
		<category><![CDATA[Native Language Support]]></category>
		<category><![CDATA[NLS]]></category>
		<category><![CDATA[Persian]]></category>
		<category><![CDATA[porting]]></category>
		<category><![CDATA[RIM]]></category>
		<category><![CDATA[SCIM]]></category>
		<category><![CDATA[Sun]]></category>
		<category><![CDATA[Unicode editor]]></category>
		<category><![CDATA[Yudit]]></category>

		<guid isPermaLink="false">http://stargrads.net/blogs/davinci/?p=1265</guid>
		<description><![CDATA[
In this post, I describe a brief experiment to write a Yudit-like application for a mobile environment.
]]></description>
			<content:encoded><![CDATA[<p>One application that I always look for in a mobile device is a multilingual dictionary.  If one is not available, I can make do with support for multiple input methods (such as through <a href="http://en.wikipedia.org/wiki/Smart_Common_Input_Method">SCIM</a>), access to the internet, and a decent web browser (one that handles non-Latin fonts and right-to-left scripts).</p>
<p>I often end up using a program called <a href="http://www.yudit.org/">Yudit</a>, a Unicode editor written by Gáspár Sinai, even on a system that has native integrated support for multiple input methods, because it&#8217;s available on a wide range of systems and I&#8217;m familiar with the input methods bundled with it.  For example, even though the same input method is supposedly available on both Microsoft Windows and through SCIM, there may be slight differences in the keyboard layouts that can result in typos if one is not careful.</p>
<p>It seems that the majority of wireless handheld devices ship with only one input method.  Yudit does not appear to run on any of the major mobile operating systems<span id="more-1265"></span>, but it would be overkill anyway, since people aren&#8217;t likely to want to edit entire Unicode documents on their cell phones.  On the other hand, it would be useful to be able to switch input methods, even if only to type a few words into an e-mail, or to look something up in an online dictionary.</p>
<p>In adapting input methods designed for personal computers to cellphones, one immediately runs into the problem that what would have been a single keystroke on a full-sized keyboard becomes a key combination or a series of taps on a keypad.  With the <a href="http://en.wikipedia.org/wiki/Predictive_text">predictive text</a> systems used on some cellphones, one also runs into the problem of <a href="http://en.wikipedia.org/wiki/Predictive_text#Textonyms">textonyms</a>.</p>
<p>I wanted to see if I could port some of Yudit&#8217;s input methods to a mobile environment, so I ignored these problems for the moment and assumed, for the sake of simplicity, that the device had a full-sized keyboard.  I also decided to write a Java <a href="http://en.wikipedia.org/wiki/MIDlet">MIDlet</a>, because these can be run on a number of different mobile platforms.  (Yudit itself is written in C++.)</p>
<p>Even this simplified version of the problem turned out not to be so easy.  I tried using the <a href="http://java.sun.com/javame/reference/apis/jsr037/javax/microedition/lcdui/TextField.html"><code>TextField</code></a> and <a href="http://java.sun.com/javame/reference/apis/jsr037/javax/microedition/lcdui/TextBox.html"><code>TextBox</code></a> classes in the <a href="http://java.sun.com/javame/reference/apis/jsr037/javax/microedition/lcdui/package-summary.html"><code>javax.microedition.lcdui</code></a> package, and while I was able to capture the user&#8217;s input and replace it with the appropriate Unicode, neither class handled bidirectional text very well.  </p>
<p>I tried mixing the Chinese-CJ (Cangjie), Devanagari, and Farsi input methods (used for entering Chinese, Hindi, and Persian text, respectively), and the result was that my little program became very confused.  On some emulated devices, the left and right arrow keys would move the cursor left and right, respectively, while within the Persian text; but on others, the effects of these keys were reversed, as were the effects of the backspace and delete keys.</p>
<p>It would probably take a lot more work to get any of these input methods working properly, but this was only a first attempt, which I wanted to document in case anyone else wanted to try something similar.  Also, it&#8217;s possible that I would have been more successful if I had used UI libraries specific to a company or manufacturer, such as RIM&#8217;s <a href="http://www.blackberry.com/developers/docs/4.2api/net/rim/device/api/ui/package-summary.html"><code>net.rim.device.api.ui</code></a> package, rather than the generic (and hence not as powerful) <code>javax.microedition.lcdui</code> package. </p>
<p>I&#8217;ve included several screenshots of what my little experiment looks like when run on Sun&#8217;s Java ME SDK mobile emulator with a generic device, as well as on RIM&#8217;s BlackBerry emulator with two different devices.  The Devanagari did not render at all on the emulated BlackBerry devices.  There were also some small layout problems, which I didn&#8217;t fix since this was only supposed to be a quick experiment.</p>
<p>Screenshots of experiment on Sun&#8217;s generic emulated mobile phone:<br />
<a href="http://img26.imageshack.us/img26/946/sun1a.jpg" target="_blank"><img src="http://img26.imageshack.us/img26/946/sun1a.th.jpg" border="0" /></a> <a href="http://img25.imageshack.us/img25/5093/sun2g.jpg" target="_blank"><img src="http://img25.imageshack.us/img25/5093/sun2g.th.jpg" border="0" /></a> <a href="http://img527.imageshack.us/img527/821/sun3.jpg" target="_blank"><img src="http://img527.imageshack.us/img527/821/sun3.th.jpg" border="0" /></a> <a href="http://img527.imageshack.us/img527/5495/sun4w.jpg" target="_blank"><img src="http://img25.imageshack.us/img25/1512/sun4x.th.jpg" border="0" /></a></p>
<p>Screenshots of experiment on RIM&#8217;s emulated <a href="http://na.blackberry.com/eng/devices/blackberrycurve8300/">BlackBerry Curve 8300</a>:<br />
<a href="http://img26.imageshack.us/img26/9814/blackberry83001.jpg" target="_blank"><img src="http://img26.imageshack.us/img26/9814/blackberry83001.th.jpg" border="0" /></a> <a href="http://img25.imageshack.us/img25/8673/blackberry83002.jpg" target="_blank"><img src="http://img25.imageshack.us/img25/8673/blackberry83002.th.jpg" border="0" /></a> <a href="http://img26.imageshack.us/img26/9484/blackberry83003.jpg" target="_blank"><img src="http://img26.imageshack.us/img26/9484/blackberry83003.th.jpg" border="0" /></a> <a href="http://img26.imageshack.us/img26/806/blackberry83004.jpg" target="_blank"><img src="http://img26.imageshack.us/img26/806/blackberry83004.th.jpg" border="0" /></a></p>
<p>Screenshots of experiment on RIM&#8217;s emulated <a href="http://na.blackberry.com/eng/devices/blackberrytour/">BlackBerry Tour 9630</a>:<br />
<a href="http://img26.imageshack.us/img26/1907/blackberry96301.jpg" target="_blank"><img src="http://img26.imageshack.us/img26/1907/blackberry96301.th.jpg" border="0" /></a> <a href="http://img26.imageshack.us/img26/5524/blackberry96302.jpg" target="_blank"><img src="http://img26.imageshack.us/img26/5524/blackberry96302.th.jpg" border="0" /></a> <a href="http://img25.imageshack.us/img25/6650/blackberry96303.jpg" target="_blank"><img src="http://img25.imageshack.us/img25/6650/blackberry96303.th.jpg" border="0" /></a> <a href="http://img25.imageshack.us/img25/927/blackberry96304.jpg" target="_blank"><img src="http://img25.imageshack.us/img25/927/blackberry96304.th.jpg" border="0" alt="Free Image Hosting at www.ImageShack.us" /></a></p>
<p>Considering the size of the mobile device markets in China, India, and the Middle East, getting different input methods to work in a mobile environment is an important (and potentially financially rewarding) problem to tackle.</p>
<p>&#8211; davinci</p>




	<a rel="nofollow"  href="http://stargrads.net/blogs/davinci/feed/" title="RSS"><img src="http://stargrads.net/common/images/handycons/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="mailto:?subject=Mock-up%20of%20a%20Yudit-like%20mobile%20application&amp;body=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fmock-up-of-a-yudit-like-mobile-application%2F" title="email"><img src="http://stargrads.net/common/images/handycons/email_link.png" title="email" alt="email" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fmock-up-of-a-yudit-like-mobile-application%2F&amp;t=Mock-up%20of%20a%20Yudit-like%20mobile%20application" title="Facebook"><img src="http://stargrads.net/common/images/handycons/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Mock-up%20of%20a%20Yudit-like%20mobile%20application%20-%20http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fmock-up-of-a-yudit-like-mobile-application%2F" title="Twitter"><img src="http://stargrads.net/common/images/handycons/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.friendfeed.com/share?title=Mock-up%20of%20a%20Yudit-like%20mobile%20application&amp;link=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fmock-up-of-a-yudit-like-mobile-application%2F" title="FriendFeed"><img src="http://stargrads.net/common/images/handycons/friendfeed.png" title="FriendFeed" alt="FriendFeed" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fmock-up-of-a-yudit-like-mobile-application%2F&amp;title=Mock-up%20of%20a%20Yudit-like%20mobile%20application&amp;notes=In%20this%20post%2C%20I%20describe%20a%20brief%20experiment%20to%20write%20a%20Yudit-like%20application%20for%20a%20mobile%20environment." title="del.icio.us"><img src="http://stargrads.net/common/images/handycons/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fmock-up-of-a-yudit-like-mobile-application%2F&amp;title=Mock-up%20of%20a%20Yudit-like%20mobile%20application&amp;bodytext=In%20this%20post%2C%20I%20describe%20a%20brief%20experiment%20to%20write%20a%20Yudit-like%20application%20for%20a%20mobile%20environment." title="Digg"><img src="http://stargrads.net/common/images/handycons/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fmock-up-of-a-yudit-like-mobile-application%2F&amp;title=Mock-up%20of%20a%20Yudit-like%20mobile%20application&amp;annotation=In%20this%20post%2C%20I%20describe%20a%20brief%20experiment%20to%20write%20a%20Yudit-like%20application%20for%20a%20mobile%20environment." title="Google Bookmarks"><img src="http://stargrads.net/common/images/handycons/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://bookmarks.yahoo.com/toolbar/savebm?u=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fmock-up-of-a-yudit-like-mobile-application%2F&amp;t=Mock-up%20of%20a%20Yudit-like%20mobile%20application&opener=bm&amp;ei=UTF-8&amp;d=In%20this%20post%2C%20I%20describe%20a%20brief%20experiment%20to%20write%20a%20Yudit-like%20application%20for%20a%20mobile%20environment." title="Yahoo! Bookmarks"><img src="http://stargrads.net/common/images/handycons/yahoomyweb.png" title="Yahoo! Bookmarks" alt="Yahoo! Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fmock-up-of-a-yudit-like-mobile-application%2F&amp;title=Mock-up%20of%20a%20Yudit-like%20mobile%20application" title="StumbleUpon"><img src="http://stargrads.net/common/images/handycons/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fmock-up-of-a-yudit-like-mobile-application%2F" title="Technorati"><img src="http://stargrads.net/common/images/handycons/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fstargrads.net%2Fblogs%2Fdavinci%2F2009%2F09%2Fmock-up-of-a-yudit-like-mobile-application%2F&amp;title=Mock-up%20of%20a%20Yudit-like%20mobile%20application" title="Reddit"><img src="http://stargrads.net/common/images/handycons/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>


<br/><br/><img src="http://stargrads.net/blogs/davinci/?ak_action=api_record_view&id=1265&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://stargrads.net/blogs/davinci/2009/11/persian-soft-keyboard-and-applications-for-android/' rel='bookmark' title='Permanent Link: Persian Soft Keyboard and Applications for Android'>Persian Soft Keyboard and Applications for Android</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://stargrads.net/blogs/davinci/2009/09/mock-up-of-a-yudit-like-mobile-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->