@@ -142,6 +142,32 @@ Generate all derangements of an indexable object `a` in lexicographic order.
142142Because the number of derangements can be very large, this function returns an iterator object.
143143Use `collect(derangements(a))` to get an array of all derangements.
144144Only works for `a` with defined length.
145+
146+ # Examples
147+ ```jldoctest
148+ julia> derangements("julia") |> collect
149+ 44-element Vector{Vector{Char}}:
150+ ['u', 'j', 'i', 'a', 'l']
151+ ['u', 'j', 'a', 'l', 'i']
152+ ['u', 'l', 'j', 'a', 'i']
153+ ['u', 'l', 'i', 'a', 'j']
154+ ['u', 'l', 'a', 'j', 'i']
155+ ['u', 'i', 'j', 'a', 'l']
156+ ['u', 'i', 'a', 'j', 'l']
157+ ['u', 'i', 'a', 'l', 'j']
158+ ['u', 'a', 'j', 'l', 'i']
159+ ['u', 'a', 'i', 'j', 'l']
160+ ⋮
161+ ['a', 'j', 'i', 'l', 'u']
162+ ['a', 'l', 'j', 'u', 'i']
163+ ['a', 'l', 'u', 'j', 'i']
164+ ['a', 'l', 'i', 'j', 'u']
165+ ['a', 'l', 'i', 'u', 'j']
166+ ['a', 'i', 'j', 'u', 'l']
167+ ['a', 'i', 'j', 'l', 'u']
168+ ['a', 'i', 'u', 'j', 'l']
169+ ['a', 'i', 'u', 'l', 'j']
170+ ```
145171"""
146172derangements (a) = (d for d in multiset_permutations (a, length (a)) if all (t -> t[1 ] != t[2 ], zip (a, d)))
147173
0 commit comments