Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -6,41 +6,46 @@ struct Permutations(bool doCopy=true, T) if (isMutable!T) {
|
|||
private uint[31] indexes;
|
||||
private ulong tot;
|
||||
|
||||
this (in T[] items) pure /*nothrow*/
|
||||
this (T[] items) pure nothrow @safe @nogc
|
||||
in {
|
||||
static enum string L = text(indexes.length); // impure
|
||||
static enum string L = indexes.length.text;
|
||||
assert(items.length >= 0 && items.length <= indexes.length,
|
||||
"Permutations: items.length must be >= 0 && < " ~ L);
|
||||
} body {
|
||||
static ulong factorial(in uint n) pure nothrow {
|
||||
static ulong factorial(in size_t n) pure nothrow @safe @nogc {
|
||||
ulong result = 1;
|
||||
foreach (i; 2 .. n + 1)
|
||||
foreach (immutable i; 2 .. n + 1)
|
||||
result *= i;
|
||||
return result;
|
||||
}
|
||||
|
||||
this.num = items.length;
|
||||
this.items = items.dup;
|
||||
foreach (i; 0 .. cast(typeof(indexes[0]))this.num)
|
||||
this.items = items;
|
||||
foreach (immutable i; 0 .. cast(typeof(indexes[0]))this.num)
|
||||
this.indexes[i] = i;
|
||||
this.tot = factorial(this.num);
|
||||
}
|
||||
|
||||
@property T[] front() pure nothrow {
|
||||
@property T[] front() pure nothrow @safe {
|
||||
static if (doCopy) {
|
||||
//return items.dup; // Not nothrow.
|
||||
auto items2 = new T[items.length];
|
||||
items2[] = items[];
|
||||
return items2;
|
||||
return items.dup;
|
||||
} else
|
||||
return items;
|
||||
}
|
||||
|
||||
@property bool empty() const pure nothrow {
|
||||
@property bool empty() const pure nothrow @safe @nogc {
|
||||
return tot == 0;
|
||||
}
|
||||
|
||||
void popFront() pure nothrow {
|
||||
@property size_t length() const pure nothrow @safe @nogc {
|
||||
// Not cached to keep the function pure.
|
||||
typeof(return) result = 1;
|
||||
foreach (immutable x; 1 .. items.length + 1)
|
||||
result *= x;
|
||||
return result;
|
||||
}
|
||||
|
||||
void popFront() pure nothrow @safe @nogc {
|
||||
tot--;
|
||||
if (tot > 0) {
|
||||
size_t j = num - 2;
|
||||
|
|
@ -66,8 +71,8 @@ struct Permutations(bool doCopy=true, T) if (isMutable!T) {
|
|||
}
|
||||
|
||||
Permutations!(doCopy,T) permutations(bool doCopy=true, T)
|
||||
(in T[] items)
|
||||
pure /*nothrow*/ if (isMutable!T) {
|
||||
(T[] items)
|
||||
pure nothrow if (isMutable!T) {
|
||||
return Permutations!(doCopy, T)(items);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
import std.stdio, std.algorithm;
|
||||
|
||||
void main() {
|
||||
import std.stdio, std.algorithm;
|
||||
|
||||
auto items = [1, 2, 3];
|
||||
do
|
||||
writeln(items);
|
||||
while (items.nextPermutation());
|
||||
items.writeln;
|
||||
while (items.nextPermutation);
|
||||
}
|
||||
|
|
|
|||
34
Task/Permutations/Eiffel/permutations.e
Normal file
34
Task/Permutations/Eiffel/permutations.e
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
permute(a: ARRAY[INTEGER]; k: INTEGER)
|
||||
require
|
||||
ar_not_void: a.count>=1
|
||||
k_valid_index: k>0
|
||||
local
|
||||
i,t: INTEGER
|
||||
do
|
||||
if k=a.count then
|
||||
across a as ar loop io.put_string (ar.item.out) end
|
||||
io.put_string ("%N")
|
||||
else
|
||||
from
|
||||
i:= k
|
||||
until
|
||||
i> a.count
|
||||
loop
|
||||
t:= a[k]
|
||||
a[k]:= a[i]
|
||||
a[i]:= t
|
||||
permute(a,k+1)
|
||||
t:= a[k]
|
||||
a[k]:= a[i]
|
||||
a[i]:= t
|
||||
i:= i+1
|
||||
end
|
||||
end
|
||||
make
|
||||
do
|
||||
test:= << 2,5,1>>
|
||||
permute(test, 1)
|
||||
end
|
||||
test: ARRAY[INTEGER]
|
||||
|
||||
end
|
||||
31
Task/Permutations/Lua/permutations-2.lua
Normal file
31
Task/Permutations/Lua/permutations-2.lua
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
-- Iterative version
|
||||
function ipermutations(a,b)
|
||||
if a==0 then return end
|
||||
local taken = {} local slots = {}
|
||||
for i=1,a do slots[i]=0 end
|
||||
for i=1,b do taken[i]=false end
|
||||
local index = 1
|
||||
while index > 0 do repeat
|
||||
repeat slots[index] = slots[index] + 1
|
||||
until slots[index] > b or not taken[slots[index]]
|
||||
if slots[index] > b then
|
||||
slots[index] = 0
|
||||
index = index - 1
|
||||
if index > 0 then
|
||||
taken[slots[index]] = false
|
||||
end
|
||||
break
|
||||
else
|
||||
taken[slots[index]] = true
|
||||
end
|
||||
if index == a then
|
||||
for i=1,a do io.write(slots[i]) io.write(" ") end
|
||||
io.write("\n")
|
||||
taken[slots[index]] = false
|
||||
break
|
||||
end
|
||||
index = index + 1
|
||||
until true end
|
||||
end
|
||||
|
||||
ipermutations(3, 3)
|
||||
5
Task/Permutations/Perl/permutations-1.pl
Normal file
5
Task/Permutations/Perl/permutations-1.pl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
use ntheory qw/forperm/;
|
||||
my @tasks = (qw/party sleep study/);
|
||||
forperm {
|
||||
print "@tasks[@_]\n";
|
||||
} scalar(@tasks);
|
||||
7
Task/Permutations/Perl/permutations-2.pl
Normal file
7
Task/Permutations/Perl/permutations-2.pl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
sub permutation {
|
||||
my ($perm,@set) = @_;
|
||||
print "$perm\n" || return unless (@set);
|
||||
permutation($perm.$set[$_],@set[0..$_-1],@set[$_+1..$#set]) foreach (0..$#set);
|
||||
}
|
||||
my @input = (qw/a 2 c 4/);
|
||||
permutation('',@input);
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
# quick and dirty recursion
|
||||
sub permutation(){
|
||||
my ($perm,@set) = @_;
|
||||
print "$perm\n" || return unless (@set);
|
||||
&permutation($perm.$set[$_],@set[0..$_-1],@set[$_+1..$#set]) foreach (0..$#set);
|
||||
}
|
||||
@input = (a,2,c,4);
|
||||
&permutation('',@input);
|
||||
|
|
@ -16,16 +16,15 @@ permSets: procedure; parse arg x,y,between,uSyms /*X things Y at a time.*/
|
|||
|
||||
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. */
|
||||
if length(_)\==1 then sep='_' /*if not 1st char, then use sep.*/
|
||||
$.k=_ /*append it to the symbol list. */
|
||||
end
|
||||
end /*k*/
|
||||
|
||||
if between=='' then between=sep /*use the appropriate separator. */
|
||||
list='$. @. between x y'
|
||||
call .permset 1
|
||||
call .permset 1 /*start with the first permuation*/
|
||||
return
|
||||
/*──────────────────────────────────.PERMSET subroutine─────────────────*/
|
||||
.permset: procedure expose (list); parse arg ?
|
||||
.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*/
|
||||
|
|
|
|||
|
|
@ -1,42 +1,42 @@
|
|||
/* Store permutations in a SAS dataset. Translation of Fortran 77 */
|
||||
data perm;
|
||||
n=6;
|
||||
array a{6} p1-p6;
|
||||
do i=1 to n;
|
||||
a(i)=i;
|
||||
end;
|
||||
n=6;
|
||||
array a{6} p1-p6;
|
||||
do i=1 to n;
|
||||
a(i)=i;
|
||||
end;
|
||||
L1:
|
||||
output;
|
||||
link L2;
|
||||
if next then goto L1;
|
||||
stop;
|
||||
output;
|
||||
link L2;
|
||||
if next then goto L1;
|
||||
stop;
|
||||
L2:
|
||||
next=0;
|
||||
i=n-1;
|
||||
next=0;
|
||||
i=n-1;
|
||||
L10:
|
||||
if a(i)<a(i+1) then goto L20;
|
||||
i=i-1;
|
||||
if i=0 then goto L20;
|
||||
goto L10;
|
||||
L20:
|
||||
j=i+1;
|
||||
k=n;
|
||||
if a(i)<a(i+1) then goto L20;
|
||||
i=i-1;
|
||||
if i=0 then goto L20;
|
||||
goto L10;
|
||||
L20:
|
||||
j=i+1;
|
||||
k=n;
|
||||
L30:
|
||||
t=a(j);
|
||||
a(j)=a(k);
|
||||
a(k)=t;
|
||||
j=j+1;
|
||||
k=k-1;
|
||||
if j<k then goto L30;
|
||||
j=i;
|
||||
if j=0 then return;
|
||||
t=a(j);
|
||||
a(j)=a(k);
|
||||
a(k)=t;
|
||||
j=j+1;
|
||||
k=k-1;
|
||||
if j<k then goto L30;
|
||||
j=i;
|
||||
if j=0 then return;
|
||||
L40:
|
||||
j=j+1;
|
||||
if a(j)<a(i) then goto L40;
|
||||
t=a(i);
|
||||
a(i)=a(j);
|
||||
a(j)=t;
|
||||
next=1;
|
||||
return;
|
||||
keep p1-p6;
|
||||
j=j+1;
|
||||
if a(j)<a(i) then goto L40;
|
||||
t=a(i);
|
||||
a(i)=a(j);
|
||||
a(j)=t;
|
||||
next=1;
|
||||
return;
|
||||
keep p1-p6;
|
||||
run;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue