2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,7 +1,11 @@
|
|||
Write a program that generates all [[wp:Permutation|permutations]] of '''n''' different objects. (Practically numerals!)
|
||||
;Cf.
|
||||
* [[Find the missing permutation]]
|
||||
* [[Permutations/Derangements]]
|
||||
;Task:
|
||||
Write a program that generates all [[wp:Permutation|permutations]] of '''n''' different objects. (Practically numerals!)
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Find the missing permutation]]
|
||||
* [[Permutations/Derangements]]
|
||||
|
||||
|
||||
'''See Also:'''
|
||||
{{Template:Combinations and permutations}}
|
||||
<br><br>
|
||||
|
|
|
|||
114
Task/Permutations/AppleScript/permutations.applescript
Normal file
114
Task/Permutations/AppleScript/permutations.applescript
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
-- permutations :: [a] -> [[a]]
|
||||
on permutations(xs)
|
||||
script firstElement
|
||||
on lambda(x)
|
||||
script tailElements
|
||||
on lambda(ys)
|
||||
{x & ys}
|
||||
end lambda
|
||||
end script
|
||||
|
||||
concatMap(tailElements, permutations(|delete|(x, xs)))
|
||||
end lambda
|
||||
end script
|
||||
|
||||
if length of xs > 0 then
|
||||
concatMap(firstElement, xs)
|
||||
else
|
||||
{{}}
|
||||
end if
|
||||
end permutations
|
||||
|
||||
|
||||
-- TEST
|
||||
on run
|
||||
|
||||
return permutations({1, 2, 3})
|
||||
permutations({"aardvarks", "eat", "ants"})
|
||||
|
||||
end run
|
||||
|
||||
|
||||
|
||||
-- GENERIC LIBRARY FUNCTIONS
|
||||
|
||||
-- concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
on concatMap(f, xs)
|
||||
script append
|
||||
on lambda(a, b)
|
||||
a & b
|
||||
end lambda
|
||||
end script
|
||||
|
||||
foldl(append, {}, map(f, xs))
|
||||
end concatMap
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to lambda(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
-- delete :: a -> [a] -> [a]
|
||||
on |delete|(x, xs)
|
||||
script Eq
|
||||
on lambda(a, b)
|
||||
a = b
|
||||
end lambda
|
||||
end script
|
||||
|
||||
deleteBy(Eq, x, xs)
|
||||
end |delete|
|
||||
|
||||
-- deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a]
|
||||
on deleteBy(fnEq, x, xs)
|
||||
if length of xs > 0 then
|
||||
set {h, t} to uncons(xs)
|
||||
if lambda(x, h) of mReturn(fnEq) then
|
||||
t
|
||||
else
|
||||
{h} & deleteBy(fnEq, x, t)
|
||||
end if
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end deleteBy
|
||||
|
||||
-- uncons :: [a] -> Maybe (a, [a])
|
||||
on uncons(xs)
|
||||
if length of xs > 0 then
|
||||
{item 1 of xs, rest of xs}
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end uncons
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
33
Task/Permutations/Batch-File/permutations.bat
Normal file
33
Task/Permutations/Batch-File/permutations.bat
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
@echo off
|
||||
setlocal enabledelayedexpansion
|
||||
set arr=ABCD
|
||||
set /a n=4
|
||||
:: echo !arr!
|
||||
call :permu %n% arr
|
||||
goto:eof
|
||||
|
||||
:permu num &arr
|
||||
setlocal
|
||||
if %1 equ 1 call echo(!%2! & exit /b
|
||||
set /a "num=%1-1,n2=num-1"
|
||||
set arr=!%2!
|
||||
for /L %%c in (0,1,!n2!) do (
|
||||
call:permu !num! arr
|
||||
set /a n1="num&1"
|
||||
if !n1! equ 0 (call:swapit !num! 0 arr) else (call:swapit !num! %%c arr)
|
||||
)
|
||||
call:permu !num! arr
|
||||
endlocal & set %2=%arr%
|
||||
exit /b
|
||||
|
||||
:swapit from to &arr
|
||||
setlocal
|
||||
set arr=!%3!
|
||||
set temp1=!arr:~%~1,1!
|
||||
set temp2=!arr:~%~2,1!
|
||||
set arr=!arr:%temp1%=@!
|
||||
set arr=!arr:%temp2%=%temp1%!
|
||||
set arr=!arr:@=%temp2%!
|
||||
:: echo %1 %2 !%~3! !arr!
|
||||
endlocal & set %3=%arr%
|
||||
exit /b
|
||||
|
|
@ -5,18 +5,18 @@ var d = document.getElementById('result');
|
|||
|
||||
function perm(list, ret)
|
||||
{
|
||||
if (list.length == 0) {
|
||||
var row = document.createTextNode(ret.join(' ') + '\n');
|
||||
d.appendChild(row);
|
||||
return;
|
||||
}
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
var x = list.splice(i, 1);
|
||||
ret.push(x);
|
||||
perm(list, ret);
|
||||
ret.pop();
|
||||
list.splice(i, 0, x);
|
||||
}
|
||||
if (list.length == 0) {
|
||||
var row = document.createTextNode(ret.join(' ') + '\n');
|
||||
d.appendChild(row);
|
||||
return;
|
||||
}
|
||||
for (var i = 0; i < list.length; i++) {
|
||||
var x = list.splice(i, 1);
|
||||
ret.push(x);
|
||||
perm(list, ret);
|
||||
ret.pop();
|
||||
list.splice(i, 0, x);
|
||||
}
|
||||
}
|
||||
|
||||
perm([1, 2, 'A', 4], []);
|
||||
|
|
|
|||
|
|
@ -1,30 +1,12 @@
|
|||
(function () {
|
||||
function perm(a) {
|
||||
if (a.length < 2) return [a];
|
||||
var c, d, b = [];
|
||||
for (c = 0; c < a.length; c++) {
|
||||
var e = a.splice(c, 1),
|
||||
f = perm(a);
|
||||
for (d = 0; d < f.length; d++) b.push([e].concat(f[d]));
|
||||
a.splice(c, 0, e[0])
|
||||
} return b
|
||||
}
|
||||
|
||||
// [a] -> [[a]]
|
||||
function permutations(xs) {
|
||||
return xs.length ? (
|
||||
chain( xs, function (x) {
|
||||
return chain( permutations(deleted(x, xs)), function (ys) {
|
||||
|
||||
return ( [[x].concat(ys)] );
|
||||
|
||||
})})) : [[]]
|
||||
}
|
||||
|
||||
// monadic bind/chain for lists
|
||||
function chain(xs, f) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
}
|
||||
|
||||
// drops first instance found
|
||||
function deleted(x, xs) {
|
||||
return xs.length ? (
|
||||
x === xs[0] ? xs.slice(1) : [xs[0]].concat(
|
||||
deleted(x, xs.slice(1))
|
||||
)
|
||||
) : [];
|
||||
}
|
||||
|
||||
return permutations(['Aardvarks', 'eat', 'ants'])
|
||||
|
||||
})();
|
||||
console.log(perm(['Aardvarks', 'eat', 'ants']).join("\n"));
|
||||
|
|
|
|||
|
|
@ -1 +1,6 @@
|
|||
[["Aardvarks", "eat", "ants"], ["Aardvarks", "ants", "eat"], ["eat", "Aardvarks", "ants"], ["eat", "ants", "Aardvarks"], ["ants", "Aardvarks", "eat"], ["ants", "eat", "Aardvarks"]]
|
||||
Aardvarks,eat,ants
|
||||
Aardvarks,ants,eat
|
||||
eat,Aardvarks,ants
|
||||
eat,ants,Aardvarks
|
||||
ants,Aardvarks,eat
|
||||
ants,eat,Aardvarks
|
||||
|
|
|
|||
39
Task/Permutations/JavaScript/permutations-4.js
Normal file
39
Task/Permutations/JavaScript/permutations-4.js
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
(function () {
|
||||
|
||||
// permutations :: [a] -> [[a]]
|
||||
function permutations(xs) {
|
||||
return xs.length ? (concatMap(
|
||||
function (x) {
|
||||
return concatMap(
|
||||
function (ys) {
|
||||
return ([[x].concat(ys)]);
|
||||
}, permutations(delete1(x, xs)))
|
||||
}, xs)) : [[]]
|
||||
}
|
||||
|
||||
|
||||
// GENERIC LIBRARY FUNCTIONS
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
function concatMap(f, xs) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
}
|
||||
|
||||
// delete first instance of a in [a]
|
||||
// delete1 :: a -> [a] -> [a]
|
||||
function delete1(x, xs) {
|
||||
return deleteBy(function (a, b) {
|
||||
return a === b;
|
||||
}, x, xs);
|
||||
}
|
||||
|
||||
// deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a]
|
||||
function deleteBy(fnEq, x, xs) {
|
||||
return xs.length ? fnEq(x, xs[0]) ? xs.slice(1) : [xs[0]]
|
||||
.concat(deleteBy(fnEq, x, xs.slice(1))) : [];
|
||||
}
|
||||
|
||||
|
||||
return permutations(['Aardvarks', 'eat', 'ants'])
|
||||
|
||||
})();
|
||||
3
Task/Permutations/JavaScript/permutations-5.js
Normal file
3
Task/Permutations/JavaScript/permutations-5.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[["Aardvarks", "eat", "ants"], ["Aardvarks", "ants", "eat"],
|
||||
["eat", "Aardvarks", "ants"], ["eat", "ants", "Aardvarks"],
|
||||
["ants", "Aardvarks", "eat"], ["ants", "eat", "Aardvarks"]]
|
||||
19
Task/Permutations/JavaScript/permutations-6.js
Normal file
19
Task/Permutations/JavaScript/permutations-6.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
(function (lst) {
|
||||
'use strict';
|
||||
|
||||
const permutations = (xs) => xs.length ? (
|
||||
flatMap((x) => flatMap((xs) => [[x].concat(xs)],
|
||||
permutations(del(x, xs))), xs)
|
||||
) : [[]],
|
||||
|
||||
flatMap = (f, xs) => [].concat.apply([], xs.map(f)),
|
||||
|
||||
del = (x, xs) => xs.length ? x === xs[0] ? (
|
||||
xs.slice(1)
|
||||
) : [xs[0]].concat(del(x, xs.slice(1))
|
||||
) : [];
|
||||
|
||||
|
||||
return permutations(lst);
|
||||
|
||||
})(["Aardvarks", "eat", "ants"]);
|
||||
3
Task/Permutations/JavaScript/permutations-7.js
Normal file
3
Task/Permutations/JavaScript/permutations-7.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[["Aardvarks", "eat", "ants"], ["Aardvarks", "ants", "eat"],
|
||||
["eat", "Aardvarks", "ants"], ["eat", "ants", "Aardvarks"],
|
||||
["ants", "Aardvarks", "eat"], ["ants", "eat", "Aardvarks"]]
|
||||
25
Task/Permutations/K/permutations-2.k
Normal file
25
Task/Permutations/K/permutations-2.k
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
perm:{x@m@&n=(#?:)'m:!n#n:#x}
|
||||
|
||||
perm[!3]
|
||||
(0 1 2
|
||||
0 2 1
|
||||
1 0 2
|
||||
1 2 0
|
||||
2 0 1
|
||||
2 1 0)
|
||||
|
||||
perm "abc"
|
||||
("abc"
|
||||
"acb"
|
||||
"bac"
|
||||
"bca"
|
||||
"cab"
|
||||
"cba")
|
||||
|
||||
`0:{1_,/" ",/: $x}' perm `$" "\"some random text"
|
||||
some random text
|
||||
some text random
|
||||
random some text
|
||||
random text some
|
||||
text some random
|
||||
text random some
|
||||
60
Task/Permutations/Modula-2/permutations.mod2
Normal file
60
Task/Permutations/Modula-2/permutations.mod2
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
MODULE Permute;
|
||||
|
||||
FROM Terminal
|
||||
IMPORT Read, Write, WriteLn;
|
||||
|
||||
FROM Terminal2
|
||||
IMPORT WriteString;
|
||||
|
||||
CONST MAXIDX = 6;
|
||||
MINIDX = 1;
|
||||
|
||||
TYPE TInpCh = ['a'..'z'];
|
||||
TChr = SET OF TInpCh;
|
||||
|
||||
VAR n,
|
||||
nl: INTEGER;
|
||||
ch: CHAR;
|
||||
a: ARRAY[MINIDX..MAXIDX] OF CHAR;
|
||||
kt: TChr = TChr{'a'..'f'};
|
||||
|
||||
PROCEDURE output;
|
||||
VAR i: INTEGER;
|
||||
BEGIN
|
||||
FOR i := MINIDX TO n DO Write(a[i]) END;
|
||||
WriteString(" | ");
|
||||
END output;
|
||||
|
||||
PROCEDURE exchange(VAR x, y : CHAR);
|
||||
VAR z: CHAR;
|
||||
BEGIN z := x; x := y; y := z
|
||||
END exchange;
|
||||
|
||||
PROCEDURE permute(k: INTEGER);
|
||||
VAR i: INTEGER;
|
||||
BEGIN
|
||||
IF k = 1 THEN
|
||||
output;
|
||||
INC(nl);
|
||||
IF (nl MOD 8 = 1) THEN WriteLn END;
|
||||
ELSE
|
||||
permute(k-1);
|
||||
FOR i := MINIDX TO k-1 DO
|
||||
exchange(a[i], a[k]);
|
||||
permute(k-1);
|
||||
exchange(a[i], a[k]);
|
||||
END
|
||||
END
|
||||
END permute;
|
||||
|
||||
BEGIN
|
||||
n := 0; nl := 1; WriteString("Input {a,b,c,d,e,f} >");
|
||||
REPEAT
|
||||
Read(ch);
|
||||
IF ch IN kt THEN INC(n); a[n] := ch; Write(ch) END
|
||||
UNTIL (ch <= " ") OR (n > MAXIDX);
|
||||
|
||||
WriteLn;
|
||||
IF n > 0 THEN permute(n) END;
|
||||
(*Wait*)
|
||||
END Permute.
|
||||
23
Task/Permutations/PHP/permutations.php
Normal file
23
Task/Permutations/PHP/permutations.php
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
//Author Gavryushin Ivan @dcc0
|
||||
<?php
|
||||
$b="0123";
|
||||
$a=strrev($b);
|
||||
|
||||
while ($a !=$b) {
|
||||
$i=1;
|
||||
while($a[$i] > $a[$i-1]) {
|
||||
$i++;
|
||||
}
|
||||
$j=0;
|
||||
while($a[$j] < $a[$i]) {
|
||||
|
||||
$j++;
|
||||
}
|
||||
$c=$a[$j];
|
||||
$a[$j]=$a[$i];
|
||||
$a[$i]=$c;
|
||||
$a=strrev(substr($a, 0, $i)).substr($a, $i);
|
||||
print $a. "\n";
|
||||
|
||||
}
|
||||
?>
|
||||
71
Task/Permutations/Pascal/permutations-2.pascal
Normal file
71
Task/Permutations/Pascal/permutations-2.pascal
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
{$IFDEF FPC}
|
||||
{$MODE DELPHI}
|
||||
{$ELSE}
|
||||
{$APPTYPE CONSOLE}
|
||||
{$ENDIF}
|
||||
uses
|
||||
sysutils;
|
||||
type
|
||||
tPermfield = array[0..15] of Nativeint;
|
||||
var
|
||||
permcnt: NativeUint;
|
||||
|
||||
procedure DoSomething(k: NativeInt;var x:tPermfield);
|
||||
var
|
||||
i:integer;
|
||||
kk:string;
|
||||
begin
|
||||
kk:='';
|
||||
for i:=1 to k do kk:=kk+inttostr(x[i])+' ';
|
||||
writeln(kk);
|
||||
end;
|
||||
|
||||
procedure PermKoutOfN(k,n: nativeInt);
|
||||
var
|
||||
x,y:tPermfield;
|
||||
i,yi,tmp:NativeInt;
|
||||
begin
|
||||
//initialise
|
||||
permcnt:= 1;
|
||||
if k>n then
|
||||
k:=n;
|
||||
if k=n then
|
||||
k:=k-1;
|
||||
for i:=1 to n do x[i]:=i;
|
||||
for i:=1 to k do y[i]:=i;
|
||||
|
||||
// DoSomething(k,x);
|
||||
i := k;
|
||||
repeat
|
||||
yi:=y[i];
|
||||
if yi <n then
|
||||
begin
|
||||
inc(permcnt);
|
||||
inc(yi);
|
||||
y[i]:=yi;
|
||||
tmp:=x[i];x[i]:=x[yi];x[yi]:=tmp;
|
||||
i:=k;
|
||||
// DoSomething(k,x);
|
||||
end
|
||||
else
|
||||
begin
|
||||
repeat
|
||||
tmp:=x[i];x[i]:=x[yi];x[yi]:=tmp;
|
||||
dec(yi);
|
||||
until yi<=i;
|
||||
y[i]:=yi;
|
||||
dec(i);
|
||||
end;
|
||||
until (i=0);
|
||||
end;
|
||||
|
||||
var
|
||||
t1,t0 : TDateTime;
|
||||
Begin
|
||||
permcnt:= 0;
|
||||
T0 := now;
|
||||
PermKoutOfN(12,12);
|
||||
T1 := now;
|
||||
writeln(permcnt);
|
||||
writeln(FormatDateTime('HH:NN:SS.zzz',T1-T0));
|
||||
end.
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
sub permute(@items) {
|
||||
sub permute(+@items) {
|
||||
my @seq := 1..+@items;
|
||||
gather for (^[*] @seq) -> $n is copy {
|
||||
my @order;
|
||||
|
|
@ -7,7 +7,7 @@ sub permute(@items) {
|
|||
$n div= $_;
|
||||
}
|
||||
my @i-copy = @items;
|
||||
take [ map { @i-copy.splice($_, 1) }, @order ];
|
||||
take map { |@i-copy.splice($_, 1) }, @order;
|
||||
}
|
||||
}
|
||||
.say for permute( 'a'..'c' )
|
||||
|
|
|
|||
|
|
@ -1,42 +1,42 @@
|
|||
next.perm <- function(p) {
|
||||
n <- length(p)
|
||||
i <- n - 1
|
||||
r = TRUE
|
||||
for(i in (n-1):1) {
|
||||
if(p[i] < p[i+1]) {
|
||||
r = FALSE
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
j <- i + 1
|
||||
k <- n
|
||||
while(j < k) {
|
||||
x <- p[j]
|
||||
p[j] <- p[k]
|
||||
p[k] <- x
|
||||
j <- j + 1
|
||||
k <- k - 1
|
||||
}
|
||||
|
||||
if(r) return(NULL)
|
||||
|
||||
j <- n
|
||||
while(p[j] > p[i]) j <- j - 1
|
||||
j <- j + 1
|
||||
|
||||
x <- p[i]
|
||||
p[i] <- p[j]
|
||||
p[j] <- x
|
||||
return(p)
|
||||
n <- length(p)
|
||||
i <- n - 1
|
||||
r = T
|
||||
for (i in seq(n - 1, 1)) {
|
||||
if (p[i] < p[i + 1]) {
|
||||
r = F
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
j <- i + 1
|
||||
k <- n
|
||||
while (j < k) {
|
||||
x <- p[j]
|
||||
p[j] <- p[k]
|
||||
p[k] <- x
|
||||
j <- j + 1
|
||||
k <- k - 1
|
||||
}
|
||||
|
||||
if(r) return(NULL)
|
||||
|
||||
j <- n
|
||||
while (p[j] > p[i]) j <- j - 1
|
||||
j <- j + 1
|
||||
|
||||
x <- p[i]
|
||||
p[i] <- p[j]
|
||||
p[j] <- x
|
||||
return(p)
|
||||
}
|
||||
|
||||
print.perms <- function(n) {
|
||||
p <- 1:n
|
||||
while(!is.null(p)) {
|
||||
cat(p,"\n")
|
||||
p <- next.perm(p)
|
||||
}
|
||||
p <- 1:n
|
||||
while (!is.null(p)) {
|
||||
cat(p, "\n")
|
||||
p <- next.perm(p)
|
||||
}
|
||||
}
|
||||
|
||||
print.perms(3)
|
||||
|
|
|
|||
|
|
@ -1,33 +1,32 @@
|
|||
/*REXX program generates all permutations of N different objects. */
|
||||
/*REXX program generates and displays 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). */
|
||||
/* inbetweenChars (optional) defaults to a [null]. */
|
||||
/* names (optional) defaults to digits (and letters).*/
|
||||
|
||||
call permSets things, bunch, inbetweenChars, names
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────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
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
p: return word(arg(1),1) /*P function (Pick first arg of many).*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
permSets: procedure; parse arg x,y,between,uSyms /*X things taken 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.*/
|
||||
_=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. */
|
||||
end /*k*/
|
||||
do k=1 for x /*build a list of permutation symbols. */
|
||||
_=p(word(uSyms,k) p(substr(@0abcS,k,1) k)) /*get or generate a symbol.*/
|
||||
if length(_)\==1 then sep='_' /*if not 1st character, then use sep. */
|
||||
$.k=_ /*append the character to symbol list. */
|
||||
end /*k*/
|
||||
|
||||
if between=='' then between=sep /*use the appropriate separator. */
|
||||
call .permset 1 /*start with the first permuation*/
|
||||
return
|
||||
/*──────────────────────────────────.PERMSET subroutine─────────────────*/
|
||||
.permset: procedure expose $. @. between x y; 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
|
||||
if between=='' then between=sep /*use the appropriate separator chars. */
|
||||
call .permset 1 /*start with the first permuation. */
|
||||
return
|
||||
.permset: procedure expose $. @. between x y; parse arg ?
|
||||
if ?>y then do; _=@.1; do j=2 to y; _=_ || between || @.j; end; say _; end
|
||||
else do q=1 for x /*build the permutation recursively. */
|
||||
do k=1 for ?-1; if @.k==$.q then iterate q; end /*k*/
|
||||
@.?=$.q; call .permset ?+1
|
||||
end /*q*/
|
||||
return
|
||||
|
|
|
|||
|
|
@ -1,22 +1,16 @@
|
|||
/*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 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
|
||||
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
|
||||
return 1
|
||||
/*──────────────────────────────────TELL subroutine─────────────────────*/
|
||||
tell: procedure expose @.; _=; do j=1 for arg(1);_=_ @.j;end; say _;return
|
||||
/*REXX program displays permutations of N number of objects (1, 2, 3, ···). */
|
||||
parse arg n .; if n=='' | n=="," then n=3 /*Not specified? Then use the default.*/
|
||||
/* [↓] populate the first permutation.*/
|
||||
do pop=1 for n; @.pop=pop ; end /*pop */; call tell n
|
||||
do while nPerm(n, 0); call tell n; end /*while*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
nPerm: 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; end /*k*/
|
||||
do j=i+1 while j<n; parse value @.j @.n with @.n @.j; n=n-1; end /*j*/
|
||||
if i==0 then return 0
|
||||
do m=i+1 while @.m<@.i; end /*m*/
|
||||
parse value @.m @.i with @.i @.m
|
||||
return 1
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
tell: procedure expose @.; _=; do j=1 for arg(1); _=_ @.j; end; say _; return
|
||||
|
|
|
|||
1
Task/Permutations/Scala/permutations-1.scala
Normal file
1
Task/Permutations/Scala/permutations-1.scala
Normal file
|
|
@ -0,0 +1 @@
|
|||
List(1, 2, 3).permutations.foreach(println)
|
||||
11
Task/Permutations/Scala/permutations-2.scala
Normal file
11
Task/Permutations/Scala/permutations-2.scala
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def permutations[T]: List[T] => Traversable[List[T]] = {
|
||||
case Nil => List(Nil)
|
||||
case xs => {
|
||||
for {
|
||||
(x, i) <- xs.zipWithIndex
|
||||
ys <- permutations(xs.take(i) ++ xs.drop(1 + i))
|
||||
} yield {
|
||||
x :: ys
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
List('a, 'b, 'c).permutations foreach println
|
||||
|
|
@ -1,63 +1,81 @@
|
|||
Public Sub Permute(n As Integer, Optional printem As Boolean = True)
|
||||
'generate, count and print (if printem is not false) all permutations of first n integers
|
||||
'Generate, count and print (if printem is not false) all permutations of first n integers
|
||||
|
||||
Dim P() As Integer
|
||||
Dim t As Integer, i As Integer, j As Integer, k As Integer
|
||||
Dim count As Long
|
||||
dim Last as boolean
|
||||
Dim t, i, j, k As Integer
|
||||
Dim Last As Boolean
|
||||
|
||||
If n <= 1 Then
|
||||
Debug.Print "give a number greater than 1!"
|
||||
|
||||
Debug.Print "Please give a number greater than 1"
|
||||
Exit Sub
|
||||
|
||||
End If
|
||||
|
||||
'initialize
|
||||
'Initialize
|
||||
ReDim P(n)
|
||||
For i = 1 To n: P(i) = i: Next
|
||||
|
||||
For i = 1 To n
|
||||
P(i) = i
|
||||
Next
|
||||
|
||||
count = 0
|
||||
Last = False
|
||||
|
||||
Do While Not Last
|
||||
'print?
|
||||
If printem Then
|
||||
For t = 1 To n: Debug.Print P(t);: Next
|
||||
Debug.Print
|
||||
End If
|
||||
count = count + 1
|
||||
'print?
|
||||
If printem Then
|
||||
|
||||
For t = 1 To n
|
||||
Debug.Print P(t);
|
||||
Next
|
||||
|
||||
Debug.Print
|
||||
|
||||
Last = True
|
||||
i = n - 1
|
||||
Do While i > 0
|
||||
If P(i) < P(i + 1) Then
|
||||
Last = False
|
||||
Exit Do
|
||||
End If
|
||||
i = i - 1
|
||||
Loop
|
||||
|
||||
If Not Last Then
|
||||
j = i + 1
|
||||
k = n
|
||||
While j < k
|
||||
' swap p(j) and p(k)
|
||||
t = P(j)
|
||||
P(j) = P(k)
|
||||
P(k) = t
|
||||
j = j + 1
|
||||
k = k - 1
|
||||
Wend
|
||||
j = n
|
||||
While P(j) > P(i)
|
||||
j = j - 1
|
||||
Wend
|
||||
j = j + 1
|
||||
'swap p(i) and p(j)
|
||||
t = P(i)
|
||||
P(i) = P(j)
|
||||
P(j) = t
|
||||
End If 'not last
|
||||
count = count + 1
|
||||
|
||||
Loop 'while not last
|
||||
Last = True
|
||||
i = n - 1
|
||||
|
||||
Do While i > 0
|
||||
|
||||
If P(i) < P(i + 1) Then
|
||||
|
||||
Last = False
|
||||
Exit Do
|
||||
|
||||
End If
|
||||
|
||||
i = i - 1
|
||||
Loop
|
||||
|
||||
j = i + 1
|
||||
k = n
|
||||
|
||||
While j < k
|
||||
' Swap p(j) and p(k)
|
||||
t = P(j)
|
||||
P(j) = P(k)
|
||||
P(k) = t
|
||||
j = j + 1
|
||||
k = k - 1
|
||||
Wend
|
||||
|
||||
j = n
|
||||
|
||||
While P(j) > P(i)
|
||||
j = j - 1
|
||||
Wend
|
||||
|
||||
j = j + 1
|
||||
'Swap p(i) and p(j)
|
||||
t = P(i)
|
||||
P(i) = P(j)
|
||||
P(j) = t
|
||||
Loop 'While not last
|
||||
|
||||
Debug.Print "Number of permutations: "; count
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue