Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This solution is going to involve a bit of <a href="http://docs.python.org/library/itertools.html#module-itertools" rel="nofollow"><code>itertools.permutations</code></a> usage, <a href="http://docs.python.org/library/stdtypes.html#set" rel="nofollow"><code>set()</code></a>, and some good ol' fashioned set difference. Bear in mind, the runtime for finding a permutation will still be O(n!). My solution won't do it in-line, either, but there <em>may</em> be a much more elegant solution that allows you to do so (and doesn't involve <code>itertools.permutations</code>). For this purpose, this is the straightforward way to accomplish the task.</p> <p>Step 1: Algorithm for generating cycles, using the first element given. For a list <code>[1, 1, 2, 2]</code>, this will give us <code>[1, 1, 2, 2], [1, 2, 2, 1], [2, 1, 1, 2], [2, 2, 1, 1]</code>.</p> <pre><code>def rotations(li): count = 0 while count &lt; len(li): yield tuple(li) li = li[1:] + [li[0]] count += 1 </code></pre> <p>Step 2: Importing <code>itertools.permutations</code> to give us the permutations in the first place, then setting up its results into a <code>set</code>.</p> <pre><code>from itertools import permutations perm = set(permutations([1, 1, 2, 2])) </code></pre> <p>Step 3: Using the generator to give us our own set, with the cycles (something we want to rid ourselves of).</p> <pre><code>cycles = set(((i for i in rotations([1, 1, 2, 2])))) </code></pre> <p>Step 4: Apply set difference to each and the cycles are removed.</p> <pre><code>perm = perm.difference(cycles) </code></pre> <p>Hopefully this will help you out. I'm open to suggestions and/or corrections.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload