September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
58
Task/Permutations/Modula-3/permutations-1.mod3
Normal file
58
Task/Permutations/Modula-3/permutations-1.mod3
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
MODULE Permutations EXPORTS Main;
|
||||
|
||||
IMPORT IO, IntSeq;
|
||||
|
||||
CONST n = 3;
|
||||
|
||||
TYPE Domain = SET OF [ 1.. n ];
|
||||
|
||||
VAR
|
||||
|
||||
chosen: IntSeq.T;
|
||||
values := Domain { };
|
||||
|
||||
PROCEDURE GeneratePermutations(VAR chosen: IntSeq.T; remaining: Domain) =
|
||||
(*
|
||||
Recursively generates all the permutations of elements
|
||||
in the union of "chosen" and "values".
|
||||
Values in "chosen" have already been chosen;
|
||||
values in "remaining" can still be chosen.
|
||||
If "remaining" is empty, it prints the sequence and returns.
|
||||
Otherwise, it picks each element in "remaining", removes it,
|
||||
adds it to "chosen", recursively calls itself,
|
||||
then removes the last element of "chosen" and adds it back to "remaining".
|
||||
*)
|
||||
BEGIN
|
||||
FOR i := 1 TO n DO
|
||||
(* check if each element is in "remaining" *)
|
||||
IF i IN remaining THEN
|
||||
(* if so, remove from "remaining" and add to "chosen" *)
|
||||
remaining := remaining - Domain { i };
|
||||
chosen.addhi(i);
|
||||
IF remaining # Domain { } THEN
|
||||
(* still something to process? do it *)
|
||||
GeneratePermutations(chosen, remaining);
|
||||
ELSE
|
||||
(* otherwise, print what we've chosen *)
|
||||
FOR j := 0 TO chosen.size() - 2 DO
|
||||
IO.PutInt(chosen.get(j)); IO.Put(", ");
|
||||
END;
|
||||
IO.PutInt(chosen.gethi());
|
||||
IO.PutChar('\n');
|
||||
END;
|
||||
(* add "i" back to "remaining" and remove from "chosen" *)
|
||||
remaining := remaining + Domain { i };
|
||||
EVAL chosen.remhi();
|
||||
END;
|
||||
END;
|
||||
END GeneratePermutations;
|
||||
|
||||
BEGIN
|
||||
|
||||
(* initial setup *)
|
||||
chosen := NEW(IntSeq.T).init(n);
|
||||
FOR i := 1 TO n DO values := values + Domain { i }; END;
|
||||
|
||||
GeneratePermutations(chosen, values);
|
||||
|
||||
END Permutations.
|
||||
28
Task/Permutations/Modula-3/permutations-2.mod3
Normal file
28
Task/Permutations/Modula-3/permutations-2.mod3
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
GENERIC INTERFACE GenericPermutations(DomainSeq, DomainSet, DomainSeqSeq);
|
||||
|
||||
(*
|
||||
"Domain" is where the things to permute come from (unused in interface).
|
||||
"DomainSeq" is a "Sequence" of "Domain".
|
||||
"DomainSet" is a "Set" of "Domain".
|
||||
"DomainSeqSeq" is a "Sequence" of "DomainSeq".
|
||||
*)
|
||||
|
||||
PROCEDURE GeneratePermutations(
|
||||
READONLY chosen: DomainSeq.T;
|
||||
READONLY remaining: DomainSet.T;
|
||||
READONLY result: DomainSeqSeq.T
|
||||
);
|
||||
(*
|
||||
Recursively generates all the permutations of elements
|
||||
in the union of "chosen" and "remaining".
|
||||
Values in "chosen" have already been chosen;
|
||||
values in "remaining" can still be chosen.
|
||||
If "remaining" is empty, it adds the permutation to "result".
|
||||
Otherwise, it picks each element in "remaining", removes it,
|
||||
adds it to "chosen", recursively calls itself,
|
||||
then removes the last element of "chosen" and adds it back to "remaining".
|
||||
Although the parameters are modified, we can describe them as "READONLY"
|
||||
because we do not re-assign them.
|
||||
*)
|
||||
|
||||
END GenericPermutations.
|
||||
71
Task/Permutations/Modula-3/permutations-3.mod3
Normal file
71
Task/Permutations/Modula-3/permutations-3.mod3
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
GENERIC MODULE GenericPermutations(Domain, DomainSeq, DomainSet, DomainSeqSeq);
|
||||
|
||||
(*
|
||||
"Domain" is where the things to permute come from.
|
||||
"DomainSeq" is a "Sequence" of "Domain".
|
||||
"DomainSet" is a "Set" of "Domain".
|
||||
"DomainSeqSeq" is a "Sequence" of "DomainSeq".
|
||||
*)
|
||||
|
||||
PROCEDURE GeneratePermutations(
|
||||
READONLY chosen: DomainSeq.T;
|
||||
READONLY remaining: DomainSet.T;
|
||||
READONLY result: DomainSeqSeq.T
|
||||
) =
|
||||
|
||||
(*
|
||||
Recursively generates all the permutations of elements
|
||||
in the union of "chosen" and "remaining".
|
||||
Values in "chosen" have already been chosen;
|
||||
values in "remaining" can still be chosen.
|
||||
If "remaining" is empty, it adds the permutation to "result".
|
||||
Otherwise, it picks each element in "remaining", removes it,
|
||||
adds it to "chosen", recursively calls itself,
|
||||
then removes the last element of "chosen" and adds it back to "remaining".
|
||||
*)
|
||||
|
||||
VAR
|
||||
|
||||
r: Domain.T; (* element added to permutation *)
|
||||
|
||||
iterator := remaining.iterate(); (* to iterate through remaining elements *)
|
||||
|
||||
values := NEW(DomainSeq.T).init(remaining.size());
|
||||
(* used to store values for iteration *)
|
||||
|
||||
BEGIN
|
||||
|
||||
(* cannot safely modify a set while iterating, so we'll store the values *)
|
||||
WHILE iterator.next(r) DO values.addhi(r); END;
|
||||
|
||||
(* now loop through the stored values *)
|
||||
FOR i := 0 TO values.size() - 1 DO
|
||||
|
||||
(* remove from "remaining" and add to "chosen" *)
|
||||
r := values.get(i);
|
||||
EVAL remaining.delete(r);
|
||||
chosen.addhi(r);
|
||||
|
||||
(* if this is not the last remaining elements, call recursively *)
|
||||
IF remaining.size() # 0 THEN
|
||||
GeneratePermutations(chosen, remaining, result);
|
||||
ELSE
|
||||
(* we have a new permutation; add a copy to the set *)
|
||||
VAR newPerm := NEW(DomainSeq.T).init(chosen.size());
|
||||
BEGIN
|
||||
FOR i := 0 TO chosen.size() - 1 DO
|
||||
newPerm.addhi(chosen.get(i));
|
||||
END;
|
||||
result.addhi(newPerm);
|
||||
END;
|
||||
END;
|
||||
|
||||
(* move r back from chosen *)
|
||||
EVAL remaining.insert(chosen.remhi());
|
||||
|
||||
END;
|
||||
|
||||
END GeneratePermutations;
|
||||
|
||||
BEGIN
|
||||
END GenericPermutations.
|
||||
43
Task/Permutations/Modula-3/permutations-4.mod3
Normal file
43
Task/Permutations/Modula-3/permutations-4.mod3
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
MODULE GPermutations EXPORTS Main;
|
||||
|
||||
IMPORT IO, IntSeq, IntSetTree, IntSeqSeq, IntPermutations;
|
||||
|
||||
CONST
|
||||
|
||||
n = 7;
|
||||
|
||||
VAR
|
||||
|
||||
chosen: IntSeq.T;
|
||||
remaining: IntSetTree.T;
|
||||
result: IntSeqSeq.T;
|
||||
|
||||
PROCEDURE Factorial(n: CARDINAL): CARDINAL =
|
||||
VAR result := 1;
|
||||
BEGIN
|
||||
FOR i := 2 TO n DO
|
||||
result := result * i;
|
||||
END;
|
||||
RETURN result;
|
||||
END Factorial;
|
||||
|
||||
BEGIN
|
||||
|
||||
(* initial setup *)
|
||||
chosen := NEW(IntSeq.T).init(n);
|
||||
remaining := NEW(IntSetTree.T).init();
|
||||
result := NEW(IntSeqSeq.T).init(Factorial(n));
|
||||
FOR i := 1 TO n DO EVAL remaining.insert(i); END;
|
||||
|
||||
IntPermutations.GeneratePermutations(chosen, remaining, result);
|
||||
|
||||
IO.Put("Printing "); IO.PutInt(result.size());
|
||||
IO.Put(" permutations of "); IO.PutInt(n); IO.Put(" elements \n");
|
||||
FOR i := 0 TO result.size() - 1 DO
|
||||
FOR j := 0 TO result.get(i).size() - 1 DO
|
||||
IO.PutInt(result.get(i).get(j)); IO.PutChar(' ');
|
||||
END;
|
||||
IO.PutChar('\n');
|
||||
END;
|
||||
|
||||
END GPermutations.
|
||||
Loading…
Add table
Add a link
Reference in a new issue