langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,8 @@
let rec comb m lst =
match m, lst with
0, _ -> [[]]
| _, [] -> []
| m, x :: xs -> List.map (fun y -> x :: y) (comb (pred m) xs) @
comb m xs
;;
comb 3 [0;1;2;3;4];;

View file

@ -0,0 +1 @@
nchoosek([0:4], 3)

View file

@ -0,0 +1,16 @@
declare
fun {Comb M N}
proc {CombScript Comb}
%% Comb is a subset of [0..N-1]
Comb = {FS.var.upperBound {List.number 0 N-1 1}}
%% Comb has cardinality M
{FS.card Comb M}
%% enumerate all possibilities
{FS.distribute naive [Comb]}
end
in
%% Collect all solutions and convert to lists
{Map {SearchAll CombScript} FS.reflect.upperBoundList}
end
in
{Inspect {Comb 3 5}}

View file

@ -0,0 +1,12 @@
c(n,k,r,d)={
if(d==k,
for(i=2,k+1,
print1(r[i]" "));
print
,
for(i=r[d+1]+1,n,
r[d+2]=i;
c(n,k,r,d+1)));
}
c(5,3,vector(5,i,i-1),0)

View file

@ -0,0 +1,30 @@
Program Combinations;
const
m_max = 3;
n_max = 5;
var
combination: array [1..m_max] of integer;
procedure generate(m: integer);
var
n, i: integer;
begin
if (m > m_max) then
begin
for i := 1 to m_max do
write (combination[i], ' ');
writeln;
end
else
for n := 1 to n_max do
if ((m = 1) or (n > combination[m-1])) then
begin
combination[m] := n;
generate(m + 1);
end;
end;
begin
generate(1);
end.

View file

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

View file

@ -0,0 +1,17 @@
define comb(n, m);
lvars ress = [];
define do_combs(l, m, el_lst);
lvars i;
if m = 0 then
cons(rev(el_lst), ress) -> ress;
else
for i from l to n - m do
do_combs(i + 1, m - 1, cons(i, el_lst));
endfor;
endif;
enddefine;
do_combs(0, m, []);
rev(ress);
enddefine;
comb(5, 3) ==>

View file

@ -0,0 +1,7 @@
comb m n = comb m (0..n-1) with
comb 0 _ = [[]];
comb _ [] = [];
comb m (x:xs) = [x:xs | xs = comb (m-1) xs] + comb m xs;
end;
comb 3 5;

View file

@ -0,0 +1,34 @@
Procedure.s Combinations(amount, choose)
NewList comb.s()
; all possible combinations with {amount} Bits
For a = 0 To 1 << amount
count = 0
; count set bits
For x = 0 To amount
If (1 << x)&a
count + 1
EndIf
Next
; if set bits are equal to combination length
; we generate a String representing our combination and add it to list
If count = choose
string$ = ""
For x = 0 To amount
If (a >> x)&1
; replace x by x+1 to start counting with 1
String$ + Str(x) + " "
EndIf
Next
AddElement(comb())
comb() = string$
EndIf
Next
; now we sort our list and format it for output as string
SortList(comb(), #PB_Sort_Ascending)
ForEach comb()
out$ + ", [ " + comb() + "]"
Next
ProcedureReturn Mid(out$, 3)
EndProcedure
Debug Combinations(5, 3)

View file

@ -0,0 +1 @@
print({0..4} npow 3);

View file

@ -0,0 +1,35 @@
$ include "seed7_05.s7i";
const type: combinations is array array integer;
const func combinations: comb (in array integer: arr, in integer: k) is func
result
var combinations: combResult is combinations.value;
local
var integer: x is 0;
var integer: i is 0;
var array integer: suffix is 0 times 0;
begin
if k = 0 then
combResult := 1 times 0 times 0;
else
for x key i range arr do
for suffix range comb(arr[succ(i) ..], pred(k)) do
combResult &:= [] (x) & suffix;
end for;
end for;
end if;
end func;
const proc: main is func
local
var array integer: aCombination is 0 times 0;
var integer: element is 0;
begin
for aCombination range comb([] (0, 1, 2, 3, 4), 3) do
for element range aCombination do
write(element lpad 3);
end for;
writeln;
end for;
end func;

View file

@ -0,0 +1,6 @@
fun comb (0, _ ) = [[]]
| comb (_, [] ) = []
| comb (m, x::xs) = map (fn y => x :: y) (comb (m-1, xs)) @
comb (m, xs)
;
comb (3, [0,1,2,3,4]);

View file

@ -0,0 +1 @@
choices = ^(iota@r,~&l); leql@a^& ~&al?\&! ~&arh2fabt2RDfalrtPXPRT

View file

@ -0,0 +1,4 @@
#import std
#import nat
combinations = @rlX choices^|(iota,~&); -< @p nleq+ ==-~rh

View file

@ -0,0 +1,3 @@
#cast %nLL
example = combinations(3,5)

View file

@ -0,0 +1,6 @@
[comb [m lst] let
[ [m zero?] [[[]]]
[lst null?] [[]]
[true] [m pred lst rest comb [lst first swap cons] map
m lst rest comb concat]
] when].

View file

@ -0,0 +1,6 @@
[comb
[ [pop zero?] [pop pop [[]]]
[null?] [pop pop []]
[true] [ [m lst : [m pred lst rest comb [lst first swap cons] map
m lst rest comb concat]] view i ]
] when].

View file

@ -0,0 +1,8 @@
[comb
[2dup [a b : a b a b] view].
[2pop pop pop].
[ [pop zero?] [2pop [[]]]
[null?] [2pop []]
[true] [2dup [pred] dip uncons swapd comb [cons] map popd rollup rest comb concat]
] when].

View file

@ -0,0 +1,19 @@
code ChOut=8, CrLf=9, IntOut=11;
def M=3, N=5;
int A(N-1);
proc Combos(D, S); \Display all size M combinations of N in sorted order
int D, S; \depth of recursion, starting value of N
int I;
[if D<M then \depth < size
for I:= S to N-1 do
[A(D):= I;
Combos(D+1, I+1);
]
else [for I:= 0 to M-1 do
[IntOut(0, A(I)); ChOut(0, ^ )];
CrLf(0);
];
];
Combos(0, 0)