This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -1,11 +1,9 @@
import std.stdio: writeln;
T[][] permutations(T)(T[] items) {
T[][] permutations(T)(T[] items) pure nothrow {
T[][] result;
void perms(T[] s, T[] prefix=[]) {
void perms(T[] s, T[] prefix=[]) nothrow {
if (s.length)
foreach (i, c; s)
foreach (immutable i, immutable c; s)
perms(s[0 .. i] ~ s[i+1 .. $], prefix ~ c);
else
result ~= prefix;
@ -15,7 +13,9 @@ T[][] permutations(T)(T[] items) {
return result;
}
void main() {
foreach (p; permutations([1, 2, 3]))
writeln(p);
version (permutations1_main) {
void main() {
import std.stdio;
writefln("%(%s\n%)", [1, 2, 3].permutations);
}
}

View file

@ -1,4 +1,4 @@
gap>perms := n -> List(SymmetricGroup(n), p -> List([1..n], x -> x^p));
gap>List(SymmetricGroup(4), p -> Permuted([1 .. 4], p));
perms(4);
[ [ 1, 2, 3, 4 ], [ 4, 2, 3, 1 ], [ 2, 4, 3, 1 ], [ 3, 2, 4, 1 ], [ 1, 4, 3, 2 ], [ 4, 1, 3, 2 ], [ 2, 1, 3, 4 ],
[ 3, 1, 4, 2 ], [ 1, 3, 4, 2 ], [ 4, 3, 1, 2 ], [ 2, 3, 1, 4 ], [ 3, 4, 1, 2 ], [ 1, 2, 4, 3 ], [ 4, 2, 1, 3 ],

View file

@ -1,4 +1,4 @@
# All arrangements of 4 elements in 1..4
Arrangements([1..4], 4);
# All permutations of 1..4
PermutationsList([1..4]);
# All arrangements of 4 elements in 1 .. 4
Arrangements([1 .. 4], 4);
# All permutations of 1 .. 4
PermutationsList([1 .. 4]);

View file

@ -1,5 +1,5 @@
import Data.List (delete)
permutations :: Eq a -> [a] -> [[a]]
permutations :: Eq a => [a] -> [[a]]
permutations [] = [[]]
permutations xs = [ x:ys | x <- xs, ys <- permutations (delete x xs)]

View file

@ -0,0 +1 @@
perms([1,2,3,4])

View file

@ -0,0 +1,13 @@
sub permute(@items) {
my @seq := 1..+@items;
gather for (^[*] @seq) -> $n is copy {
my @order;
for @seq {
unshift @order, $n mod $_;
$n div= $_;
}
my @i-copy = @items;
take [ map { @i-copy.splice($_, 1) }, @order ];
}
}
.say for permute( 'a'..'c' )

View file

@ -1,26 +1,36 @@
defint a-z
option base 1
input "n=",n
dim a(n)
for i=1 to n: a(i)=i: next
do
for i=1 to n: print a(i);: next: print
i=n
do
decr i
loop until i=0 or a(i)<a(i+1)
j=i+1
k=n
while j<k
swap a(j),a(k)
incr j
decr k
wend
if i>0 then
j=i+1
while a(j)<a(i)
incr j
wend
swap a(i),a(j)
end if
loop until i=0
#COMPILE EXE
#DIM ALL
GLOBAL a, i, j, k, n AS INTEGER
GLOBAL d, ns, s AS STRING 'dynamic string
FUNCTION PBMAIN () AS LONG
ns = INPUTBOX$(" n =",, "3") 'input n
n = VAL(ns)
DIM a(1 TO n) AS INTEGER
FOR i = 1 TO n: a(i)= i: NEXT
DO
s = " "
FOR i = 1 TO n
d = STR$(a(i))
s = BUILD$(s, d) ' s & d concatenate
NEXT
? s 'print and pause
i = n
DO
DECR i
LOOP UNTIL i = 0 OR a(i) < a(i+1)
j = i+1
k = n
DO WHILE j < k
SWAP a(j), a(k)
INCR j
DECR k
LOOP
IF i > 0 THEN
j = i+1
DO WHILE a(j) < a(i)
INCR j
LOOP
SWAP a(i), a(j)
END IF
LOOP UNTIL i = 0
END FUNCTION

View file

@ -1,26 +1,20 @@
/*REXX program generates all permutations of N different objects. */
/*REXX program generates all permutations of N different objects. */
parse arg things bunch inbetweenChars names
/* inbetweenChars (optional) defaults to a [null]. */
/* names (optional) defaults to digits (and letters). */
call permSets things,bunch,inbetweenChars,names
call permSets things, bunch, inbetweenChars, names
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────.PERMSET subroutine─────────────────*/
.permset: procedure expose (list); parse arg ?
if ?>y then do; _=@.1; do j=2 to y; _=_||between||@.j; end; say _; end
else do q=1 for x /*build permutation recursively. */
do k=1 for ?-1; if @.k==$.q then iterate q; end /*k*/
@.?=$.q; call .permset ?+1
end /*q*/
return
/*──────────────────────────────────P subroutine (Pick one)─────────────*/
p: return word(arg(1),1)
/*──────────────────────────────────PERMSETS subroutine─────────────────*/
permSets: procedure; parse arg x,y,between,uSyms /*X things Y at a time.*/
@.=; sep= /*X can't be > length(@0abcs). */
@abc = 'abcdefghijklmnopqrstuvwxyz'; @abcU=@abc; upper @abcU
@abcS = @abcU || @abc; @0abcS=123456789 || @abcS
do k=1 for x /*build a list of (perm) symbols.*/
do k=1 for x /*build a list of (perm) symbols.*/
_=p(word(uSyms,k) p(substr(@0abcS,k,1) k)) /*get|generate a symbol.*/
if length(_)\==1 then sep='_' /*if not 1st char, then use sep. */
$.k=_ /*append it to the symbol list. */
@ -30,5 +24,11 @@ if between=='' then between=sep /*use the appropriate separator. */
list='$. @. between x y'
call .permset 1
return
/*──────────────────────────────────P subroutine (Pick one)─────────────*/
p: return word(arg(1),1)
/*──────────────────────────────────.PERMSET subroutine─────────────────*/
.permset: procedure expose (list); parse arg ?
if ?>y then do; _=@.1; do j=2 to y; _=_||between||@.j; end; say _; end
else do q=1 for x /*build permutation recursively. */
do k=1 for ?-1; if @.k==$.q then iterate q; end /*k*/
@.?=$.q; call .permset ?+1
end /*q*/
return

View file

@ -1,22 +1,22 @@
/*REXX program shows permutations of '''N''' number of objects (1,2,3, ...).*/
parse arg n .; if n=='' then n=3 /*Not specified? Assume default*/
/*REXX program shows permutations of N number of objects (1,2,3, ...).*/
parse arg n .; if n=='' then n=3 /*Not specified? Assume default.*/
/*populate the first permutation.*/
do pop=1 for n; @.pop=pop ; end; call tell n
do pop=1 for n; @.pop=pop ; end; call tell n
do while nextperm(n,0); call tell n; end
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────NEXTPERM subroutine─────────────────*/
nextperm: procedure expose @.; parse arg n,i; nm=n-1
do k=nm by -1 for nm; kp=k+1
if @.k<@.kp then do; i=k; leave; end
do k=nm by -1 for nm; kp=k+1
if @.k<@.kp then do; i=k; leave; end
end /*k*/
do j=i+1 while j<n; parse value @.j @.n with @.n @.j; n=n-1; end
if i==0 then return 0
do j=i+1 while @.j<@.i; end
parse value @.j @.i with @.i @.j
if i==0 then return 0
do j=i+1 while @.j<@.i; end
parse value @.j @.i with @.i @.j
return 1
/*──────────────────────────────────TELL subroutine─────────────────────*/
tell: procedure expose @.; _=; do j=1 for arg(1);_=_ @.j;end; say _;return

View file

@ -15,11 +15,11 @@ for i = 1 to d ' print the Permutations
for k = 1 to d
perm$ = perm$ + theList$(k)
next k
if instr(perms$,perm$+",") = 0 then print perm$ ' only list 1 time
perms$ = perms$ + perm$ + ","
if instr(perm2$,perm$+",") = 0 then print perm$ ' only list 1 time
perm2$ = perm2$ + perm$ + ","
h$ = theList$(j)
theList$(j) = theList$(j - 1)
theList$(j-1) = h$
theList$(j - 1) = h$
next j
next i
end