Combinations with repetitions: Difference between revisions
Content added Content deleted
Catskill549 (talk | contribs) |
(→{{header|Julia}}: added Julia example) |
||
Line 1,011: | Line 1,011: | ||
["plain","plain"] |
["plain","plain"] |
||
There are 220 ways to pick 3 objects with replacement from 10.</pre> |
There are 220 ways to pick 3 objects with replacement from 10.</pre> |
||
=={{header|Julia}}== |
|||
(Based on [http://stackoverflow.com/questions/26313868/julia-unique-sets-of-n-elements-with-replacement this StackOverflow discussion].) |
|||
<lang julia>function combos_with_replacement(list, k) |
|||
n = length(list) |
|||
[[list[c[i]-i+1] for i=1:length(c)] for c in combinations([1:(n+k-1)],k)] |
|||
end</lang> |
|||
{{out}} |
|||
<pre>julia> combos_with_replacement(["iced","jam","plain"], 2) |
|||
6-element Array{Array{ASCIIString,1},1}: |
|||
ASCIIString["iced","iced"] |
|||
ASCIIString["iced","jam"] |
|||
ASCIIString["iced","plain"] |
|||
ASCIIString["jam","jam"] |
|||
ASCIIString["jam","plain"] |
|||
ASCIIString["plain","plain"] |
|||
julia> length(combos_with_replacement(1:10, 3)) |
|||
220</pre> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |