Combinations with repetitions: Difference between revisions
Content added Content deleted
(→{{header|Perl}}: Add a module) |
(jq) |
||
Line 943: | Line 943: | ||
6 combos |
6 combos |
||
pick 3 out of 10: 220 combos</lang> |
pick 3 out of 10: 220 combos</lang> |
||
=={{header|jq}}== |
|||
<lang jq>def pick(n): |
|||
def pick(n; m): # pick n, from m onwards |
|||
if n == 0 then [] |
|||
elif m == length then empty |
|||
elif n == 1 then (.[m:][] | [.]) |
|||
else ([.[m]] + pick(n-1; m)), pick(n; m+1) |
|||
end; |
|||
pick(n;0) ;</lang> |
|||
'''The task''': |
|||
<lang jq> "Pick 2:", |
|||
(["iced", "jam", "plain"] | pick(2)), |
|||
([[range(0;10)] | pick(3)] | length) as $n |
|||
| "There are \($n) ways to pick 3 objects with replacement from 10." |
|||
</lang> |
|||
{{Out}} |
|||
<lang sh>$ jq -n -r -c -f pick.jq |
|||
Pick 2: |
|||
["iced","iced"] |
|||
["iced","jam"] |
|||
["iced","plain"] |
|||
["jam","jam"] |
|||
["jam","plain"] |
|||
["plain","plain"] |
|||
There are 220 ways to pick 3 objects with replacement from 10.</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |