Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can figure out how <code>printList</code> behaves by drawing a recursion tree. Each node consists of two elements: an <code>alist</code> and a <code>blist</code>. The root has the <code>alist</code> with the initial sequence of items you want to permute, and an empty <code>blist</code>. Each node of the tree has one branch for each element of that node's <code>alist</code>; you move from a 'father' node to each one of its 'children' by choosing an element from the father's <code>alist</code> and:</p> <ul> <li>assigning to the child's <code>alist</code> the father's <code>alist</code> <em>minus that element</em>;</li> <li>assigning to the child's <code>blist</code> the father's <code>blist</code> plus that element <em>appended to its end</em>.</li> </ul> <p>The leafs have an empty <code>alist</code>, and since following different paths from the root to the leafs you have to choose elements from the root's <code>alist</code> in different orders, the <code>blist</code> of the leafs themselves contain all the various permutations of the root's <code>alist</code>.</p> <p>For example (<code>[abc],[] == alist,blist</code>):</p> <pre><code> [abc],[] / | \ a/ b| \c / | \ [bc],[a] [ac],[b] [ab],[c] / \ b/ \c / \ [c],[ab] [b],[ac] | | c| |b | | [],[abc] [],[acb] def printList(alist, blist=[]): # if alist is empty, we are in a 'leaf' in the recursion tree; # then blist contains one permutation; print it if not len(alist): print ''.join(blist) # ELSE, for each possible position in alist, for i in range(len(alist)): # move the element at that position from alist to the end of blist blist.append(alist.pop(i)) # go to the 'children' node and do the printing job for its subtree printList(alist, blist) # then move back the element from the end of blist to its original # position in alist, so we can continue with the for loop # without altering alist alist.insert(i, blist.pop()) </code></pre>
    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