Is there a function behaving the same as next_permutation does in C++?

A “next” permutation only makes sense when you have a clear definition of what “next” is supposed to mean. You seem to want it to mean “the next permutation of a given vector in a lexicographic ordering of all permutations of that vector”, which I don’t think is part of the Combinatorics.jl API by itself since that package deals with mathematical permutations in the form of vectors (i.e., lists of indices describing a permutation).

If you expected [2,1,2] instead, you’ve hit an off-by-one error in your mental model, since julia uses 1-based indexing. The first and second permutation is the same and you didn’t specify whether you need unique permutations or not:

julia> permutations([1,2,2]) |> collect
6-element Vector{Vector{Int64}}:       
 [1, 2, 2]                             
 [1, 2, 2]                             
 [2, 1, 2]                             
 [2, 2, 1]                             
 [2, 1, 2]                             
 [2, 2, 1]                             

If that’s not what you expected, what do you want the result to be?

1 Like