From 6afa6f086d998df7b7a4c6dbee346a41afca5fbc Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Tue, 30 Apr 2024 11:46:29 -0400 Subject: [PATCH 1/3] Add `multiset_permutations` method that doesn't require length in analogy to the same case for regular `permuations` --- src/permutations.jl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/permutations.jl b/src/permutations.jl index 9e51ef7..a7c1085 100644 --- a/src/permutations.jl +++ b/src/permutations.jl @@ -170,10 +170,22 @@ function Base.length(c::MultiSetPermutations) return round(Int, p[t+1]) end + +""" + multiset_permutations(a) + +Generate all permutations of an array `a` where `a` may have duplicated elements. +""" +function multiset_permutations(a) + m = unique(collect(a)) + f = [sum([c == x for c in a]) for x in m] + multiset_permutations(m, f, length(a)) +end + """ multiset_permutations(a, t) -Generate all permutations of size `t` from an array `a` with possibly duplicated elements. +Generate all permutations of size `t` from an array `a` where `a` may have duplicated elements. """ function multiset_permutations(a, t::Integer) m = unique(collect(a)) From 2813a1910bdd1a7f9014f322db810a063857c2f7 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Tue, 30 Apr 2024 22:26:56 -0400 Subject: [PATCH 2/3] Add test --- test/permutations.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/permutations.jl b/test/permutations.jl index ae18b48..60b5fa9 100644 --- a/test/permutations.jl +++ b/test/permutations.jl @@ -59,6 +59,8 @@ end @test collect(multiset_permutations("aabc", 5)) == Any[] @test collect(multiset_permutations("aabc", 2)) == Any[['a', 'a'], ['a', 'b'], ['a', 'c'], ['b', 'a'], ['b', 'c'], ['c', 'a'], ['c', 'b']] + @test collect(multiset_permutations("aabcc", 5)) == collect(multiset_permutations("aabcc")) + @test collect(multiset_permutations("aabc")) == Any[Char[]] @test collect(multiset_permutations("aabc", 0)) == Any[Char[]] @test collect(multiset_permutations("aabc", -1)) == Any[] @test collect(multiset_permutations("", 1)) == Any[] From 144f3608b424650b4bb67116913513672b277ca6 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Thu, 7 Nov 2024 09:46:04 -0500 Subject: [PATCH 3/3] Update src/permutations.jl Co-authored-by: Thomas Christensen --- src/permutations.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/permutations.jl b/src/permutations.jl index a7c1085..8b39f91 100644 --- a/src/permutations.jl +++ b/src/permutations.jl @@ -178,7 +178,7 @@ Generate all permutations of an array `a` where `a` may have duplicated elements """ function multiset_permutations(a) m = unique(collect(a)) - f = [sum([c == x for c in a]) for x in m] + f = [count(==(x), a) for x in m] multiset_permutations(m, f, length(a)) end