Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>class unique_element: def __init__(self,value,occurrences): self.value = value self.occurrences = occurrences def perm_unique(elements): eset=set(elements) listunique = [unique_element(i,elements.count(i)) for i in eset] u=len(elements) return perm_unique_helper(listunique,[0]*u,u-1) def perm_unique_helper(listunique,result_list,d): if d &lt; 0: yield tuple(result_list) else: for i in listunique: if i.occurrences &gt; 0: result_list[d]=i.value i.occurrences-=1 for g in perm_unique_helper(listunique,result_list,d-1): yield g i.occurrences+=1 a = list(perm_unique([1,1,2])) print(a) </code></pre> <p>result:</p> <pre><code>[(2, 1, 1), (1, 2, 1), (1, 1, 2)] </code></pre> <p>EDIT (how this works): </p> <p>I rewrote the upper program to be longer but more readable</p> <p>I usually have a hard time to explain how something works, but let me try. In order to understand how this works you have to understand similar, but a simpler program that would yield all permutations with repetition.</p> <pre><code>def permutations_with_replacement(elements,n): return permutations_helper(elements,[0]*n,n-1)#this is generator def permutations_helper(elements,result_list,d): if d&lt;0: yield tuple(result_list) else: for i in elements: result_list[d]=i all_permutations = permutations_helper(elements,result_list,d-1)#this is generator for g in all_permutations: yield g </code></pre> <p>This program is obviously much simpler: d stands for depth in permutations_helper and has two functions. One function is stopping condition of our recursive algorithm and other is for result list, that is passed around.</p> <p>Instead of returning each result, we yield it. If there were no function/operator <code>yield</code> we had to push result in some queue at point of stopping condition. But this way once stopping condition is meet result is propagated trough all stack up to the caller. That is purpose of<br> <code>for g in perm_unique_helper(listunique,result_list,d-1): yield g</code> so each result is propagated up to caller.</p> <p>Back to the original program: We have list of unique elements. Before we can use each element, we have to check how many of them are still available to push it on result_list. Working of this program is very similar compared to <code>permutations_with_replacement</code> difference is that each element can not be repeated more times that is in perm_unique_helper.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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