<?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; map</title>
	<atom:link href="http://stargrads.net/blogs/davinci/tag/map/feed/" rel="self" type="application/rss+xml" />
	<link>http://stargrads.net/blogs/davinci</link>
	<description>everything is an experiment</description>
	<lastBuildDate>Mon, 21 Mar 2011 18:31:14 +0000</lastBuildDate>
	<language>fa</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>The geographic and temporal spread argument, part 19</title>
		<link>http://stargrads.net/blogs/davinci/2010/11/the-geographic-and-temporal-spread-argument-part-19/</link>
		<comments>http://stargrads.net/blogs/davinci/2010/11/the-geographic-and-temporal-spread-argument-part-19/#comments</comments>
		<pubDate>Tue, 09 Nov 2010 03:27:26 +0000</pubDate>
		<dc:creator>davinci</dc:creator>
				<category><![CDATA[reflections on religion]]></category>
		<category><![CDATA[religion]]></category>
		<category><![CDATA[apologetics]]></category>
		<category><![CDATA[Arabic]]></category>
		<category><![CDATA[Christianity]]></category>
		<category><![CDATA[creationism]]></category>
		<category><![CDATA[double standard]]></category>
		<category><![CDATA[expertise]]></category>
		<category><![CDATA[geographic information]]></category>
		<category><![CDATA[Ghazali]]></category>
		<category><![CDATA[intelligent design]]></category>
		<category><![CDATA[Islam]]></category>
		<category><![CDATA[Judaism]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[linguistics]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[maps]]></category>
		<category><![CDATA[Plantinga]]></category>
		<category><![CDATA[reason]]></category>
		<category><![CDATA[Torah]]></category>
		<category><![CDATA[Why I am not a non-Buddhist]]></category>

		<guid isPermaLink="false">http://stargrads.net/blogs/davinci/?p=3447</guid>
		<description><![CDATA[
In this post, I continue to explain how the evidence contradicts some religious beliefs, and why a broad education and the ability to reason are more relevant than expertise in a specific field to determining whether any religion is true.
]]></description>
			<content:encoded><![CDATA[<p>(Part 18 is <a href="http://stargrads.net/blogs/davinci/2010/11/the-geographic-and-temporal-spread-argument-part-18/">here</a>.)</p>
<p>Any religious argument based on expertise in a language is necessarily unconvincing to someone who doesn&#8217;t already consider that language to be <a href="http://en.wikipedia.org/wiki/Sacred_language">special or holy</a>.  Numerous languages have been asserted by various religions to have special or divine properties: Sanskrit, Pali, Chinese, Hebrew, Aramaic, Greek, Latin, Arabic, Tibetan, Persian, and <a href="http://en.wikipedia.org/wiki/King_James_Only_movement">even English</a>.  The question of linguistic competence is often brought up by religious apologists seeking to deflect issues raised by skeptics about problematic passages in their scriptures, but when good translations and linguistic resources are available, this is just a smokescreen.  When there are errors in fact or reasoning in a translation, or instances of morally reprehensible behaviour, the problems are rarely made better by referring to the primary text in the original language<span id="more-3447"></span>.  (In fact, the problems are sometimes worse, as religious translators attempt to minimise or downplay those aspects of the text which they think might offend their target audience.)  </p>
<p><!--adsensestart-->The fact that a lack of linguistic expertise is not actually a real issue with many religious apologists is made evident by the double standard of insisting on linguistic qualifications from critics of their scriptures, while criticising the scriptures of others without studying those in their original language.  For example, no Muslim apologist has ever refrained from arguing that the Torah has been corrupted, in spite of not being able to read Hebrew and not having any substantial knowledge of the history of Jewish scripture.  Similarly, many of the popular apologists for various religions are not trained in the study of religion, nor in the fields whose findings they claim support their religious beliefs.</p>
<p>Linguistic expertise, or indeed expertise in any field, is nowhere as important as demonstrating <em>the ability to reason</em>.  For example, if I claimed that the arguments made by the Indian <a href="http://en.wikipedia.org/wiki/Buddhist_philosophy">Buddhist philosophers</a> against monotheism are absolutely devastating, no monotheist would accept that they are disqualified from rebutting these arguments on the grounds that they cannot read Sanskrit.  Arguments must stand or fall on their own merits, regardless of which language they are expressed in.  Linguistic expertise may allow a religious believer to gain a deeper understanding of the texts of his or her religion, but it can never defend his or her beliefs against criticisms made on the basis of logic or facts.</p>
<p>In the intersection between history and language, we find the story of the <a href="http://en.wikipedia.org/wiki/Tower_of_Babel">tower of Babel</a> (Gen. 11:1&#8211;8).  In order for this story to be true, it would have to be possible to build a skyscraper using nothing but Bronze Age brick technology. Any material sturdy enough to support a tower that reached to the sky would also be durable enough to leave behind some evidence for the present day, but no such evidence exists, and it is difficult to believe that the technology was used in just the one tower if it had existed.  Furthermore, we humans <em>have</em> subsequently built towers reaching into the sky.  Indeed, we have <a href="http://en.wikipedia.org/wiki/Tokyo">entire</a> <a href="http://en.wikipedia.org/wiki/Hong_Kong">cities</a> full of them.  We have even left the confines of the Earth&#8217;s surface and walked on the Moon.  And yet none of these developments brought about any visible divine intervention.  Instead, they are the products of science and engineering.</p>
<p>So, even though the spotlight is on biology as a field where science and religion are in conflict, it is not the only one.  I have discussed some examples from history and linguistics above, but there are many others.  The point is that the more <em>broadly</em> educated a person is, the more <em>obvious</em> it becomes that every religious belief can be explained as a human product of its time and place, without recourse to divine intervention.  In order to maintain a belief in the literal inerrancy of the Bible, it is not sufficient merely to assert <a href="http://en.wikipedia.org/wiki/Intelligent_design">intelligent design</a> creationism as an alternative to the biological theory of evolution.  One must also propose alternate theories to the standard theories in the fields of archeology, linguistics, history, hydrology, geology, astronomy, material science, and so on.</p>
<p>Religious believers cannot accuse a skeptic of ignoring the evidence.  As I hope I have shown, the <em>more</em> evidence one examines, the stronger the case becomes that religion is a human invention.  Conversely, religious believers often do not examine the evidence themselves, instead relying on people they presume to be experts.  But even if there are experts in Middle Eastern history, say, who are fervent Christians, or experts in classical Arabic who are devout Muslims, there are people who are equally expert in those subjects who believe in other religions, or in no religion.  Furthermore, an expert in Middle Eastern history may be quite ignorant of another area in which the evidence demonstrates that their beliefs are false, and no one can be an expert in every field touching upon their religious beliefs.  </p>
<p>When religious believers defer to supposed authorities as their reason for believing that their religion is true, they&#8217;re essentially admitting that they do not consider themselves qualified to judge the evidence and arguments for themselves.  But in that case, they would also be unqualified to judge whether the evidence is actually incompatible with their beliefs or whether the arguments allegedly supporting their beliefs are wrong.  When religious believers cite an authority whose arguments they do not themselves understand as the justification for their beliefs, the discussion is basically at an impasse.  For example, there are what I think are serious problems or errors in the arguments for monotheism made by <a href="http://en.wikipedia.org/wiki/Ghazali">Ghazālī</a> and <a href="http://en.wikipedia.org/wiki/Alvin_Plantinga">Plantinga</a>.  If I&#8217;m right and their arguments are wrong, then citing these authorities has done nothing to convince me.  However, if I&#8217;m wrong and their arguments are right, then I&#8217;m simply incapable of seeing how they could be right, and thus I am not convinced by their arguments in that case either.</p>
<p>This is the reason that the <em>geographic</em> and <em>temporal</em> aspects of the evidence that religion is a human invention are so powerful.  It is <em>so easy to explain to anyone</em>: just plot the spread of the world&#8217;s religions on a series of maps corresponding to successive instances in time.  When you do so, it can be <em>clearly seen by anyone</em> that religious beliefs and ideas are spread by human migration, conquest, and contact.  You do not have to be an expert to immediately notice that there are no instances of &#8220;misplaced jinn&#8221;, that is, reports of human interactions with supernatural beings in a place and time where humans did not already believe that such beings exist.  The simplest and best explanation for this readily observable fact is that supernatural beings such as gods, or angels, or demons, or jinn, do not exist outside of the human imagination.  </p>
<p>This is not to say that the <em>phenomena</em> which they were concocted to explain do not exist, such as people hearing voices, seeing strange lights, feeling a presence that is not visible, getting spontaneously cured of diseases, becoming inexplicably ill, and so on.  In fact, given that these phenomena <em>do</em> exist and need explaining, it is easy to see how people all over the world came up with the idea of supernatural beings, or witchcraft.  And, for a person who is surrounded only by those who share the same view of the supernatural world (which would be most people in history), the explanation is good enough.  The problem only arises when you begin to <em>compare</em> what different religions teach about the supernatural world, and examine how the descriptions of the supernatural world coming from a particular time and place just happen to correspond exactly with the religious composition and dispositions of the people living there.  This is something that only a few scholars would have had the privilege to even be in a position to notice in a hundred years ago, but with the spread of libraries and the Internet, it is something that many people now can see for themselves.<!--adsensestop--></p>
<p>(Continued in part 20 <a href="http://stargrads.net/blogs/davinci/2010/11/the-geographic-and-temporal-spread-argument-part-20/">here</a>.)</p>
<p>&#8211; davinci</p>
<img src="http://stargrads.net/blogs/davinci/?ak_action=api_record_view&id=3447&type=feed" alt="" /><p>Related posts:<ol>
<li><a href='http://stargrads.net/blogs/davinci/2010/11/the-geographic-and-temporal-spread-argument-part-16/' rel='bookmark' title='The geographic and temporal spread argument, part 16'>The geographic and temporal spread argument, part 16</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2010/10/the-geographic-and-temporal-spread-argument-part-7/' rel='bookmark' title='The geographic and temporal spread argument, part 7'>The geographic and temporal spread argument, part 7</a></li>
<li><a href='http://stargrads.net/blogs/davinci/2010/11/the-geographic-and-temporal-spread-argument-part-18/' rel='bookmark' title='The geographic and temporal spread argument, part 18'>The geographic and temporal spread argument, part 18</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://stargrads.net/blogs/davinci/2010/11/the-geographic-and-temporal-spread-argument-part-19/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: #b1b100;">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: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null?</span> the<span style="color: #66cc66;">-</span><span style="color: #b1b100;">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: #b1b100;">equal?</span> the<span style="color: #66cc66;">-</span>item <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> the<span style="color: #66cc66;">-</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cdr</span> the<span style="color: #66cc66;">-</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">car</span> the<span style="color: #66cc66;">-</span><span style="color: #b1b100;">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: #b1b100;">cdr</span> the<span style="color: #66cc66;">-</span><span style="color: #b1b100;">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: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>permute the<span style="color: #66cc66;">-</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null?</span> the<span style="color: #66cc66;">-</span><span style="color: #b1b100;">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: #b1b100;">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: #b1b100;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">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: #b1b100;">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: #b1b100;">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: #b1b100;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">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: #b1b100;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">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: #b1b100;">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: #b1b100;">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: #b1b100;">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: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>permute the<span style="color: #66cc66;">-</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">null?</span> the<span style="color: #66cc66;">-</span><span style="color: #b1b100;">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: #b1b100;">append</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">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: #b1b100;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">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: #b1b100;">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: #b1b100;">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: #b1b100;">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: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>permutations the<span style="color: #66cc66;">-</span><span style="color: #b1b100;">string</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">s</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>apply <span style="color: #b1b100;">string</span> <span style="color: #b1b100;">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;">-&amp;</span>gt<span style="color: #808080; font-style: italic;">;list the-string)))</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">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: #b1b100;">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&amp;lt;iostream&amp;gt;</span>
<span style="color: #339900;">#include&amp;lt;string.h&amp;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: #000040;">&amp;</span>lt<span style="color: #008080;">;</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: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> output <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</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: #000040;">&amp;</span>lt<span style="color: #008080;">;</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: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</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>
<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='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='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='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: #b1b100;">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: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">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: #b1b100;">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: #b1b100;">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: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>subset<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">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: #b1b100;">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: #b1b100;">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: #b1b100;">define</span> <span style="color: #66cc66;">&#40;</span>combinations the<span style="color: #66cc66;">-</span><span style="color: #b1b100;">string</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">s</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>apply <span style="color: #b1b100;">string</span> <span style="color: #b1b100;">s</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 
         <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">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;">-&amp;</span>gt<span style="color: #808080; font-style: italic;">;list the-string))) )</span>
<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">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: #b1b100;">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&amp;lt;iostream&amp;gt;</span>
<span style="color: #339900;">#include&amp;lt;string.h&amp;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: #000040;">&amp;</span>lt<span style="color: #008080;">;</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: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> output <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</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>
<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='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='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='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>
	</channel>
</rss>

