Top-rated recipes tagged "permutations"http://code.activestate.com/recipes/tags/permutations/top/2016-02-21T06:00:46-08:00ActiveState Code RecipesSimple Permutations (Python) 2010-02-07T06:47:14-08:00manchesterboyhttp://code.activestate.com/recipes/users/4172234/http://code.activestate.com/recipes/577031-simple-permutations/ <p style="color: grey"> Python recipe 577031 by <a href="/recipes/users/4172234/">manchesterboy</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/combinations/">combinations</a>, <a href="/recipes/tags/permutations/">permutations</a>). Revision 2. </p> <p>Given a string of chars and a length, returns permutations of the specified length using the char string given in order. For example, given a string of "01" and a length of 3 returns 000, 001, 010, 011 ... 111</p> Get the inversion number of a permutation (Python) 2016-02-21T06:00:46-08:00Samuel James Ericksonhttp://code.activestate.com/recipes/users/4187478/http://code.activestate.com/recipes/579051-get-the-inversion-number-of-a-permutation/ <p style="color: grey"> Python recipe 579051 by <a href="/recipes/users/4187478/">Samuel James Erickson</a> (<a href="/recipes/tags/combinatorics/">combinatorics</a>, <a href="/recipes/tags/discrete/">discrete</a>, <a href="/recipes/tags/inversion/">inversion</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/permutations/">permutations</a>). Revision 2. </p> <p>This function takes a permutation in the form of a list and returns the number of inversions in the permutation. </p> Alternatve generation of the parity or sign of a permutation (Python) 2012-08-07T08:45:23-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/578236-alternatve-generation-of-the-parity-or-sign-of-a-p/ <p style="color: grey"> Python recipe 578236 by <a href="/recipes/users/398009/">Paddy McCarthy</a> (<a href="/recipes/tags/determinants/">determinants</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/permutations/">permutations</a>). </p> <p>Although <a href="http://code.activestate.com/recipes/578227/">Recipe 578227</a> was straight-forward to derive, I came across <a href="http://stackoverflow.com/questions/337664/counting-inversions-in-an-array/6424847#6424847">this Stack Overflow answer</a> that gives another method for generating the sign based on comparing the perm and the sorted perm.</p> Generate the parity or sign of a permutation (Python) 2012-07-28T07:21:49-07:00Paddy McCarthyhttp://code.activestate.com/recipes/users/398009/http://code.activestate.com/recipes/578227-generate-the-parity-or-sign-of-a-permutation/ <p style="color: grey"> Python recipe 578227 by <a href="/recipes/users/398009/">Paddy McCarthy</a> (<a href="/recipes/tags/determinants/">determinants</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/matrix/">matrix</a>, <a href="/recipes/tags/permutations/">permutations</a>). </p> <p>The <a href="http://en.wikipedia.org/wiki/Parity_of_a_permutation">parity</a> of a given permutation is whether an odd or even number of swaps between any two elements are needed to transform the given permutation to the first permutation.</p> <p>The sign of the permutation is +1 for an even parity and -1 for an odd parity.</p> <p>This python function returns the sign of a permutation of all the integers 0..N. When the program is run it shows the sign of permutations as generated by the standard function itertools.permutations.</p> <p>The function uses a modified <a href="http://rosettacode.org/wiki/Sorting_algorithms/Selection_sort#Python">selection sort</a> to compute the parity.</p> Efficient generation of permutations (Python) 2008-12-22T06:11:55-08:00nnarulahttp://code.activestate.com/recipes/users/4168533/http://code.activestate.com/recipes/576593-efficient-generation-of-permutations/ <p style="color: grey"> Python recipe 576593 by <a href="/recipes/users/4168533/">nnarula</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/permutations/">permutations</a>). </p> <p>This code builds an iterator on the fly that will successfully return unique permutations of n integers, m at a time (nPm). It does not use recursion, so stack size is not a problem. Sample usage it= build(n,p) <br /> it.next() # returns permutation</p> <p>it=build(n) is the same as build(n,n) do it will generate n! unique permuatations.</p> <p>I worte it over the weekend and have tested it reasonably for n upto 30 and p from 1 to 30 </p> Bell permutations using Even's nonrecursive algorithm (Python) 2013-12-23T02:44:43-08:00Chris Smithhttp://code.activestate.com/recipes/users/2725752/http://code.activestate.com/recipes/578792-bell-permutations-using-evens-nonrecursive-algorit/ <p style="color: grey"> Python recipe 578792 by <a href="/recipes/users/2725752/">Chris Smith</a> (<a href="/recipes/tags/bell/">bell</a>, <a href="/recipes/tags/johnson/">johnson</a>, <a href="/recipes/tags/permutations/">permutations</a>, <a href="/recipes/tags/steinhaus/">steinhaus</a>, <a href="/recipes/tags/trotter/">trotter</a>). Revision 2. </p> <p>The "bell" permutations are those in which only a single inversion of neighbors occurs to generate the next permutation.</p>