Skip to content

Commit 6897953

Browse files
committed
Add doctest for derangements
1 parent d16ccf7 commit 6897953

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/permutations.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,32 @@ Generate all derangements of an indexable object `a` in lexicographic order.
144144
Because the number of derangements can be very large, this function returns an iterator object.
145145
Use `collect(derangements(a))` to get an array of all derangements.
146146
Only works for `a` with defined length.
147+
148+
# Examples
149+
```jldoctest
150+
julia> derangements("julia") |> collect
151+
44-element Vector{Vector{Char}}:
152+
['u', 'j', 'i', 'a', 'l']
153+
['u', 'j', 'a', 'l', 'i']
154+
['u', 'l', 'j', 'a', 'i']
155+
['u', 'l', 'i', 'a', 'j']
156+
['u', 'l', 'a', 'j', 'i']
157+
['u', 'i', 'j', 'a', 'l']
158+
['u', 'i', 'a', 'j', 'l']
159+
['u', 'i', 'a', 'l', 'j']
160+
['u', 'a', 'j', 'l', 'i']
161+
['u', 'a', 'i', 'j', 'l']
162+
163+
['a', 'j', 'i', 'l', 'u']
164+
['a', 'l', 'j', 'u', 'i']
165+
['a', 'l', 'u', 'j', 'i']
166+
['a', 'l', 'i', 'j', 'u']
167+
['a', 'l', 'i', 'u', 'j']
168+
['a', 'i', 'j', 'u', 'l']
169+
['a', 'i', 'j', 'l', 'u']
170+
['a', 'i', 'u', 'j', 'l']
171+
['a', 'i', 'u', 'l', 'j']
172+
```
147173
"""
148174
derangements(a) = (d for d in multiset_permutations(a, length(a)) if all(t -> t[1] != t[2], zip(a, d)))
149175

0 commit comments

Comments
 (0)