Skip to content

Commit 9380656

Browse files
oscardssmiththchrinkydragon
authored
Add multiset_permutations method that doesn't require length (#164)
* Add `multiset_permutations` method that doesn't require length in analogy to the same case for regular `permuations` * Add test * Update src/permutations.jl Co-authored-by: Thomas Christensen <tchr@mit.edu> * Update test/permutations.jl * Update src/permutations.jl Co-authored-by: Chengyu Han <cyhan.dev@outlook.com> --------- Co-authored-by: Thomas Christensen <tchr@mit.edu> Co-authored-by: Chengyu Han <cyhan.dev@outlook.com>
1 parent bbd5b1b commit 9380656

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/permutations.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,18 @@ function Base.length(c::MultiSetPermutations)
211211
return round(Int, p[t+1])
212212
end
213213

214+
215+
"""
216+
multiset_permutations(a)
217+
218+
Generate all permutations of an array `a` where `a` may have duplicated elements.
219+
"""
220+
multiset_permutations(a) = multiset_permutations(a, length(a))
221+
214222
"""
215223
multiset_permutations(a, t)
216224
217-
Generate all permutations of size `t` from an array `a` with possibly duplicated elements.
225+
Generate all permutations of size `t` from an array `a` where `a` may have duplicated elements.
218226
219227
# Examples
220228
```jldoctest

test/permutations.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ end
5959
@test collect(multiset_permutations("aabc", 5)) == Any[]
6060
@test collect(multiset_permutations("aabc", 2)) == Any[['a', 'a'], ['a', 'b'], ['a', 'c'], ['b', 'a'],
6161
['b', 'c'], ['c', 'a'], ['c', 'b']]
62+
@test collect(multiset_permutations("aabcc", 5)) == collect(multiset_permutations("aabcc"))
63+
@test collect(multiset_permutations("")) == Any[Char[]]
6264
@test collect(multiset_permutations("aabc", 0)) == Any[Char[]]
6365
@test collect(multiset_permutations("aabc", -1)) == Any[]
6466
@test collect(multiset_permutations("", 1)) == Any[]

0 commit comments

Comments
 (0)