tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
61
Task/Permutations/ALGOL-68/permutations-1.alg
Normal file
61
Task/Permutations/ALGOL-68/permutations-1.alg
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
# Document prelude template usage:
|
||||
TEMPLATE(
|
||||
INT upb values := 4;
|
||||
MODE VALUE = INT;
|
||||
FORMAT value fmt := $g(0)$
|
||||
); #
|
||||
|
||||
MODE
|
||||
VALVALUES = [upb values]VALUE, VALUES = REF VALVALUES,
|
||||
YIELDVALUES = PROC(VALUES)VOID;
|
||||
|
||||
FORMAT
|
||||
values fmt := $"("n(upb values-1)(f(value fmt)", ")f(value fmt)")"$;
|
||||
|
||||
# Generate permutations of the input values of valueues #
|
||||
PROC gen values permutations = (VALUES values, YIELDVALUES yield)VOID: (
|
||||
# Warning: this routine does not correctly handle duplicate elements #
|
||||
IF LWB values = UPB values THEN
|
||||
yield(values)
|
||||
ELSE
|
||||
FOR elem FROM LWB values TO UPB values DO
|
||||
VALUE first = values[elem];
|
||||
values[LWB values+1:elem] := values[:elem-1];
|
||||
values[LWB values] := first;
|
||||
# FOR VALUES next values IN # gen values permutations(values[LWB values+1:] # ) DO #,
|
||||
## (VALUES next)VOID:(
|
||||
yield(values)
|
||||
# OD #));
|
||||
values[:elem-1] := values[LWB values+1:elem];
|
||||
values[elem] := first
|
||||
OD
|
||||
FI
|
||||
);
|
||||
|
||||
############################################
|
||||
# Define some additional utility OPerators #
|
||||
############################################
|
||||
PRIO P = 7; # OP to calculate number of permutations #
|
||||
OP P = (INT n, k)INT: ( # n! OVER (n-k)! #
|
||||
# ( n>k | n * ((n-1) P k) | n ); #
|
||||
INT out := k;
|
||||
FOR i FROM k+1 TO n DO out *:= i OD;
|
||||
out
|
||||
);
|
||||
|
||||
# Define an operator for doing iterations over permutations #
|
||||
PRIO DOPERM = 1;
|
||||
OP (VALUES, YIELDVALUES)VOID DOPERM = gen values permutations;
|
||||
|
||||
# Return an a matrix of permutations #
|
||||
OP PERM = (VALUES in values)[, ]VALUE: (
|
||||
[(UPB in values-LWB in values+1) P 1, LWB in values:UPB in values]VALUE out;
|
||||
INT elem := LWB out;
|
||||
|
||||
# FOR VALUES values IN # in values DOPERM (
|
||||
## (VALUES values)VOID:(
|
||||
out[elem, ] := values;
|
||||
elem +:= 1
|
||||
# OD #));
|
||||
out
|
||||
);
|
||||
23
Task/Permutations/ALGOL-68/permutations-2.alg
Normal file
23
Task/Permutations/ALGOL-68/permutations-2.alg
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#!/usr/local/bin/a68g --script #
|
||||
|
||||
PR READ "Template_Permutations.a68" PR # n.b. READ is nonstandard #
|
||||
# USING( #
|
||||
INT upb values := 4;
|
||||
MODE VALUE = INT; # user defined #
|
||||
FORMAT value fmt := $g(0)$
|
||||
# ) #;
|
||||
|
||||
main:(
|
||||
VALVALUES test case := (1, 22, 333, 44444);
|
||||
print(("Number of permutations: ", UPB test case P 1, new line));
|
||||
|
||||
COMMENT # Use the generator: #
|
||||
# FOR ARRAY values IN # test case DOPERM (
|
||||
## (ARRAY values)VOID:(
|
||||
printf((values fmt, values, $l$))
|
||||
# OD #));
|
||||
END COMMENT
|
||||
|
||||
# or simply the operator: #
|
||||
printf(($f(values fmt)l$, PERM test case))
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue