You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
struct WithReplacementPermutations{T}
a::T
t::Int
end
Base.eltype(::Type{WithReplacementPermutations{T}}) where {T} = Vector{eltype(T)}
Base.length(p::WithReplacementPermutations) = length(p.a)^p.t
"""
with_replacement_permutations(a, t)
Generate all permutations with replacement of size `t` from an array `a`.
"""
with_replacement_permutations(elements::T, len::Integer) where {T} =
WithReplacementPermutations{T}(unique(elements), len)
function Base.iterate(p::WithReplacementPermutations, s = ones(Int, p.t))
(!isempty(s) && s[end] > length(p.a) || (isempty(s) && p.t > 0)) && return
nextpermutationwithreplacement(p.a, p.t, s)
end
function nextpermutationwithreplacement(a, t, state)
cur = a[state]
n = length(state)
state[1] += 1
local i = 1
while i < n && state[i] > length(a)
state[i] = 1
state[i+1] += 1
i += 1
end
return cur, state
end
The text was updated successfully, but these errors were encountered:
I believe it would be useful to add a method to generate permutations with repetitions, so that, for example
A possible implementation could be the following
The text was updated successfully, but these errors were encountered: