This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,3 @@
USING: math.combinatorics prettyprint ;
5 iota 3 all-combinations .

View file

@ -0,0 +1 @@
{ "a" "b" "c" } 2 all-combinations .

View file

@ -0,0 +1,6 @@
# Built-in
Combinations([1 .. n], m);
Combinations([1 .. 5], 3);
# [ [ 1, 2, 3 ], [ 1, 2, 4 ], [ 1, 2, 5 ], [ 1, 3, 4 ], [ 1, 3, 5 ],
# [ 1, 4, 5 ], [ 2, 3, 4 ], [ 2, 3, 5 ], [ 2, 4, 5 ], [ 3, 4, 5 ] ]

View file

@ -0,0 +1,10 @@
def comb
comb = { m, list ->
def n = list.size()
m == 0 ?
[[]] :
(0..(n-m)).inject([]) { newlist, k ->
def sublist = (k+1 == n) ? [] : list[(k+1)..<n]
newlist += comb(m-1, sublist).collect { [list[k]] + it }
}
}

View file

@ -0,0 +1,3 @@
def csny = [ "Crosby", "Stills", "Nash", "Young" ]
println "Choose from ${csny}"
(0..(csny.size())).each { i -> println "Choose ${i}:"; comb(i, csny).each { println it }; println() }

View file

@ -0,0 +1 @@
def comb0 = { m, n -> comb(m, (0..<n)) }

View file

@ -0,0 +1,2 @@
println "Choose out of 5 (zero-based):"
(0..3).each { i -> println "Choose ${i}:"; comb0(i, 5).each { println it }; println() }

View file

@ -0,0 +1 @@
def comb1 = { m, n -> comb(m, (1..n)) }

View file

@ -0,0 +1,2 @@
println "Choose out of 5 (one-based):"
(0..3).each { i -> println "Choose ${i}:"; comb1(i, 5).each { println it }; println() }

View file

@ -0,0 +1,20 @@
procedure main()
return combinations(3,5,0)
end
procedure combinations(m,n,z) # demonstrate combinations
/z := 1
write(m," combinations of ",n," integers starting from ",z)
every put(L := [], z to n - 1 + z by 1) # generate list of n items from z
write("Intial list\n",list2string(L))
write("Combinations:")
every write(list2string(lcomb(L,m)))
end
procedure list2string(L) # helper function
every (s := "[") ||:= " " || (!L|"]")
return s
end
link lists

View file

@ -0,0 +1,8 @@
procedure lcomb(L,i) #: list combinations
local j
if i < 1 then fail
suspend if i = 1 then [!L]
else [L[j := 1 to *L - i + 1]] ||| lcomb(L[j + 1:0],i - 1)
end

View file

@ -0,0 +1,5 @@
comb1=: dyad define
c=. 1 {.~ - d=. 1+y-x
z=. i.1 0
for_j. (d-1+y)+/&i.d do. z=. (c#j) ,. z{~;(-c){.&.>&lt;i.{.c=. +/\.c end.
)

View file

@ -0,0 +1,3 @@
comb=: dyad define M.
if. (x>:y)+.0=x do. i.(x<:y),x else. (0,.x comb&.<: y),1+x comb y-1 end.
)

View file

@ -0,0 +1,13 @@
julia> for i in @task combinations(1:5,3)
println(i)
end
[1, 2, 3]
[1, 2, 4]
[1, 3, 4]
[2, 3, 4]
[1, 2, 5]
[1, 3, 5]
[2, 3, 5]
[1, 4, 5]
[2, 4, 5]
[3, 4, 5]

View file

@ -0,0 +1,7 @@
to comb :n :list
if :n = 0 [output [[]]]
if empty? :list [output []]
output sentence map [sentence first :list ?] comb :n-1 bf :list ~
comb :n bf :list
end
print comb 3 [0 1 2 3 4]

View file

@ -0,0 +1,26 @@
divert(-1)
define(`set',`define(`$1[$2]',`$3')')
define(`get',`defn(`$1[$2]')')
define(`setrange',`ifelse(`$3',`',$2,`define($1[$2],$3)`'setrange($1,
incr($2),shift(shift(shift($@))))')')
define(`for',
`ifelse($#,0,``$0'',
`ifelse(eval($2<=$3),1,
`pushdef(`$1',$2)$4`'popdef(`$1')$0(`$1',incr($2),$3,`$4')')')')
define(`show',
`for(`k',0,decr($1),`get(a,k) ')')
define(`chklim',
`ifelse(get(`a',$3),eval($2-($1-$3)),
`chklim($1,$2,decr($3))',
`set(`a',$3,incr(get(`a',$3)))`'for(`k',incr($3),decr($2),
`set(`a',k,incr(get(`a',decr(k))))')`'nextcomb($1,$2)')')
define(`nextcomb',
`show($1)
ifelse(eval(get(`a',0)<$2-$1),1,
`chklim($1,$2,decr($1))')')
define(`comb',
`for(`j',0,decr($1),`set(`a',j,j)')`'nextcomb($1,$2)')
divert
comb(3,5)

View file

@ -0,0 +1 @@
combinations[n_Integer, m_Integer]/;m>= 0:=Union[Sort /@ Permutations[Range[0, n - 1], {m}]]

View file

@ -0,0 +1,26 @@
next_comb(n, p, a) := block(
[a: copylist(a), i: p],
if a[1] + p = n + 1 then return(und),
while a[i] - i >= n - p do i: i - 1,
a[i]: a[i] + 1,
for j from i + 1 thru p do a[j]: a[j - 1] + 1,
a
)$
combinations(n, p) := block(
[a: makelist(i, i, 1, p), v: [ ]],
while a # 'und do (v: endcons(a, v), a: next_comb(n, p, a)),
v
)$
combinations(5, 3);
/* [[1, 2, 3],
[1, 2, 4],
[1, 2, 5],
[1, 3, 4],
[1, 3, 5],
[1, 4, 5],
[2, 3, 4],
[2, 3, 5],
[2, 4, 5],
[3, 4, 5]] */