Permutations and derangements

Two snippets to compute permuatations and derangements in Python.

4 Responses to “Permutations and derangements”


  1. 1 Anonymous

    I think you meant “the total number of derangements is 44″. You have 4, not 44.

  2. 2 Gustavo Niemeyer

    Right! Thanks!

  3. 3 Anonymous

    hi, I really like your algorithm. did you come up with this yourself or is it based on some permutation theorem?

  4. 4 Gustavo Niemeyer

    It’s my own child. :-)

    The original intent was not producing only permutation sets, but all possible contained groups and order of elements on the given list (e.g. [1,2,3] would also produce [2,3], [3,2], [3], etc). To achieve that just remove the constraint on the queue size before yielding the value.

Leave a Reply