This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -12,3 +12,6 @@ For example, <tt>3 comb 5</tt> is
2 3 4
If it is more "natural" in your language to start counting from <tt>1</tt> instead of <tt>0</tt> the combinations can be of the integers from <tt>1</tt> to <tt>n</tt>.
'''See Also:'''
{{Template:Combinations and permutations}}

View file

@ -0,0 +1,32 @@
# -*- coding: utf-8 -*- #
COMMENT REQUIRED BY "prelude_combinations_generative.a68"
MODE COMBDATA = ~;
PROVIDES:
# COMBDATA*=~* #
# comb*=~ list* #
END COMMENT
MODE COMBDATALIST = REF[]COMBDATA;
MODE COMBDATALISTYIELD = PROC(COMBDATALIST)VOID;
PROC comb gen combinations = (INT m, COMBDATALIST list, COMBDATALISTYIELD yield)VOID:(
CASE m IN
# case 1: transpose list #
FOR i TO UPB list DO yield(list[i]) OD
OUT
[m + LWB list - 1]COMBDATA out;
INT index out := 1;
FOR i TO UPB list DO
COMBDATA first = list[i];
# FOR COMBDATALIST sub recombination IN # comb gen combinations(m - 1, list[i+1:] #) DO (#,
## (COMBDATALIST sub recombination)VOID:(
out[LWB list ] := first;
out[LWB list+1:] := sub recombination;
yield(out)
# OD #))
OD
ESAC
);
SKIP

View file

@ -0,0 +1,21 @@
#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #
CO REQUIRED BY "prelude_combinations.a68" CO
MODE COMBDATA = INT;
#PROVIDES:#
# COMBDATA~=INT~ #
# comb ~=int list ~#
PR READ "prelude_combinations.a68" PR;
FORMAT data fmt = $g(0)$;
main:(
INT m = 3;
FORMAT list fmt = $"("n(m-1)(f(data fmt)",")f(data fmt)")"$;
FLEX[0]COMBDATA test data list := (1,2,3,4,5);
# FOR COMBDATALIST recombination data IN # comb gen combinations(m, test data list #) DO (#,
## (COMBDATALIST recombination)VOID:(
printf ((list fmt, recombination, $l$))
# OD # ))
)

View file

@ -1,5 +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.
for_j. (d-1+y)+/&i.d do. z=. (c#j) ,. z{~;(-c){.&.><i.{.c=. +/\.c end.
)

View file

@ -0,0 +1 @@
combb=: (#~ ((-:/:~)>/:~-:\:~)"1)@(# #: [: i. ^~)

View file

@ -1,13 +1,3 @@
julia> for i in @task combinations(1:5,3)
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

@ -3,9 +3,10 @@ proto combine (Int, @) {*}
multi combine (0, @) { [] }
multi combine ($, []) { () }
multi combine ($n, [$head, *@tail]) {
map( { [$head, @^others] },
combine($n-1, @tail) ),
combine($n, @tail);
gather {
take [$head, @$_] for combine($n-1, @tail);
take [ @$_ ] for combine($n , @tail);
}
}
.say for combine(3, [^5]);
say combine(3, [^5]).perl;

View file

@ -0,0 +1,9 @@
(define sublists
(match-lambda**
[(0 _) '(())]
[(_ '()) '()]
[(m (cons x xs)) (append (map (curry cons x) (sublists (- m 1) xs))
(sublists m xs))]))
(define (combinations n m)
(sublists n (range m)))