<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Small Programming Challenge no. 5 &#8211; Generating a Permutation</title>
	<atom:link href="http://www.algorithm.co.il/blogs/programming/small-programming-challenge-no-5-generating-a-permutation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Tue, 21 Jun 2011 21:07:08 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
	<item>
		<title>By: Naccache</title>
		<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/#comment-285</link>
		<dc:creator>Naccache</dc:creator>
		<pubDate>Thu, 21 Jan 2010 18:07:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=493#comment-285</guid>
		<description>There are plenty of algorithms for doing this and even a book written on how to do it called &quot;constructive combinatorics&quot; by Stanton and White.</description>
		<content:encoded><![CDATA[<p>There are plenty of algorithms for doing this and even a book written on how to do it called &#8220;constructive combinatorics&#8221; by Stanton and White.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rani</title>
		<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/#comment-284</link>
		<dc:creator>Rani</dc:creator>
		<pubDate>Sat, 28 Nov 2009 22:13:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=493#comment-284</guid>
		<description>Well, by definition you need O(n log n) time just to read num, unless you consider machines with registers of length log n.

I&#039;d go with an algorithm similar to Amir&#039;s, just replacing &quot;while... n--&quot; with a for loop, but IMHO the more interesting question is how to define a Gray code on permutations, and one of the famous solutions here is the Johnson-Trotter algorithm (http://en.wikipedia.org/wiki/Steinhaus%E2%80%93Johnson%E2%80%93Trotter_algorithm ).</description>
		<content:encoded><![CDATA[<p>Well, by definition you need O(n log n) time just to read num, unless you consider machines with registers of length log n.</p>
<p>I&#8217;d go with an algorithm similar to Amir&#8217;s, just replacing &#8220;while&#8230; n&#8211;&#8221; with a for loop, but IMHO the more interesting question is how to define a Gray code on permutations, and one of the famous solutions here is the Johnson-Trotter algorithm (<a href="http://en.wikipedia.org/wiki/Steinhaus%E2%80%93Johnson%E2%80%93Trotter_algorithm" rel="nofollow">http://en.wikipedia.org/wiki/Steinhaus%E2%80%93Johnson%E2%80%93Trotter_algorithm</a> ).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eric</title>
		<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/#comment-283</link>
		<dc:creator>eric</dc:creator>
		<pubDate>Fri, 27 Nov 2009 13:51:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=493#comment-283</guid>
		<description>I add a mistake in my code , sorry

here is my last, most-compact shot:
[python]
def perm(n, m):
    s  = range(n)
    for j in range(1,n):
        m, r = divmod(m,j+1)
        s[r], s[j] = s[j], s[ r ]
    return tuple(s)

def tester( f, k ):
    &quot;&quot;&quot; a simple tester function just call tester( permutation_function, k) &quot;&quot;&quot;
    p = [f(k, i) for i in range(fact(k) ) ]
    return len(set(p) ) == fact(k)

assert all([ tester(perm, i) for i in range(1,10)  ])

[/python]

sorry for the lazy test before.

and btw here is the &quot;solution&quot;
http://en.wikipedia.org/wiki/Permutation#Algorithms_to_generate_permutations</description>
		<content:encoded><![CDATA[<p>I add a mistake in my code , sorry</p>
<p>here is my last, most-compact shot:<br />
[python]<br />
def perm(n, m):<br />
    s  = range(n)<br />
    for j in range(1,n):<br />
        m, r = divmod(m,j+1)<br />
        s[r], s[j] = s[j], s[ r ]<br />
    return tuple(s)</p>
<p>def tester( f, k ):<br />
    &#8220;&#8221;" a simple tester function just call tester( permutation_function, k) &#8220;&#8221;"<br />
    p = [f(k, i) for i in range(fact(k) ) ]<br />
    return len(set(p) ) == fact(k)</p>
<p>assert all([ tester(perm, i) for i in range(1,10)  ])</p>
<p>[/python]</p>
<p>sorry for the lazy test before.</p>
<p>and btw here is the &#8220;solution&#8221;<br />
<a href="http://en.wikipedia.org/wiki/Permutation#Algorithms_to_generate_permutations" rel="nofollow">http://en.wikipedia.org/wiki/Permutation#Algorithms_to_generate_permutations</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jy-ce</title>
		<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/#comment-282</link>
		<dc:creator>Jy-ce</dc:creator>
		<pubDate>Tue, 24 Nov 2009 17:05:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=493#comment-282</guid>
		<description>This solution allows a permutation of any type of list!

[Python]
def permut(values, m):
	n = len(values)
	return [values.pop(m%i) for i in range(n, 0, -1)]

for i in range(2*3*4):
	print permut(list(&quot;abcd&quot;), i)
[/Python]</description>
		<content:encoded><![CDATA[<p>This solution allows a permutation of any type of list!</p>
<p>[Python]<br />
def permut(values, m):<br />
	n = len(values)<br />
	return [values.pop(m%i) for i in range(n, 0, -1)]</p>
<p>for i in range(2*3*4):<br />
	print permut(list(&#8220;abcd&#8221;), i)<br />
[/Python]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jy-ce</title>
		<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/#comment-281</link>
		<dc:creator>Jy-ce</dc:creator>
		<pubDate>Tue, 24 Nov 2009 17:00:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=493#comment-281</guid>
		<description>The simpliest solution - similar as eric one - based on the &quot;pop&quot; function of Python lists and on the &quot;modulo&quot; operator (no divmod)

[Python]
def permut(n, m):
	values = range(n)
	return [values.pop(m%i) for i in range(n, 0, -1)]
[/Python]</description>
		<content:encoded><![CDATA[<p>The simpliest solution &#8211; similar as eric one &#8211; based on the &#8220;pop&#8221; function of Python lists and on the &#8220;modulo&#8221; operator (no divmod)</p>
<p>[Python]<br />
def permut(n, m):<br />
	values = range(n)<br />
	return [values.pop(m%i) for i in range(n, 0, -1)]<br />
[/Python]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: yuval</title>
		<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/#comment-280</link>
		<dc:creator>yuval</dc:creator>
		<pubDate>Tue, 17 Nov 2009 00:23:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=493#comment-280</guid>
		<description>My solution including doctests at: http://pastebin.com/f4c717118

Some of the solutions got a bit complexified, I think I kept it minimalistic, though it&#039;s the same algo...</description>
		<content:encoded><![CDATA[<p>My solution including doctests at: <a href="http://pastebin.com/f4c717118" rel="nofollow">http://pastebin.com/f4c717118</a></p>
<p>Some of the solutions got a bit complexified, I think I kept it minimalistic, though it&#8217;s the same algo&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Enumerating permutations &#171; cplusplus.co.il</title>
		<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/#comment-279</link>
		<dc:creator>Enumerating permutations &#171; cplusplus.co.il</dc:creator>
		<pubDate>Sat, 14 Nov 2009 10:04:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=493#comment-279</guid>
		<description>[...] by rmn    There are exactly n! different permutations of n numbers. This challenge was about writing a function which is able to enumerate all these permutations, i.e. function [...]</description>
		<content:encoded><![CDATA[<p>[...] by rmn    There are exactly n! different permutations of n numbers. This challenge was about writing a function which is able to enumerate all these permutations, i.e. function [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rmn</title>
		<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/#comment-278</link>
		<dc:creator>rmn</dc:creator>
		<pubDate>Sat, 14 Nov 2009 09:46:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=493#comment-278</guid>
		<description>I&#039;ll post a solution as well, in a few minutes :)
Haven&#039;t read the others though

Very nice challenge, it kept me thinking for quite a while!</description>
		<content:encoded><![CDATA[<p>I&#8217;ll post a solution as well, in a few minutes :)<br />
Haven&#8217;t read the others though</p>
<p>Very nice challenge, it kept me thinking for quite a while!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Amir</title>
		<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/#comment-277</link>
		<dc:creator>Amir</dc:creator>
		<pubDate>Fri, 13 Nov 2009 16:26:21 +0000</pubDate>
		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=493#comment-277</guid>
		<description>We could construct the result at the beginning and swap in-place:
[CODE]
def permute(n, p):
    result = range(1, 1 + n)
    while n&gt; 0:
        p, swap_index = divmod(p, n)
        n -= 1
        result[n], result[swap_index] = result[swap_index], result[n]
    return result
[/CODE]</description>
		<content:encoded><![CDATA[<p>We could construct the result at the beginning and swap in-place:<br />
[CODE]<br />
def permute(n, p):<br />
    result = range(1, 1 + n)<br />
    while n&gt; 0:<br />
        p, swap_index = divmod(p, n)<br />
        n -= 1<br />
        result[n], result[swap_index] = result[swap_index], result[n]<br />
    return result<br />
[/CODE]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rouli</title>
		<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/#comment-276</link>
		<dc:creator>rouli</dc:creator>
		<pubDate>Fri, 13 Nov 2009 14:59:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=493#comment-276</guid>
		<description>yeah, looking back, I don&#039;t have a clue why I opted for the more convoluted way to compute it.</description>
		<content:encoded><![CDATA[<p>yeah, looking back, I don&#8217;t have a clue why I opted for the more convoluted way to compute it.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

